简体   繁体   English

将数据传输到另一个 Excel 后的宏应该清除旧/以前工作簿中的内容

[英]Macro after transfer data to another excel it should clear content in old/previous workbook

I am new to excel macros.我是 excel 宏的新手。 I have created this macro that transfer data to another excel file.我创建了这个将数据传输到另一个 excel 文件的宏。 However, after transferring the file it should clear the content from old sheet which I am not able to fix I get an error.但是,传输文件后,它应该清除旧工作表中我无法修复的内容,我收到错误消息。 Below is my code this will transfer data properly after that it stops and debugs at the below statement.下面是我的代码,它将在以下语句停止和调试之后正确传输数据。 Please help, after transferring data I want to clear content in old excel from where it has copied the data.请帮忙,传输数据后,我想从复制数据的地方清除旧 excel 中的内容。 Set ws = Workbooks("Path Old WorkBook").Worksheets("Sheet2").Range("A2").ClearContents Set ws = Workbooks("Path Old WorkBook").Worksheets("Sheet2").Range("A2").ClearContents

Private Sub CommandButton2_Click()
Dim InvoiceNumber As String
Dim ForwarderCode As String
Dim Status As String
Dim wb As Workbook
Dim ws As Workbook

Worksheets("Sheet2").Select
InvoiceNumber = Range("A2")
Worksheets("SampleFile").Select
ForwarderCode = Range("B2")
Worksheets("SampleFile").Select
Status = Range("C2")

Set wb = Workbooks.Open("Path")
Worksheets("Sheet2").Select
Worksheets("Sheet2").Range("A2").Select
RowCount = Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Sheet2").Range("A1")
.Offset(RowCount, 0) = InvoiceNumber
.Offset(RowCount, 1) = ForwarderCode
.Offset(RowCount, 2) = Status
End With

wb.Save

Set ws = Workbooks("Path Old WorkBook").Worksheets("Sheet2").Range("A2").ClearContents
Set ws = Workbooks("Path Old WorkBook").Worksheets("Sheet2").Range("B2").ClearContents
Set ws = Workbooks("Path Old WorkBook").Worksheets("Sheet2").Range("C2").ClearContents
End Sub

Did you mean for WS to refer to a Work Book or a Work Sheet ?.你的意思是让 WS 指的是工作簿还是工作?。 Anyway, just backspace over those last 3 Set ws = bits.无论如何,只需在最后 3 个Set ws =位上退格即可。 So所以
Workbooks("Path Old...").Worksheets("Sheet2").Range("A2").ClearContents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM