user1204868
user1204868

Reputation: 606

divide every row by VBA

I am trying to divide every row until the last row but I am stuck at dividing by just one row which is row 2. I was thinking of using this

Dim LastRow As Range

LastRow = Range("A" & Rows.Count).End(xlUp).Row

But my knowledge on how to make use of the range is very limited.

Do share your thoughts.. thanks! :)

My code is as follows:

Sub test1()


For Each c In Range("AL2:AS2 , BC2 ")
c.Value = c.Value / 1000
Next c

End Sub

Upvotes: 0

Views: 976

Answers (1)

d-stroyer
d-stroyer

Reputation: 2706

You can build the range by building the range string, like this:

Range("AL2:AS" & LastRow & ", BC2:BC" & LastRow)

Notice that the .Row property of Range returns a Long which is the row number, so you have to declare :

 Dim LastRow As Long

Finally this gives :

Sub test2()

 Dim LastRow As Long
 Dim myCell As Range

 LastRow = Range("A" & Rows.Count).End(xlUp).Row

 For Each myCell In Range("AL2:AS" & LastRow & ", BC2:BC" & LastRow)
  myCell.Value = myCell.Value / 1000
 Next myCell
End Sub

Upvotes: 1

Related Questions