PDA

View Full Version : How to put a named range in one workbook to another and call a button VBA



ytjjjtyj
08-03-2019, 10:28 AM
Hello,
I was wondering if anyone can show me the vba code to export a specific range in a worksheet in one workbook(workbook 1) to another specific worksheet in another workbook(workbook 2) and call the button in workbook 2 that does some things with the data that I imported in.

Thanks in advance!

Kenneth Hobs
08-03-2019, 06:34 PM
I don't know what you mean by export. Do you mean cut and paste, copy and paste, copy and paste values only?

For the other part, it depends on button type I guess, ActiveX or Forms. The usual method would be Appliication.Run() if you are not running it from the same workbook.

ytjjjtyj
08-04-2019, 08:08 AM
I need to copy data in workbook one and paste it to workbook 2. Then I have to call a button- I just checked and it is linked to a public subroutine. I don't know if it as activeX or regular button.

I want to do this all from workbook1's subroutine. Would the code differ?

Kenneth Hobs
08-04-2019, 09:31 AM
Sub MyMain()
Dim wb1 As Workbook, wb2 As Workbook
Dim r1 As Range, r2 As Range

Set wb1 = ThisWorkbook
Set wb2 = Workbooks("RunsHi.xlsm") 'already open
Set r1 = wb1.Worksheets("Sheet1").Range("A1:B4")
Set r2 = wb2.Worksheets("Sheet2").Range("A1")

r1.Copy r2

Application.Run "'" & ThisWorkbook.Path & "\RunsHi.xlsm'!Module1.Main", "Hi"
End Sub

ytjjjtyj
08-05-2019, 04:38 PM
Finally got it to work. I also needed to specify what worksheet I was on. Thank you!