Offset getting stuck in VBA code

Latvis

New Member
Joined
Feb 22, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I cannot figure out why Offset in VBA code gets stuck after running few times...

The section of the code below is supposed to record a timer value from C2 in column G. First it checks if the first record exists in F3:G3. There's no value there yet, so it works: records the timer result after pressing the command button 3 (to reset the timer) and resets timer to 0. After resetting the timer for the second time it recognizes the existing values in F3:G3 and records new values in F4:G4. However, after resetting it for the third time and further, it does not record new values in F5:G5 and further, but overwrites the existing values in F4:G4

Private Sub CommandButton3_Click()

If Range("F3") = "" Then
Range("F3").Value = 1
Range("F3").Offset(0, 1).Value = Range("C2").Value
Else:
Range("F2").End(xlDown).Offset(1, 0).Value = Range("F2").End(xlDown).Value + 1
Range("F2").Offset(0, 1).End(xlDown).Offset(1, 0).Value = Range("C2").Value
End If
Range("C2").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
ResetIt = True
End Sub

Capture.PNG
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try changing F2 to F3 in the Else section. Because F2 is empty the xlDown command stops at the first non blank instead of the last.

It is for reasons like this that it is preferable to start from the bottom and work up rather than from the top down.
You shouldn't have any problems here once you make the suggested change, but in general an intermediate blank would still cause it to fail.
 
Upvote 0
Try changing F2 to F3 in the Else section. Because F2 is empty the xlDown command stops at the first non blank instead of the last.

It is for reasons like this that it is preferable to start from the bottom and work up rather than from the top down.
You shouldn't have any problems here once you make the suggested change, but in general an intermediate blank would still cause it to fail.
Thanks Jason!

changing F2 to F3 did not work, but changing the direction solved the issue. I updated the code in the Else section to
VBA Code:
Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Value = Cells(Rows.Count, 6).End(xlUp).Value + 1
Cells(Rows.Count, 6).End(xlUp).Offset(0, 1).Value = Range("C2").Value

and it works fine now. Thanks again :)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top