There are similar questions to this on Stack Overflow, however none of the answers applied to my issue, so I am writing a new question. I am trying to make a macro which loops through the cells in a column until it finds an empty one. It then copies some data, eg the date, into the cells of that row. Excel doesn't give me any errors, but when I click the button to run the macro, the program crashes.
Sub Button1()
Dim cell As String
cell = "A"
Dim cellCount As Integer
cellCount = 8
Dim currentDate As Date
Dim currentCell As String
currentCell = cell & cellCount
Do
If IsEmpty(currentCell) = True Then
Worksheets("Trades").Range(currentCell).Value = currentDate
cellCount = cellCount + 1
currentCell = cell & cellCount
End If
Loop Until IsEmpty(currentCell) = True
End Sub
If you just want to check if current cell is empty (Does not include anything) i recommend using something like this:
currentCell = vbNullString
"vbNullString" means that value from that specific cell didn't return any string value.
Another way for you would be that you first find last row (I suppose that you're trying to find last row in column A) and save that value to a variable. After that you use that value to save date.
Sub Button1()
Dim lastRow As Long
Dim currentDate As String
'Save current date to a string variable
currentDate = Date
'Find last row in column A and store value to variable
lastRow = Worksheets("Trades").Cells(Rows.Count, 1).End(xlUp).Row
'Save date to last row on specified column
Worksheets("Trades").Cells(lastRow, 1).Value = Date
End Sub
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.