+ Reply to Thread
Results 1 to 12 of 12

Code to detect previous active workbook instead of current active workbook

Hybrid View

kosherboy Code to detect previous... 02-20-2014, 06:14 PM
HaHoBe Re: Code to detect previous... 02-21-2014, 01:34 AM
kosherboy Re: Code to detect previous... 02-21-2014, 09:40 AM
HaHoBe Re: Code to detect previous... 02-21-2014, 12:55 PM
kosherboy Re: Code to detect previous... 02-21-2014, 01:41 PM
  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Code to detect previous active workbook instead of current active workbook

    Hello.

    I have two workbooks open. The first workbook will always have a different filename but the second workbook will always be called "Chart1". I have a code that inserts the first (unknown) workbook into "Chart1" but for some reason the code is hyperlinking "Charts1" into "Charts1" !

    Dim strText As String
    Dim var
    
    strText = Mid(ActiveWorkbook.Name, 15)
    strText = Left(strText, Len(strText) - 5)
    var = Split(strText, " ")
    
    With Workbooks("Chart1").Sheets("Sheet1").Activate
        ActiveCell.Activate
        ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
        Address:=ActiveWorkbook.FullName, TextToDisplay:=var(0) & " " & var(1)
    End With
    I think the code is detecting the active workbook as the hyperlinked file which is "Chart1" instead of the previously open workbook. So if the code can take the previous open workbook then I think this code would work properly.

    Thanks in advance

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code to detect previous active workbook instead of current active workbook

    Hi, kosherboy,

    one way to solve the issue might be to loop through the orkbooks in the instance and get the name of the first workbook which isn´t the destination workbook like:
    Dim ws As Workbook
    Dim strText As String
    Dim var
    Dim strName As String
    
    Const cstrWB_NAME As String = "Chart1.xlsm"
    
    If ActiveWorkbook.Name <> cstrWB_NAME Then Exit Sub
    For Each ws In Workbooks
      If ws.Name <> cstrWB_NAME Then
        strName = ws.FullName & "|" & ws.Name
        Exit For
      End If
    Next ws
    
    strText = Mid(Split(strName, "|")(1), 15)
    strText = Left(strText, Len(strText) - 5)
    var = Split(strText, " ")
    
    With Workbooks(cstrWB_NAME).Sheets("Sheet1")
        .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
        .Hyperlinks.Add Anchor:=Selection, _
        Address:=Split(strName, "|")(0), TextToDisplay:=var(0) & " " & var(1)
    End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Code to detect previous active workbook instead of current active workbook

    Hi Hahobe, nice to see you again.
    I plugged in your code but it did not insert any hyperlinks. Here is the file that I'm inserting into "Chart1.xlsm": "2014-02-20 no.430430 Product.xlsm"

    Thank you.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code to detect previous active workbook instead of current active workbook

    Hi, kosherboy,

    you would need to start the macro from witin Chart1.xlsm, I don´t know it maybe you were irritated with my description in the other post:
    Sub kosherboy()
    'http://www.excelforum.com/excel-formulas-and-functions/991000-code-to-detect-previous-active-workbook-instead-of-current-active-workbook.html
    Dim wb            As Workbook
    Dim strText       As String
    Dim var
    Dim strName       As String
    
    Const cstrWB_NAME As String = "Chart1.xlsm"
    Const cstr_PERS   As String = "PERSONAL.XLSB"
    
    If ActiveWorkbook.Name <> cstrWB_NAME Then Exit Sub
    For Each wb In Workbooks
      Select Case wb.Name
        Case cstrWB_NAME, cstr_PERS
        Case Else
          strName = wb.FullName & "|" & wb.Name
          Exit For
      End Select
    Next wb
    
    strText = Mid(Split(strName, "|")(1), 15)
    strText = Left(strText, Len(strText) - 5)
    var = Split(strText, " ")
    
    With Workbooks(cstrWB_NAME).Sheets("Sheet1")
        .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
        .Hyperlinks.Add Anchor:=Selection, _
        Address:=Split(strName, "|")(0), TextToDisplay:=var(0) & " " & var(1)
    End With
    End Sub
    If the code won´t do what you want please change into the VBE, place the cursor into the code line
    If ActiveWorkbook.Name <> cstrWB_NAME Then Exit Sub
    and press F9 to set a breakpoint. Start the code again and use F8 to singlestep through the code and see what the code does or where it breaks.

    Ciao,
    Holger

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Code to detect previous active workbook instead of current active workbook

    What description in other post?
    The code again did not work and the code highlighted the part of the script that you said I should put a break point in. I'm not sure what I should do next?

    Sorry to make you nuts and I do appreciate your help.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code to detect previous active workbook instead of current active workbook

    Hi, kosherboy,

    a breakpoint is set to get further information about the code and the variables. What you reckognized was exactly what was planned: after setting the breakpoint and starting the procedure the code will stop and allow you to continue to go through the code by pressing F8 thus being able to pass more information than just code doesn´t work (which I can´t find as the code worked fine in the samples I build to test).

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA macro for hyperlink to active workbook in the active workbook path
    By Scott Taylor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-10-2013, 05:37 AM
  2. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM
  3. Macro operates on 'closed' workbook & Current Active WB
    By BEEJAY in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-04-2011, 08:48 AM
  4. Saving only the current, active workbook
    By SinGin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-16-2010, 11:28 AM
  5. Paste data from active workbook to another workbook using code.
    By 2funny4words in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2009, 04:44 PM

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