Hi,
I am stuck in the VBA coding to figure out how I can copy two rows of data dynamically within the same spreadsheet. I got some suggestion earlier but still can't make it to work. There are two columns of data (ColA and ColB) that I need to copy dynamically depending on the next empty cell. For example, ColA will have the TotalRegion and then the next row will be empty for the same column until it hit another Region. I need to VBA to copy the data all the way down if there is data. In another word, if ColA and ColB have data, the rest of the rows should be filled up as well.
A generous gentleman offered the following code earlier but I try to understand where I can insert the copy and paste data funtion. In another words, how can I determine where I copy the data and paste it with the loops? Sorry, but I am quite new to VBA and just try to learn VBA via recording the macro.
Thanks a million for all of the help!
Here is the code I tried to use
Sub CopyData()
Dim cl As Range
Range("A2: B2").Select
Selection.Copy
For Each cl In Range("$A$2:$A" & Range("$B$24").End(xlUp).Row)
If cl = "" Then
Selection.Paste
cl = Cells(cl.Row - 1, 1)
Else
Range(cl, Cells(cl, 1)).Copy ' this prompts error, how can I determine
the next cell to copy as I need to copy
two columns
End If
Next cl
End Sub
I am stuck in the VBA coding to figure out how I can copy two rows of data dynamically within the same spreadsheet. I got some suggestion earlier but still can't make it to work. There are two columns of data (ColA and ColB) that I need to copy dynamically depending on the next empty cell. For example, ColA will have the TotalRegion and then the next row will be empty for the same column until it hit another Region. I need to VBA to copy the data all the way down if there is data. In another word, if ColA and ColB have data, the rest of the rows should be filled up as well.
A generous gentleman offered the following code earlier but I try to understand where I can insert the copy and paste data funtion. In another words, how can I determine where I copy the data and paste it with the loops? Sorry, but I am quite new to VBA and just try to learn VBA via recording the macro.
Thanks a million for all of the help!
Here is the code I tried to use
Sub CopyData()
Dim cl As Range
Range("A2: B2").Select
Selection.Copy
For Each cl In Range("$A$2:$A" & Range("$B$24").End(xlUp).Row)
If cl = "" Then
Selection.Paste
cl = Cells(cl.Row - 1, 1)
Else
Range(cl, Cells(cl, 1)).Copy ' this prompts error, how can I determine
the next cell to copy as I need to copy
two columns
End If
Next cl
End Sub