Writing an excel VBA to repeatedly check my data and paste back from another macro excel work book

S991102

New Member
Joined
Apr 22, 2021
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All, i am trying to write a VBA code to create a button in my excel workbook(workbook1) for below function

For each data in column A, copy the data and paste it to another macro workbook(workbook2) at cell "C2", then click the button in workbook2 to run the macro, after running the macro in wb2, copy the result data from sheet2.cell C30 back to my workbook1 at column B

I have created some code like below but it seems not working.

VBA Code:
Sub Auto()
    f_path = "C:\temp\workbook1.xlsm"
    set wb1 as workbooks.Open(f_path)
    f_path2 = "C:\temp\workbook2.xlsm"
    set wb2 as workbooks.Open(f_path2)
    Dim 1 as Long

    For i = 2 to 1000
    Workbooks("wb2").Worksheets("sheet1").Range("A" & i).Copy _
    Workbooks("wb1").Worksheets("Sheet1").Range("C2")
    Application.Run.workbooks("wb1").Worksheets("Sheet1").Shapes("macro1").Onaction
    Workbooks("wb1").Worksheets("Sheet2").Range("C30").Copy _
    Workbooks("wb2").Worksheets("sheet1").Range("B" & i)
    Next i

    End Sub



See if anyone can help on this, newbie here
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Instead of clicking a button on another workbook, it is easier just to run the macro directly with a command for example
Application.Run "AnotherWorkbook.xlsm!NameOfMacro"

The method of setting variable is not correct in your macro. I hope this works :)
VBA Code:
Sub Auto()
    
    Dim i As Long
    Dim wb1 As Workbook, wb2 As Workbook

    f_path = "C:\temp\workbook1.xlsm"
    Set wb1 = Workbooks.Open(f_path)
    f_path2 = "C:\temp\workbook2.xlsm"
    Set wb2 = Workbooks.Open(f_path2)
    
    For i = 2 To 1000
        wb2.Worksheets("sheet1").Range("A" & i).Copy wb1.Worksheets("Sheet1").Range("C2")
        Run "'" & wb1.Name & "'!" & "MacroName"
        wb1.Worksheets("Sheet2").Range("C30").Copy wb2.Worksheets("sheet1").Range("B" & i)
    Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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