简体   繁体   English

Excel VBA脚本,用于将数据从一张纸移动到另一张纸,而不会覆盖编译错误。

[英]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.

相关问题 在Excel中将数据从一张纸移动到另一张纸。 - Moving data from one sheet to another in Excel. Excel VBA无需选择即可从一张纸复制到另一张纸 - Excel VBA copying from one sheet to another without selection Excel VBA代码可将数据从一张纸复制到另一张纸 - Excel VBA code that copies data from one sheet into another 将数据从一张纸追加到另一个Excel VBA - Appending data from one sheet to another Excel VBA Excel使用VBA将数据从一张纸复制到工作表上的另一张 - Excel copy data from one sheet to another on worksheet refresh with vba 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA VBA从一个Excel工作表复制到另一个Excel工作表? - VBA copying from one excel sheet to another excel sheet? (VBA)如何将数据从一张纸移动到另一张纸,向下和跨行移动以提取数据 - (VBA) How to move data from one sheet to another, moving down and across rows to pull data 如何从一个工作表中复制列数据,然后将其复制到VBA Excel中的另一工作表 - How to copy column data from one sheet and then copy that to another sheet in vba excel Excel公式,脚本或宏,可从一个SHEET 1获取数据,搜索,比较数据并将其输入到另一SHEET 2 - Excel Formula, script or Macro that takes data from one SHEET 1, search, compare and inputs data to another SHEET 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM