简体   繁体   中英

Error 424 - copy cell value to another workbook

I'm programming a macro to consolidate data. I need to copy the cell value to the consolidate file.

My idea is to save the workbook with a new name (defined by user), then copy the data from the recently saved file to the consolidate workbook. Once the consolidate workbook is open, find the first row with no data to copy values from the saved file. Here is where I get the error 424 "object required".

''    Secuencia para guardar el nuevo 5 por que como nuevo archivo y preservar la plantilla original

Dim workbook_Name As Variant
workbook_Name = Application.GetSaveAsFilename(InitialFileName:="D:\Carpoto\AbInbev\Elaboracion y servicios") & "xlsm"
If workbook_Name <> False Then
ActiveWorkbook.SaveAs fileName:=workbook_Name
End If

Dim nuevopq As String
nuevopq = ActiveWorkbook.Name

'    THIS SEQUENCE, OPEN THE CONSOLIDATE WORKBOOK

Dim directory As String, fileName As String, sheet As Worksheet

Application.ScreenUpdating = False

directory = "D:\Carpoto\AbInbev\"
fileName = Dir(directory & "Consolidado 5pq Planta 2019.xlsx")

Workbooks.Open (directory & fileName)

Windows("Consolidado 5pq Planta 2019.xlsx").Activate

'   FIND THE NEXT EMPTY ROW TO COPY DATA HERE

Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Dim CellRange As String
CellRange = ActiveCell.Address(False, False)


Dim consol As String
Dim wbkCur As Workbook
Dim wbkNew As Workbook
Set wbkCur = Workbooks("Consolidado 5pq Planta 2019.xlsx")
Set wbkNew = Workbooks(nuevopq)


'HERE I GET THE ERROR, WHEN I WANT TO COPY THE VALUE FROM D8 TO THE EMPTY CELL IN THE CONSOLIDATE WORKBOOK

wbkCur.Range(CellRange).Value = webkNew.Range("D8").Value

My guess is your variable, CellRange , could be referring to a range off the Sheet - that would produce a 424 error. Also, in my humble opinion, please do everything you can to not use the Select method; The Select method should only be used if there;s no programmatic way to Set the range.

Specifically, you're missing the "." in your GetSaveAsFilename method, ".xlsm"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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