+ Reply to Thread
Results 1 to 6 of 6

run macro from workbook A to perform on workbook B

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Question run macro from workbook A to perform on workbook B

    Hi there,

    I currently have 2 workbooks. Workbook A contains the macro that I have created. Workbook B is a workbook that has been given to me by a work colleague that I dont want to modify or add any modules to.

    Basically my macro is functioning, but I when I call it I dont want it to operate on Workbook A I want it to operate on a particular sheet in Workbook B.

    I seen from a lot of posts on the net that the following code
    Application.Run ("[workbook name!macro]")
    seems to be able to do this but I believe from what I have seen, you have to add this to Workbook B in my case, and I dont want to do this as I want to keep the original workbook intact.

    Is there a way to do this? If you need the workbooks I am working on I can provide them upon request but Im sure the syntax isn't too demanding - I hope!

    cheers for any help,

    Jag

  2. #2
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: run macro from workbook A to perform on workbook B

    hi Jag,

    Yes, this can be done & probably without the use of "application.run", it all depends on how you define your sheet/range to be acted on.

    I suspect that your code may currently be referencing the "activesheet". If so, this needs to be changed to a more explicit reference for example something like
     workbooks("workbook B").worksheets("Specific sheet")
    .
    Can you please post your existing code?

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: run macro from workbook A to perform on workbook B

    Hi there, yeah well I've tried the code below, but it still only seems to work on the current workbook, or workbook A in my example:

    Public Sub XAmple()
    Dim CTIP As Workbook
    Dim wsCD As Worksheet, wsNew As Worksheet
    Dim HL As Hyperlink
    Dim rngCopy As Range
    On Error GoTo ExitPoint
    Application.ScreenUpdating = False
    
    Set CTIP = Workbooks("Workbook B.xls")
    Set wsCD = Sheets("Compartment Details")
    Set wsNew = Sheets.Add(After:=Sheets(Sheets.Count))
    With CTIP
        For Each HL In wsCD.Hyperlinks
            wsCD.Activate: HL.Follow
            Set rngCopy = ActiveCell.Offset(, 1 - ActiveCell.Column).Resize(, 20)
            wsNew.Cells(wsNew.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 20).Value = rngCopy.Value
            Set rngCopy = Nothing
        Next HL
    ExitPoint:
        Set wsCD = Nothing
        Set wsNew = Nothing
        Application.ScreenUpdating = True
    End With
    End Sub
    Jag

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: run macro from workbook A to perform on workbook B

    hi Jag,

    This may work...
    If not, can you please provide more detail eg where does the code error?) & perhaps even upload 2 example workbooks?

    Public Sub XAmple2()
    Dim CTIP As Workbook
    Dim wsCD As Worksheet
    Dim wsNew As Worksheet
    Dim HL As Hyperlink
    Dim rngCopy As Range
        On Error GoTo ExitPoint
        Application.ScreenUpdating = False
    
        Set CTIP = Workbooks("Workbook B.xls")
        With CTIP
            Set wsCD = .Worksheets("Compartment Details")
            Set wsNew = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        End With
        'may be useful... (note need to check that a sheet with that name doesn't already exist)
        'wsNew.Name = "Added Sheet"
        For Each HL In wsCD.Hyperlinks
            With wsCD.Range(HL.SubAddress)
                Set rngCopy = .Offset(, 1 - .Column).Resize(, 20)
                'for testing...
                '            Application.Goto rngCopy, False
            End With
            With wsNew
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 20).Value = rngCopy.Value
            End With
            Set rngCopy = Nothing
        Next HL
    ExitPoint:
        Set wsCD = Nothing
        Set wsNew = Nothing
        Set CTIP = Nothing
        Application.ScreenUpdating = True
    End Sub
    hth
    Rob

  5. #5
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: run macro from workbook A to perform on workbook B

    Hi there,

    I've tried the above method within my workbooks but I still cant seem to get it working. All it seems to do is just add another new sheet onto the target workbook but doesnt do anything else.

    Does anyone know what I am doing wrong with the following code:

    Public Sub example1()
    Dim CTIP As Workbook
    Dim wsCD As Worksheet, wsNew As Worksheet
    Dim HL As Hyperlink
    Dim rngCopy As Range
    Dim vHeaders As Variant
    
    vHeaders = Array("Compt. Code", "Section", "Para", "Performance Statement", "ACA", "0" & Chr(10) & "FTS", _
                "1" & Chr(10) & "FTS", "2" & Chr(10) & "FTS", "3" & Chr(10) & "FTS", "4" & Chr(10) & "FTS", _
                "5" & Chr(10) & "FTS", "MOD", "Criteria", "Condition", "Remarks", "Reqd By", "Test Type", _
                "Client Approval", "Event Count", "Chap Only", "Sect Only")
    
    On Error GoTo ExitPoint
    Application.ScreenUpdating = False
    
    Set CTIP = Workbooks("CVF-10078983-FNA-03-WholeShip Compartment Inspection Matrix.xls")
    With CTIP
    CTIP.Activate
        Set wsCD = Sheets("Compartment Details")
        Set wsNew = Sheets.Add(After:=Sheets(Sheets.Count))
    
        
    
       ' CTIP.Activate
        For Each HL In CTIP.wsCD.Hyperlinks
            CTIP.wsCD.Activate: HL.Follow
            Set rngCopy = ActiveCell.Offset(, 1 - ActiveCell.Column).Resize(, 20)
            wsNew.Cells(wsNew.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 20).Value = rngCopy.Value
            wsNew.Cells(wsNew.Rows.Count, "A").End(xlUp).Offset(1).Value = wsCD.Cells(HL.Range.Row, 5).Value
            Set rngCopy = Nothing
        Next HL
    End With
    
    With CTIP.wsNew
        CTIP.Activate
        CTIP.wsNew.Activate
        With .Range("A1").Resize(, UBound(vHeaders) + 1)
            .Value = vHeaders
        End With
        .Cells.Select
        Selection.Columns.AutoFit
        Selection.Rows.AutoFit
    End With
    
    CTIP.wsNew.Range("A1").Select
    ExitPoint:
    Set wsCD = Nothing
    Set wsNew = Nothing
    CTIP.Application.ScreenUpdating = True
    End Sub
    Cheers,

    Jag

  6. #6
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: run macro from workbook A to perform on workbook B

    hi Jag,

    I may not be near a computer for the next few days so hopefully somone else can help you...
    Can you please upload a sample file with the actual layout, some hyperlinks & your latest version of the macros?

    Does anyone know what I am doing wrong with the following code:...
    The code you've posted in post 5 is seems to have grown from your original code. It would be helpful if you could tell us where/if it errors. Also can you please explain in normal sentences, what your intent of the below lines of code?
    Set rngCopy = ActiveCell.Offset(, 1 - ActiveCell.Column).Resize(, 20)
    
    wsNew.Cells(wsNew.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 20).Value = rngCopy.Value
    
    wsNew.Cells(wsNew.Rows.Count, "A").End(xlUp).Offset(1).Value = wsCD.Cells(HL.Range.Row, 5).Value
    To try & track down the error, can you please retry my post 4' code with the below section replacing the similar code that's listed in post 4?
    msgbox "how many hyperlinks?" & "there are " & wsCD.Hyperlinks
        For Each HL In wsCD.Hyperlinks
    msgbox "the address that this hyperlink goes to is " & wsCD.Range(HL.SubAddress)
            With wsCD.Range(HL.SubAddress)
                Set rngCopy = .Offset(, 1 - .Column).Resize(, 20)
                msgbox "where/s the loo" 'for testing...
                            Application.Goto rngCopy, False
            End With
            With wsNew
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 20).Value = rngCopy.Value
            End With
            Set rngCopy = Nothing
        Next HL
    ExitPoint:
    hth
    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1