复制到另一本书不起作用

时间:2018-03-01 22:51:11

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

我正致力于让我疯狂的骚动 evertyhting工作得很好但是它很重,打开和保存需要很长时间 所以我决定把一些“表”移到另一本书上。但我无法让代码工作。

这是一本书中完整的代码

'Ariel
 If ActiveSheet.Range("C6").Value = "Ariel" Then
 filalibre = Sheets("ariel").Range("a1048576").End(xlUp).Row + 1
ActiveSheet.Range("A10").Select
fila = 10
Sheets("ariel").Cells(filalibre, 2) = ActiveSheet.Range("E4") 'factura
Sheets("ariel").Cells(filalibre, 1) = ActiveSheet.Range("E2") 'fecha
Sheets("ariel").Cells(filalibre, 3) = ActiveSheet.Range("C6") 'cliente
Sheets("ariel").Cells(filalibre, 4) = ActiveSheet.Range("f26") 'subtotal
Sheets("ariel").Cells(filalibre, 5) = ActiveSheet.Range("f27") 'saldo
Sheets("ariel").Cells(filalibre, 6) = ActiveSheet.Range("f28") 'total
End If

我知道这是我尝试的代码。我把表单“ariel”移到了一本新书上。它什么都不做

Dim wb As Workbook

If ActiveSheet.Range("C6").Value = "Ariel" Then
Set wb = Workbooks.Open("C:\MARI\clientes 2.xlsm")
filalibre = wb.Sheets("ariel").Range("a1048576").End(xlUp).Row + 1

ActiveSheet.Range("A10").Select
fila = 10
wb.Sheets("ariel").Cells(filalibre, 2) = ActiveSheet.Range("E4") 'factura
wb.Sheets("ariel").Cells(filalibre, 1) = ActiveSheet.Range("E2") 'fecha
wb.Sheets("ariel").Cells(filalibre, 3) = ActiveSheet.Range("C6") 'cliente
wb.Sheets("ariel").Cells(filalibre, 4) = ActiveSheet.Range("f26") 'subtotal
wb.Sheets("ariel").Cells(filalibre, 5) = ActiveSheet.Range("f27") 'saldo
wb.Sheets("ariel").Cells(filalibre, 6) = ActiveSheet.Range("f28") 'total
End If

任何人都可以帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

当您打开新工作簿时,它默认为ActiveWorkbook。因此,使用ActiveSheet的所有语句都会引用此新打开的工作簿。这些范围可能是空白的,你什么也没得到,因为你正在复制同一工作簿中的单元格。

在VBA代码时习惯使用ThisWorkbookActiveWorkbook等对象:

要使代码正常工作,请尝试以下操作:

Dim wb As Workbook

If ThisWorkbook.ActiveSheet.Range("C6").Value = "Ariel" Then
    Set wb = Workbooks.Open("C:\MARI\clientes 2.xlsm")
    filalibre = wb.Sheets("ariel").Range("a1048576").End(xlUp).Row + 1

    ThisWorkbook.ActiveSheet.Range("A10").Select
    fila = 10
    wb.Sheets("ariel").Cells(filalibre, 2) = ThisWorkbook.ActiveSheet.Range("E4") 'factura
    wb.Sheets("ariel").Cells(filalibre, 1) = ThisWorkbook.ActiveSheet.Range("E2") 'fecha
    wb.Sheets("ariel").Cells(filalibre, 3) = ThisWorkbook.ActiveSheet.Range("C6") 'cliente
    wb.Sheets("ariel").Cells(filalibre, 4) = ThisWorkbook.ActiveSheet.Range("f26") 'subtotal
    wb.Sheets("ariel").Cells(filalibre, 5) = ThisWorkbook.ActiveSheet.Range("f27") 'saldo
    wb.Sheets("ariel").Cells(filalibre, 6) = ThisWorkbook.ActiveSheet.Range("f28") 'total
End If

更多信息(英文版,但也有西班牙语版本。查看代码,我猜你是西班牙语。在提供的链接中,只需替换en-us并输入es-es)。

  

ThisWorkbook

     

ActiveWorkbook