I'm struggling to write the code for a Macro on Excel. Any help greatly appreciated :)
Currently my code selects the range C6:C10 in 'Leaves Records' and inserts at B2 in 'Old Records', shifting all cells down. I want to do exactly the same thing but instead of copying C6:C10, I want to copy ALL cells in 'Leaves Records' from B2 down to the first blank cell in Column B.
This is the code I have (which copies C6:C10):
Sub CopyToFirstBlankCell()
Dim bookingWS As Worksheet, mainWS As Worksheet
Dim copyRng As Range
Set bookingWS = Sheets("Leaves Records")
Set mainWS = Sheets("Old Records")
Set copyRng = bookingWS.Range("C6:C10")
mainWS.Range("B2:B" & copyRng.Rows.Count + 1).Insert Shift:=xlDown
copyRng.Copy mainWS.Range("B2")
End Sub
Thanks!
If I understood it correctly, you can try something like this:
Sub CopyToFirstBlankCell()
Dim bookingWS As Worksheet, mainWS As Worksheet
Dim copyRng As Range
Dim lastrow As Long
Set bookingWS = Sheets("Leaves Records")
Set mainWS = Sheets("Old Records")
lastrow = bookingWS.Cells(Rows.Count, "B").End(xlUp).Row
Set copyRng = bookingWS.Range("B2:B" & lastrow)
mainWS.Range("B2:B" & copyRng.Rows.Count + 1).Insert Shift:=xlDown
copyRng.Copy mainWS.Range("B2")
End Sub
Here's simple code that copies cells until it comes upon empty one :)
Dim i As Integer
i = 2
Do While Not IsEmpty(Sheets("Leaves Records").Cells(i, 2)) 'it'll iterate through B column
Sheets("Old Records").Cells(i, 2).Value = Sheets("Leaves Records").Cells(i, 2).Value
i = i + 1
Loop
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.