Consulting

Results 1 to 5 of 5

Thread: How to put a named range in one workbook to another and call a button VBA

  1. #1
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location

    How to put a named range in one workbook to another and call a button VBA

    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!

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location
    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?

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

  5. #5
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location
    Finally got it to work. I also needed to specify what worksheet I was on. Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •