I made a vba code which I want to click on a button (testing1) to check whether range A2 in WS "temp log" is empty, if yes, insert value of range H1 from WS "Main". however, when i run through my code, it will go to this line. For r = 2 To .Range("AA65535").End(xlUp).Row
and it will jump to end with
(Temp log).
Could someone explain that why wouldn't the code run through the for loop please?
Sub testing1()
Dim r As Integer
Dim totalValue As Object
With Worksheets("Main")
Set totalValue = .Range("H1")
End With
With Worksheets("Temp log")
For r = 2 To .Range("A65535").End(xlUp).Row
If IsEmpty(.Cells(r, "A").Value) = True Then
.Cells(r, "A").Value = totalValue
End If
Next r
end with
end sub
.Range("A65535").End(xlUp).Row
This will return the row number of the last empty row in the worksheet (for column A). If A2 in "Temp Log" is empty and every row above it is empty your FOR loop will look like:
For r = 2 to 2
Which will iterate exactly 0 times.
IF A2 is empty AND everything below it is empty, what result do you want? Fill only A2 with your TotalValue and stop, or keep on filling everything below A2 down to the bottom of the worksheet?
If your .Range(#).End(xlUp).Row finds nothing in A2 nor anywhere below it, it will return 1 (not 2), and your loop would be
For R = 2 to 1
And that will be skipped. (A loop with For r = 2 to 2
WILL run, once, with r = 2).
You may want to define the bottom row, and depending on what you find, either fill A2 with TotalValue and stop, or (if something below A2 IS populated) fill everything empty from A2 down to that last row using the loop as you have.
Or if I've missed your intent, please explain further.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.