从一本书复制到另一本书

时间:2018-02-27 18:53:10

标签: excel-vba excel-vba-mac vba excel

我试图将一些数据从一本书复制到另一本书...但我不知道如何。 现在它复制到另一个“工作表”“REGISTRO”(在同一本书中)

是否可以将数据复制到“工作表Registro”,但是在另一本现有书中?

Sub REGISTRO()
filalibre = Sheets("registros").Range("a1048576").End(xlUp).Row + 1
ActiveSheet.Range("A10").Select

fila = 10


While ActiveCell.Value <> ""
Sheets("registros").Cells(filalibre, 2) = ActiveSheet.Range("E4") 'factura
Sheets("registros").Cells(filalibre, 1) = ActiveSheet.Range("E2") 'fecha
Sheets("registros").Cells(filalibre, 3) = ActiveSheet.Range("C6") 'cliente
Sheets("registros").Cells(filalibre, 8) = ActiveSheet.Range("f26") 'subtotal
Sheets("registros").Cells(filalibre, 9) = ActiveSheet.Range("f27") 'saldo
Sheets("registros").Cells(filalibre, 10) = ActiveSheet.Range("f28") 'total

Sheets("registros").Cells(filalibre, 4) = ActiveCell.Offset(0, 0) 'cantidad
Sheets("registros").Cells(filalibre, 5) = ActiveCell.Offset(0, 1) 'producto
Sheets("registros").Cells(filalibre, 6) = ActiveCell.Offset(0, 4) '$/unidad
Sheets("registros").Cells(filalibre, 7) = ActiveCell.Offset(0, 5) 'subotal


filalibre = filalibre + 1

ActiveCell.Offset(1, 0).Select
Wend

1 个答案:

答案 0 :(得分:0)

你能试试吗?我建议你用工作表的名称替换ActiveSheet。添加要打开的文件的路径。

Sub REGISTRO()

Dim filalibre As Long, fila As Long, wb As Workbook, ws As Worksheet

Set ws = ThisWorkbook.Sheets("name of active sheet here") 'don't use activesheet
Set wb = Workbooks.Open("C:\name of file here")
filalibre = wb.Sheets("registros").Range("a" & Rows.Count).End(xlUp).Row + 1

For fila = 10 To ws.Range("A" & Rows.Count).End(xlUp).Row
    If ws.Range("A" & fila) <> vbNullString Then
        With wb.Sheets("registros")
            .Cells(filalibre, 2) = ws.Range("E4") 'factura
            .Cells(filalibre, 1) = ws.Range("E2") 'fecha
            .Cells(filalibre, 3) = ws.Range("C6") 'cliente
            .Cells(filalibre, 8) = ws.Range("f26") 'subtotal
            .Cells(filalibre, 9) = ws.Range("f27") 'saldo
            .Cells(filalibre, 10) = ws.Range("f28") 'total
            .Cells(filalibre, 4) = ws.Cells(fila, 1) 'cantidad
            .Cells(filalibre, 5) = ws.Cells(fila, 2) 'producto
            .Cells(filalibre, 6) = ws.Cells(fila, 5) '$/unidad
            .Cells(filalibre, 7) = ws.Cells(fila, 6) 'subotal
        End With
        filalibre = filalibre + 1
    End If
Next fila

wb.Close True

End Sub