[英]Excel VBA Script for moving data from one sheet to another without overwriting compile errors.
Here's my code. 这是我的代码。 I keep getting various errors and have scoured the forums trying to find an answer, but I cant get it to compile. 我不断收到各种错误,并在论坛上搜寻了试图找到答案的答案,但我无法将其编译。 Current error is "Sub or Function undefined" I'm trying to copy all data other than headers from sheet DailyInput to the end of sheet MonthlyRoll without overriding data in MonthlyRoll. 当前错误是“ Sub或Function undefined”,我正在尝试将表头DailyInput以外的所有数据复制到表MonthlyRoll的末尾,而不会覆盖MonthlyRoll中的数据。 This would be controlled by command button, but I cant even get it to compile. 这将由命令按钮控制,但是我什至无法编译它。 I'm fairly new to VBA, and just can't figure out my error. 我对VBA还是很陌生,只是无法弄清楚我的错误。 I'm using Excel2016. 我正在使用Excel2016。
Sub copycolumns()
Dim lastrow As Long, erow As Long
lastrow = Worksheet("DailyInput").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Worksheet("DailyInput").Cells(i, 1).Copy
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheet("DailyInput").PasteDestination:=Worksheets("MonthlyRoll").Cells(erow, 1)
Worksheet("DailyInput").Cells(i, 3).Copy
Worksheet("DailyInput").PasteDestination:=Worksheets(“MonthlyRoll”).Cells(erow, 2)
Worksheet("DailyInput").Cells(i, 6).Copy
Worksheet("DailyInput").Paste Destination:=Worksheets("MonthlyRoll").Cells(erow, 3)
End If
Next i
Application.CutCopyMode = False
Worksheet("MonthlyRoll").Columns().AutoFit
Range(“A1”).Select
End Sub
First, get in the habit of using "Option Explicit" at the top of your module. 首先,养成在模块顶部使用“ Option Explicit”的习惯。 This will help A LOT! 这将帮助很多!
One of your problems is you're trying to use Worksheet, which isn't a valid VBA object. 您的问题之一是您尝试使用工作表,它不是有效的VBA对象。 Also, you're missing a space in your .Paste code. 另外,您在.Paste代码中缺少空格。 This compiles, see if it gives you the expected results. 这样编译,看看是否能给您预期的结果。
Sub copycolumns()
Dim i As Long, _
lastRow As Long, _
erow As Long
lastRow = Worksheets("DailyInput").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
Worksheets("DailyInput").Cells(i, 1).Copy
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("DailyInput").Paste Destination:=Worksheets("MonthlyRoll").Cells(erow, 1)
Worksheets("DailyInput").Cells(i, 3).Copy
Worksheets("DailyInput").Paste Destination:=Worksheets("MonthlyRoll").Cells(erow, 2)
Worksheets("DailyInput").Cells(i, 6).Copy
Worksheets("DailyInput").Paste Destination:=Worksheets("MonthlyRoll").Cells(erow, 3)
Next i
Application.CutCopyMode = False
Worksheets("MonthlyRoll").Columns().AutoFit
Range("A1").Select
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.