+ Reply to Thread
Results 1 to 25 of 25

Add hyperlink and formula in closed workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Add hyperlink and formula in closed workbooks

    Hi,

    I have a bunch of worksheets where i need 2 small adjustments, does anyone know how i can change the code underneath to put in a hyperlink in cell b3 and a vlookup formula in cell b9?

    Thank you in advance

    Sub test()
    
    
        Dim MyPath          As String
        Dim MyFile          As String
        Dim Wkb             As Workbook
        Dim Cnt             As Long
        
        Application.ScreenUpdating = False
        
        MyPath = "C:\Users\Domenic\Documents\MyFolder\"
        
        If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        
        MyFile = Dir(MyPath & "*.xls")
        
        Cnt = 0
        Do While Len(MyFile) > 0
            Cnt = Cnt + 1
            Set Wkb = Workbooks.Open(MyPath & MyFile)
            Wkb.Worksheets("Sh").Range("B3").Value = "MyNewValue"
            Wkb.Close savechanges:=True
            MyFile = Dir
        Loop
        
        If Cnt > 0 Then
            MsgBox "Completed...", vbExclamation
        Else
            MsgBox "No files were found!", vbExclamation
        End If
        
        Application.ScreenUpdating = True
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Hi Henrikvv
    Sub AddLink()
        With Sheets("Sh")
         .Hyperlinks.Add _
                Anchor:=.Range("B3"), _
                Address:="https://www.google.co.uk/", _
                TextToDisplay:="Google"
        End With
    End Sub
    This searches for value of cell A9, looks it up in sheet " Sh2" column A , returns value found in column F, finds exact match
    Amend to match your lookup formula
    Sub AddFormula()
        Sheets("Sh").Range("B9").Formula = "=VLOOKUP(A9,'Sh2'!A:F,6,0)"
    End Sub
    To incorporate into your code
    replace:
         .Range("B3").Value = "MyNewValue"
    with something like:
           With Wkb.Worksheets("Sh")
                 'add hyperlink To B3
                     .Hyperlinks.Add _
                        Anchor:=.Range("B3"), _
                        Address:="https://www.google.co.uk/", _
                        TextToDisplay:="Google"
                  'add formula to B9
                    .Range("B9").Formula = "=VLOOKUP(A9,'Sh2'!A:F,6,0)"
            End With
    Last edited by kev_; 09-04-2017 at 08:08 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Hi Kev,

    Thank you so much for your reply, the vlookup formula works great but i need it to lookup the value in another master data sheet and then i get a error, can you see what i wrote wrong?

    Wkb.Worksheets("Prisskjema").Range("c9").Formula = "=VLOOKUP(a31,'[Kundeliste for prissetting.v2.xlsx]Dia'!$e:$f,2,0)"
    I also dont get the hyperlink to work, it is for making a hyperlink to open up a document saved at this location C:\Users\heio\Desktop\New folder (4)

    Again thank you so much in advance

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    1. HYPERLINK to file
    This works for me:
    Sub AddLink()
        With Wkb.Sheets("Sh")
         .Hyperlinks.Add _
                Anchor:=.Range("B3"), _
                Address:="C:\Users\Kev\Downloads\New folder (4)\TestFile.xlsx", _
                TextToDisplay:="TestFile"
        End With
    End Sub
    - change the path and file name to match your file

    2. VLookUp
    The formula requires the full path for Kundeliste for prissetting.v2.xlsx

    this worked for me
    Formula: copy to clipboard
    Sheets("Sh").Range("B9").Formula = "=VLOOKUP(A31,'C:\Users\kev\Downloads\New folder (4)\[TestFile.xlsx]Dia'!$E:$F,2)"

    - change the path and file name to match your file
    Last edited by kev_; 09-04-2017 at 09:44 AM.

  5. #5
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Awesome i got the hyperlink to work, thank you

    But the vlookup gets just an error message inserted in b9 when i paste this code:
    Wkb.Worksheets("Prisskjema").Range("B9").Value = Formula = "=VLOOKUP(A29;'V:\2. Administrasjon\7. Master Data\Customer Master Data\[Kundeliste for prissetting.v2]Dia'!E:F;2;0)"
    Is something in the code written wrong or is it unable to get the info from the other document?

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    I cannot see anything wrong with your Vlookup.
    It can acccess a closed file - it works for me with:
    Formula: copy to clipboard
    Sheets("Sh").Range("B9").Formula = "=VLOOKUP(A31,'C:\Users\kev\Downloads\1. New folder (4)\[TestFile.xlsx]Dia'!$E:$F,2)"


    I suggest you create the formula in the cell manually to get it to work, with both files open, then close the file containing lookup table, then copy the formula and paste it into VBA - that should work
    - perhaps try with something on C drive first, then try V drive - this may be a server authorisation problem ()
    Last edited by kev_; 09-07-2017 at 04:32 AM.

  7. #7
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    I have tried this and this comes with the right lookup.
    =VLOOKUP(A29;'[Kundeliste for prissetting.v2.xlsx]Dia'!$E:$F;2;0)

    But when i paste this into the vba the B9 cell gets filled with a FALSE without the formula
    Wkb.Worksheets("Prisskjema").Range("B9").Value = Formula = "=VLOOKUP(A29;'C:\Users\roineo\Desktop\[Kundeliste for prissetting.v2]Dia'!$E:$F;2;0)"

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    I should have spotted this before:
    this line wiill work:
    Wkb.Worksheets("Prisskjema").Range("B9").Formula =  "=VLOOKUP(A29,'C:\Users\roineo\Desktop\[Kundeliste for prissetting.v2]Dia'!$E:$F,2,0)"
    The text in RED should not be there
    .Value = Formula = "=VLOOKUp...
    Last edited by kev_; 09-07-2017 at 04:31 AM.

  9. #9
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Still get an error message, is it possible for it to paste in the forumla in cell b9 instead of just the answer to the vlookup formula so that next time i open the workbook it will do the vlookup?

  10. #10
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Why does your file name not have an Excel extension?
    [Kundeliste for prissetting.v2]
    Should end in .xlsx or .xlsm

  11. #11
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Even with adding .xlsx ending it still just comes up with nothing. Is it possible to just make it paste the formula itself and not the formula answer so that the formula will run the next time i open the workbook?

  12. #12
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    I have tried this and this comes with the right lookup.
    =VLOOKUP(A29;'[Kundeliste for prissetting.v2.xlsx]Dia'!$E:$F;2;0)
    THIS WORKS ON MY PC (I created file with correct name):
    Range("B29").Formula = "=VLOOKUP(A29,'C:\Users\kev\Desktop\[Kundeliste for prissetting.v2.xlsx]Dia'!$E:$F,2,0)"

    If I added "roineo" as user on my PC, then this too would work:

    Range("B29").Formula = "=VLOOKUP(A29,'C:\Users\roineo\Desktop\[Kundeliste for prissetting.v2.xlsx]Dia'!$E:$F,2,0)"
    I notice that your arguments are separated by ; (not ,)
    Last edited by kev_; 09-06-2017 at 10:44 AM.

  13. #13
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Ah perfect, the problem was the separation with ; when i changed them to , it works perfectly.

    But when i try to put in a hyperlink to the workbook Kundeliste for prissetting.v2.xlsx it gets an error.

    Is it possible to write as something like this?
    Wkb.Worksheets("sheet1").Range("b3").Hyperlink.Add = "=HYPERLINK(C:\Users\roineo\Desktop\[Kundeliste for prissetting.v2.xlsx)"

  14. #14
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

        With wkb.Sheets("Sheet1")
            .Hyperlinks.Add _
                Anchor:=.Range("B3"), _
                Address:="C:\Users\roineo\Desktop\Kundeliste for prissetting.v2.xlsx", _
                TextToDisplay:="Kundeliste"
        End With

  15. #15
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Thank you Kev Now both of them works perfectly.

    Do you know if its possible to change the code so that it also changes the workbooks in underlying folders?

  16. #16
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Do you know if its possible to change the code so that it also changes the workbooks in underlying folders?
    I do not understand the question
    - do you want to change values in cells in worbooks? change what?

  17. #17
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Ah sorry, maybe i formulated the question a bit bad.

    The macro now put in the vlookup and hyperlink into all workbooks in folder C:\Users\roineo\Desktop\New folder (3)\New folder.

    But there are many sub folders in this folder with hundreds of workbooks. So the question is if it is possible to change the code to also open all the workbooks in the sub folders and do the same actions with them?

    I hope this cleared it up, if not just tell me and i will try to explain a bit more.

  18. #18
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    yes it shoud be possible...
    - loop sub-folders
    ......loop workbooks
    ........loop worksheets
    .............loop hyperlinks
    - replace old link with new link
    - replace old lookup with new lookup

    But ....
    1. Are the folders sub-folders or are some of the folders sub-sub-folders?
    2. Is the link always in the same cell in every file or should VBA search to find it?
    3. Is the VlookUp always in same cell or should VBA search to find it?
    4. Does VBA need to open every file in each sub-folder?
    etc....

  19. #19
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    That is good to hear.

    1. several sub-sub folders.
    2. Link is always in the came cell.
    3. Vlookup is in the same cell.
    4. Yes it needs to open and close every file.

  20. #20
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Need to take a lot of care when modifying lots of files automatically
    Here are 2 subs to verify that the correct files and folders are being selected

    - run the first sub (to create folder list in sheet F1) with: {CTRL} k
    - delete any folders not wanted from F1 with {DEL}
    - empty rows are OK

    - next run 2nd macro (to create list of all files in those folders in sheet F2) with: {CTRL} {SHIFT} k

    When the list in F2 is correct, that list can be used to amend the files

    amend this to the correct top folder
        topfolder = "C:\Users\roineo\Desktop\New folder (3)\New folder"
    amend sub-layers
    with SubLayers = 4, folders listed down to: TopFolder\SubFolders\Sub-SubFolders\Sub-SubSubFolders
    SubLayers = 4
    Dim objFSO As Object, objFolder As Object, objSubFolder As Object
        Dim i As Integer, x As Integer, SubLayers As Integer, fr As Long, lr As Long
        Dim cel As Range, rng As Range
    
    Sub ListFoldersSubFoldersSubFolders()           'run with {CTRL} k
    
        topfolder = "C:\Users\roineo\Desktop\New folder (3)\New folder"
        SubLayers = 4
    
        i = 1:  x = 0:  fr = 1
        Sheets("F1").Activate
        Cells(1, 1).Value = topfolder
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        
        Do Until x = SubLayers
            lr = Cells(Rows.Count, 1).End(xlUp).Row
            Set rng = Range(Cells(fr, 1), Cells(lr, 1))
                
                For Each cel In rng
                    On Error Resume Next
                    Set objFolder = objFSO.GetFolder(cel.Value)
                        For Each objSubFolder In objFolder.subfolders
                            Cells(i + 1, 1) = objSubFolder.Path
                            i = i + 1
                        Next objSubFolder
                Next cel
            x = x + 1
            fr = lr + 1
        Loop
    
    End Sub
    Sub ListFilesInListedFolder()           'run with {CTRL} {SHIFT} k
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
            With Sheets("F1")
                Set rng = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            End With
        i = 0
        Sheets("F2").Activate
            For Each cel In rng
                On Error Resume Next
                Set objFolder = objFSO.GetFolder(cel.Value)
                    For Each objFile In objFolder.Files
                        Cells(i + 1, 2) = objFile.Name
                        Cells(i + 1, 1) = objFile.Path
                        i = i + 1
                    Next objFile
            Next cel
    End Sub
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    This is pretty awesome. Do i use the previous formula on them to do my changes?

  22. #22
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Do i use the previous formula on them to do my changes?
    I thnk you should be able to loop through the workbooks and either
    1. use the VBA code for the formula and the link
    OR
    2. Copy and paste the 2 cells
    I would expect both to work
    I suggest you test on 1 file first ...

    I think it will take VBA a long time to open and close hundreds of files
    - I would split sheet F2 into several smaller lists and loop each list separately.
    Last edited by kev_; 09-08-2017 at 11:45 AM.

  23. #23
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Are ALL the files listed in sheet F2 excel files?
    If not, this can help

    Function fExtension(FName)
        Dim c As Integer
        c = Len(FName) - InStrRev(FName, ".")
        If c < Len(FName) Then fExtension = Right(FName, c) Else fExtension = ""
    End Function
    Put the function in a general module and then you can use formula in column C
    Formula: copy to clipboard
    =fExtension(B1)


    then sort on Column C and eliminate any non-Excel files

  24. #24
    Registered User
    Join Date
    03-20-2017
    Location
    Oslo
    MS-Off Ver
    2010
    Posts
    35

    Re: Add hyperlink and formula in closed workbooks

    Ah ok, I keep getting a debugging error. Where exactly do i put in the formula?

  25. #25
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Add hyperlink and formula in closed workbooks

    Post the code you are using to loop through your workbooks
    thanks

+ 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. Export data with two Criteria values from closed workbook to closed workbooks VBA
    By julielara in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-07-2016, 01:56 PM
  2. VBA code to validate formula cells and do condition check in closed workbooks
    By johnmacpro in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2016, 11:52 PM
  3. VBA to apply vlookup formula in closed workbooks in path from ref of active wb
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-02-2016, 01:43 AM
  4. [SOLVED] VBA replace old formula in closed workbooks
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-05-2016, 11:11 AM
  5. Altering existing formula to work with closed workbooks?
    By mrbusto71 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-08-2015, 09:54 PM
  6. Formula for Master worksheet to pull data from multiple closed workbooks...
    By Batman24 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-29-2014, 06:24 AM
  7. Replies: 0
    Last Post: 09-22-2009, 04:11 AM

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