将一系列数据从临时工作表保存到目录中的另一个工作簿

时间:2019-02-26 02:26:25

标签: excel vba

我在工作簿1中有一个工作表“ Disso”,在工作簿2中有另一个工作表“ Sheet1”。我需要将数据从工作簿1“ Disso”列A复制到H。然后打开目录转到工作簿2“ Sheet1”,然后找到从A列到H列的最后一行,然后粘贴数据。粘贴数据后,我希望将工作表“ Disso”从工作簿中删除。 我被困住了,我真的需要帮助。

Set Sheet = Worksheets("Disso") 
lastrow = Sheet.Cells(Rows.Count, "A").End(xlUp).Row 
Sheet.Range("A2:H" & lastrow).Copy 
wb.Open "C:\Users\harry\Desktop\STO Update\LDG\Workbook 2.xlsm" 
wb.Sheets("Sheet1").Paste 
wb.Save
wb.Close 
Application.DisplayAlerts = False 
Sheet.delete 
Application.DisplayAlerts = True 

1 个答案:

答案 0 :(得分:0)

您没有在目标工作表中定义第一个空单元格。另外,正确地识别和分配变量以及缩进代码也很重要。

'Define your variables
Dim sorcews As Worksheet, destwb As Workbook, destws As Worksheet

'Assign your variables
Set sorcews = ThisWorkbook.Sheets("Sheet1")

'Assign the workbook you open to a variable
Set destwb = Workbooks.Open("C:\Users\harry\Desktop\STO Update\LDG\Workbook 2.xlsm")
Set destws = ThisWorkbook.Sheets("Sheet2")

    'This section copies the range from your source ws and paste to your destination ws
    sorcews.Range("A2:H" & Cells(Rows.Count, "A").End(xlUp).Row).Copy _
    Destination:=destws.Cells(Rows.Count, 1).End(xlUp).Offset(1)

    destwb.Save
    destwb.Close

Application.DisplayAlerts = False
    scorcews.Delete
Application.DisplayAlerts = True