我很难在Excel上编写宏代码。任何帮助非常感谢:)
目前我的代码选择范围C6:C10 in' Leaves Records'并在“旧唱片”中插入B2,将所有单元格向下移动。我想完全做同样的事情,但不是复制C6:C10,我想要复制所有单元格中的“离开记录”#39;从B2到B列的第一个空白单元格。
这是我的代码(复制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
谢谢!
答案 0 :(得分:0)
如果我理解正确,你可以尝试这样的事情:
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
答案 1 :(得分:0)
这是一个复制单元格的简单代码,直到它出现空单:)
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