#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA if loop

Hi!

I'm working as accountant and I need a little help with a macro that
could relief me from several hours of scanning and replace

Basically I got to work with several sheets in excel 2000 where in
each row I have to change a value of a cell in another one (the value
that is in that cell divided by 2) if the another cell in that row is
equal to a certain value in another worksheet. Explained:

Sheet1
62 colums
Row 1
if the value in the column 51 is equal to value in Sheet2!A1 then
replace the number in column 44 with that number divided by 2. Go to
the next row till end.

Many thanks for your time and your help!

Tony
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA if loop

Hi,

Try this.
Right click the sheet tab, view code and paste this in

Sub stance()
lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)
For Each C In Myrange
If C.Value = Worksheets("Sheet2").Range("A1").Value Then
C.Offset(0, -7).Value = C.Offset(0, -7).Value / 2
End If
Next
End Sub


Mike

"Tony from Work" wrote:

Hi!

I'm working as accountant and I need a little help with a macro that
could relief me from several hours of scanning and replace

Basically I got to work with several sheets in excel 2000 where in
each row I have to change a value of a cell in another one (the value
that is in that cell divided by 2) if the another cell in that row is
equal to a certain value in another worksheet. Explained:

Sheet1
62 colums
Row 1
if the value in the column 51 is equal to value in Sheet2!A1 then
replace the number in column 44 with that number divided by 2. Go to
the next row till end.

Many thanks for your time and your help!

Tony

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA if loop

Hi Mike!

It's working great, and you are my saver, at last the saver of my eyes
and my brain!

If I may ask also some explanation of the the code, I'm in a beginner
in VBA I' d like to learn bit more

first, I don't need to declare any variable?

lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)


Here you are "dimensioning" the matrix the will be used, right?

For Each C In Myrange
If C.Value = Worksheets("Sheet2").Range("A1").Value Then
C.Offset(0, -7).Value = C.Offset(0, -7).Value / 2
End If



here you are using the Offset to have a "positional" reference of the
value that has to be changed right?..... But, how C.Value works? it
keeps in memory the value and then divides it by two? Could I use a
modified version of this syntax to change that value with another one
I decide?

Many thanks again for the help, again, it means really a lot of time
saved for me, and sorry for abusing again your time

Thanks, thank,s thanks, and kind regards,

Tony




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA if loop

Tony,

Your welcome and thanks for the feedback.
How does it work you ask.
Firstly it would be good practice to open with
'Option Explicit'
which forces variable declaration and has the added advantage of catching
typos when you think you've typed 'Myvariable' and you actually typed
'Myvaraible' and then spend a lot of time wondering why the code doesn't do
what you want.

lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)
This finds the bottom used cell in column AY and then sets a range from AY1
to that cell

For Each Cell In Myrange
If Cell.Value = Worksheets("Sheet2").Range("A1").Value Then
Cell.Offset(0, -7).Value = Cell.Offset(0, -7).Value / 2
End If
Next

The code above which I've changed slightly to make it more understandable
reads the value of every cell in the created range, compares it to sheet 2 A1
and if its the same diivides a cell 7 columns to the left by 2. Once again my
lazy code would crash here if it tried to divide text by 2 so it would have
been good practice to include the line

on error resume next

at the start of the code or check in some other way that its a number.

HTH

Mike

"Tony from Work" wrote:

Hi Mike!

It's working great, and you are my saver, at last the saver of my eyes
and my brain!

If I may ask also some explanation of the the code, I'm in a beginner
in VBA I' d like to learn bit more

first, I don't need to declare any variable?

lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)


Here you are "dimensioning" the matrix the will be used, right?

For Each C In Myrange
If C.Value = Worksheets("Sheet2").Range("A1").Value Then
C.Offset(0, -7).Value = C.Offset(0, -7).Value / 2
End If



here you are using the Offset to have a "positional" reference of the
value that has to be changed right?..... But, how C.Value works? it
keeps in memory the value and then divides it by two? Could I use a
modified version of this syntax to change that value with another one
I decide?

Many thanks again for the help, again, it means really a lot of time
saved for me, and sorry for abusing again your time

Thanks, thank,s thanks, and kind regards,

Tony





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA if loop

Mike,

Your help is again priceless to me. Following your advices I've
modified the code as follow, now I could also modify other cells with
the values I need. There is no need of an error control, those colums
are a fixed output database and they are always number... Forgive my
"baboon" coding but it's working :)

....What is now beyond my grasp is to add a conditional cell formatting
(color, bold whatever) of the changed cell to have a quick check
against with the original output.

Any other advice will be more than welcomed and appreciated....

Many thanks,

Tony



Option Explicit

Dim lastrow As Variant
Dim Myrange As Variant
Dim Cell As Variant
Dim Val1 As Long


Sub stance1()

Val1 = Sheet2.Range("B1").Value

On Error Resume Next

lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)
For Each Cell In Myrange
If Cell.Value = Worksheets("Sheet2").Range("A1").Value Then
Cell.Offset(0, -7).Value = Cell.Offset(0, -7).Value / 2

End If

If Cell.Value = Worksheets("Sheet2").Range("A1").Value Then
Cell.Offset(0, -34).Value = Val1
End If

Next

End Sub


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA if loop

Update!

thanks to a quiet Friday afternoon I tried to work out myself a
solution, and I came with

Option Explicit

Dim lastrow As Variant
Dim Myrange As Variant
Dim Cell As Variant
Dim Val1 As Long


Sub stance1()

Val1 = Sheet2.Range("B1").Value

On Error Resume Next

lastrow = Range("AY65536").End(xlUp).Row
Set Myrange = Range("AY1:AY" & lastrow)
For Each Cell In Myrange

If Cell.Value = Worksheets("Sheet2").Range("A1").Value Then
Cell.Select

With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.Font.Bold = True
End With

Cell.Offset(0, -7).Value = Cell.Offset(0, -7).Value / 2
Cell.Offset(0, -7).Font.Bold = True

Cell.Offset(0, -34).Value = Val1
Cell.Offset(0, -34).Font.Bold = True


End If

Next

End Sub

It looks awful but it works..... Any hint on a "slimmer" syntax ?
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Naming Worksheets - Loop within a loop issue klysell Excel Programming 5 March 29th 07 05:48 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM
Problem adding charts using Do-Loop Until loop Chris Bromley[_2_] Excel Programming 2 May 23rd 05 01:31 PM


All times are GMT +1. The time now is 02:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"