将列从一个工作表复制到另一个工作簿

时间:2015-05-26 12:26:48

标签: excel vba excel-vba

我有一张包含多张纸的工作簿。我有一张名为“MergedData”的工作表,我需要复制列B& L成为名为“Bookings.xls”的工作簿

B列是一个Word订单号,L是一个£值,我需要它复制超过0.01英镑的值,并以零值排除所有内容。

数据需要放入A列的行中。预订工作手册中的B,从第9行开始。

运行代码时,每个月都需要擦除数据。

我将此代码应用于不同承包商的多个工作簿。所以代码不能硬锁到原始文件。需要成为活动工作簿,Bookings.xls文件是所有承包商文件夹中的静态工作簿。

感谢所有人的支持。

Private Sub CommandButton1_Click()
    Dim LastDRow As Integer, _
        InitWorkSheet As Worksheet, _
        DestWorkSheet As Worksheet, _
        myData As Workbook, _
        LastWRow As Integer

Set InitWorkSheet = ActiveWorkbook.Sheets("MergedData") 'Or ActiveWorkbook.Sheets("Sheet1")

Set myData = Workbooks.Open(ActiveWorkbook.Path & "\Bookings.xls")
DoEvents
Set DestWorkSheet = myData.Sheets("Sheet1") 'Or myData.Sheets("Sheet1")

With InitWorkSheet
    LastDRow = .Rows(.Rows.Count).End(xlUp).Row
    MsgBox LastDRow
    For i = LastDRow To 1 Step -1
        If .Cells(i, "L") < 0 Then
        Else
            LastWRow = DestWorkSheet.Cells(DestWorkSheet.Rows.Count, "A").End(xlUp).Row
            If LastWRow < 9 Then LastWRow = 9
            DestWorkSheet.Cells(LastWRow, 1) = .Cells(i, "B")
            DestWorkSheet.Cells(LastWRow, 2) = .Cells(i, "L")
        End If
    Next i
End With

myData.Save

End Sub`

1 个答案:

答案 0 :(得分:1)

这个让我知道:

Private Sub CommandButton1_Click()
    Dim LastDRow As Integer, _
        InitWorkSheet As Worksheet, _
        DestWorkSheet As Worksheet, _
        myData As Workbook, _
        LastWRow As Integer

Set InitWorkSheet = ActiveWorkbook.Sheets("MergedData")
Set myData = Workbooks.Open(ActiveWorkbook.Path & "\Bookings.xls")
Set DestWorkSheet = myData.Sheets("Sheet1")

DestWorkSheet.Cells.ClearContents

With InitWorkSheet
    LastDRow = .Rows(.Rows.Count).End(xlUp).Row
    For i = 1 To LastDRow 'To 1 Step -1
        If .Cells(i, "L") < 0.01 Then
        Else
            LastWRow = DestWorkSheet.Cells(DestWorkSheet.Rows.Count, "A").End(xlUp).Row + 1
            If LastWRow < 9 Then LastWRow = 9
            DestWorkSheet.Cells(LastWRow, 1) = .Cells(i, "B")
            DestWorkSheet.Cells(LastWRow, 2) = .Cells(i, "L")
        End If
    Next i
End With

myData.Save

End Sub