VBA code to Copy values to another workbook

ianharbs

New Member
Joined
Jun 4, 2024
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hello guys,

I'm new to VBA coding, so I used an AI to generate a code for me, but it didn't work as I intended.

I have a workbook with dozens of worksheets, my idea was to put a button with this macro to copy the same cells range of the active worksheet and to paste this values as a new line in another workbook one directory above.
But the code always copy the same values of the first worksheet.

I tried changing the ThisWorkbook.Sheets(1).Range to ActiveSheet.Range, but then the macro copies the registry.xlsx values.

VBA Code:
Sub RegistrarValores()

    Dim pastaRegistro As String
    pastaRegistro = ThisWorkbook.Path & "\.."

    Dim nomePlanilhaRegistro As String
    nomePlanilhaRegistro = "registry.xlsx"

    Dim celulasParaRegistrar As Variant
    celulasParaRegistrar = Array("C2", "C3", "C4", "C5")

    Dim wbRegistro As Workbook
    Set wbRegistro = Workbooks.Open(pastaRegistro & "\" & nomePlanilhaRegistro)

    Dim ultimaLinha As Long
    ultimaLinha = wbRegistro.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1

    For i = 0 To UBound(celulasParaRegistrar)
        wbRegistro.Sheets(1).Cells(ultimaLinha, i + 1).Value = ThisWorkbook.Sheets(1).Range(celulasParaRegistrar(i)).Value
    Next i

    wbRegistro.Save
    wbRegistro.Close

    ActiveSheet.PrintOut

End Sub

I don't speak English very well, but I hope you understand what I meant.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Ignore AI suggestions and rather record your own macro: you will learn a lot
For suggestions about recording macros: Automate tasks with the Macro Recorder - Microsoft Support

After having recorded the macro while you execute the operations, publish the obtained code and we will suggest the modifications to make it more efficient or suitable for your requirement. You should also clarify what you mean for "the same cells range of the active worksheet" (to be copied) and for "another workbook one directory above" (where to paste)
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top