• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

hyperlink and other

For sake of forum users who cannot download from file-sharing sites. Here's complete code.

Code:
'\\ Written     : Shrivallabha S. Redij
'\\ Requirements: https://chandoo.org/forum/threads/hyperlink-and-other.36097/

Option Explicit
Public objFSO As Object
Private Sub cmdStart_Click()
'\\ Routine to Update Hyperlinks by checking files in a particular folder
Dim strFldName As String
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Title = "Select GEONAMES Folder!"
    .InitialFileName = ThisWorkbook.Path
    .Show
    If .SelectedItems.Count > 0 Then
        strFldName = .SelectedItems(1)
    Else
        MsgBox "No Folder Selected!", vbExclamation
        Exit Sub
    End If
End With

Set objFSO = CreateObject("Scripting.FileSystemObject")
ListItemsInFolder strFldName, True '\\Change Second Argument to False if you don't want to check subfolder
UpdateLinks '\\ Clean Corrupt Links which could be result of past mappings!
Set objFSO = Nothing

End Sub
Public Sub ListItemsInFolder(strPath As String, boolSubFolder As Boolean)
'\\ Routine to check files for existence and creating hyperlinks
Dim objFld As Object, objFil As Object, objSubFld As Object
Dim strName As String, strExtension As String
Dim rngFind As Range

Set objFld = objFSO.GetFolder(strPath)
For Each objFil In objFld.Files
    strExtension = objFSO.GetExtensionName(objFil.Path)
    If InStr(1, strExtension, "xls", vbTextCompare) > 0 Then
        strName = Replace(objFil.Name, "." & strExtension, "", , , vbTextCompare)
        Set rngFind = Range("D5:D" & Rows.Count).Find(strName, Range("D5"), , xlWhole)
        If Not rngFind Is Nothing Then
            ActiveSheet.Hyperlinks.Add Anchor:=rngFind, Address:=objFil.Path, TextToDisplay:=rngFind.Value
        End If
    End If
    If boolSubFolder Then
        For Each objSubFld In objFld.SubFolders
            Call ListItemsInFolder(objSubFld.Path, True)
        Next
    End If
Next

End Sub
Private Sub UpdateLinks()
'\\ Clean Corrupt Links which could be result of past mappings!
Dim rng As Range
Dim HLink As Hyperlink
For Each rng In Range("D5:D" & Range("D" & Rows.Count).End(xlUp).Row)
    If rng.Hyperlinks.Count > 0 Then
        For Each HLink In rng.Hyperlinks
            If Not objFSO.FileExists(HLink.Address) Then
                rng.Hyperlinks.Delete
                Exit For
            End If
        Next
    End If
Next
End Sub

Private Sub cmdUpdateCapital_Click()
'\\ This routine updates following:
'\\ 1. Gets "Capital Name" based on "PPLC" Value in Column H
'\\ 2. Runs through Column G and H and produces counts
Dim rng As Range, rCell As Range, rngCptCol As Range, rngPPLC As Range, rngFeat As Range, rngFeCo As Range
Dim objDic As Object
Dim HLink As Hyperlink
Dim wksThis As Worksheet
Dim wbkSource As Workbook
Dim wksSource As Worksheet
Dim varKey
Dim i As Long

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDic = CreateObject("Scripting.Dictionary")
objDic.CompareMode = vbTextCompare
Set wksThis = ThisWorkbook.Sheets(1)

Application.ScreenUpdating = False
For Each rng In wksThis.Range("D5:D" & wksThis.Range("D" & Rows.Count).End(xlUp).Row)
    If rng.Hyperlinks.Count > 0 Then
        For Each HLink In rng.Hyperlinks
            If objFSO.FileExists(HLink.Address) Then
                Set wbkSource = Workbooks.Open(HLink.Address)
                Set wksSource = wbkSource.Sheets(1)
                '\\ Update Country
                Set rngCptCol = wksSource.Range("1:1").Find("FEATURE CODE", wksSource.Range("A1"), , xlWhole)
                If Not rngCptCol Is Nothing Then
                    Set rngPPLC = rngCptCol.EntireColumn.Find("PPLC", rngCptCol, , xlWhole)
                    If Not rngPPLC Is Nothing Then rng.Offset(0, 5).Value = wksSource.Cells(rngPPLC.Row, "B").Value
                End If
                '\\ Code to update counts based on Column G
                Set rngFeat = wksThis.Range("J4:R4")
                Set rngCptCol = wksSource.Range("G1")
                objDic.RemoveAll
                For i = 2 To wksSource.Cells(wksSource.Rows.Count, rngCptCol.Column).End(xlUp).Row
                    If objDic.exists(wksSource.Cells(i, rngCptCol.Column).Value) Then
                        objDic.Item(wksSource.Cells(i, rngCptCol.Column).Value) = objDic.Item(wksSource.Cells(i, rngCptCol.Column).Value) + 1
                    Else
                        objDic.Add wksSource.Cells(i, rngCptCol.Column).Value, 1
                    End If
                Next i
                wksThis.Range("J" & rng.Row & ":R" & rng.Row).Value = 0
                For Each varKey In objDic.Keys
                    If Len(varKey) > 0 Then
                        Set rCell = rngFeat.Find(varKey, , , xlWhole)
                        If Not rCell Is Nothing Then wksThis.Cells(rng.Row, rCell.Column).Value = objDic.Item(varKey)
                    End If
                Next
                '\\ Code to update counts based on Column H
                Set rngFeCo = wksThis.Range("S4:BO4")
                Set rngCptCol = wksSource.Range("H1")
                objDic.RemoveAll
                For i = 2 To wksSource.Cells(wksSource.Rows.Count, rngCptCol.Column).End(xlUp).Row
                    If objDic.exists(wksSource.Cells(i, rngCptCol.Column).Value) Then
                        objDic.Item(wksSource.Cells(i, rngCptCol.Column).Value) = objDic.Item(wksSource.Cells(i, rngCptCol.Column).Value) + 1
                    Else
                        objDic.Add wksSource.Cells(i, rngCptCol.Column).Value, 1
                    End If
                Next i
                wksThis.Range("S" & rng.Row & ":BO" & rng.Row).Value = 0
                For Each varKey In objDic.Keys
                    If Len(varKey) > 0 Then
                        Set rCell = rngFeCo.Find(varKey, , , xlWhole)
                        If Not rCell Is Nothing Then wksThis.Cells(rng.Row, rCell.Column).Value = objDic.Item(varKey)
                    End If
                Next
               
                wbkSource.Close False
               
                Exit For
            End If
        Next
    End If
Next

Application.ScreenUpdating = True
MsgBox "Finished Updating Information!", vbInformation
Set objFSO = Nothing

End Sub
 
I have noticed these things:

1) When I click on the buttons of the two macros they work, then if I remove the hyperlinks (with the right mouse button), or at the other macro, I drop the capitals, when i go back to the two buttons, excel does not find the macros, even if I click on F8. Excel files will change over time and I will need to resume the procedures multiple times. I would like to be able to delete data and reboot macros without having these problems.

2) I noticed that I must first click on the hypertext link button otherwise the capital does not part. Is that so?

3) By testing macros on home PCs (where I use office 2010) and work (office 2013), I noticed that the macro execution speed changes (at home is much slower). I understand that is possible to create a code for a specific office version, or code valid for all versions? is this so? if this code is valid for all versions? From the work pc (if I did not make any mistakes), when I click on a hyperlink excel do not open the file.

Now I try the new version. :)
 
Same problem for the latest version. I replaced the old code with the new one in the excel sheet where hyperlinks were already activated and I deleted the portion of the extracted capital. When I activate the macro, it comes out of the closing message that you entered in the macro.
 
I have noticed these things:

1) When I click on the buttons of the two macros they work, then if I remove the hyperlinks (with the right mouse button), or at the other macro, I drop the capitals, when i go back to the two buttons, excel does not find the macros, even if I click on F8. Excel files will change over time and I will need to resume the procedures multiple times. I would like to be able to delete data and reboot macros without having these problems.

2) I noticed that I must first click on the hypertext link button otherwise the capital does not part. Is that so?

3) By testing macros on home PCs (where I use office 2010) and work (office 2013), I noticed that the macro execution speed changes (at home is much slower). I understand that is possible to create a code for a specific office version, or code valid for all versions? is this so? if this code is valid for all versions? From the work pc (if I did not make any mistakes), when I click on a hyperlink excel do not open the file.

Now I try the new version. :)
1. Second macro relies on having a existing link simply because otherwise it cannot get information from non-existent file. If it is so then you must run the first macro as it will verify two things:
- If a new file is available with match then it will create hyperlink.
- If a file has been shifted or deleted and therefore isn't valid will get non-working hyperlink removed.

2. I do not understand what you mean here.

3. Macro execution speed will differ if the computer specs differ and it can be significant. However, that is not my area of knowledge and cannot provide insight further. Regarding Excel Version itself, it should not make much of difference on VBA side as both use VBA#7. So you can assume the reason to be computer spec.

For your information, I tried to pull information using ADO&SQL Tools however it posed some difficulties and didn't give performance benefit I expected. That effort is invisible here. With the kind of data that you are having I will suggest a database solution will give you more flexibility and speed.
 
Same problem for the latest version. I replaced the old code with the new one in the excel sheet where hyperlinks were already activated and I deleted the portion of the extracted capital. When I activate the macro, it comes out of the closing message that you entered in the macro.
Re-run the first macro to establish links as explained in the previous post. It would not take too long.
 
you can skip step 2. For step 1, can you do so first to delete the external links and the sloppy returns and then proceed to the macro procedures? So maybe I could always update the file as soon as I download the new excel files to the "GEONAMES - FILE EXCEL" folder.
 
you can skip step 2. For step 1, can you do so first to delete the external links and the sloppy returns and then proceed to the macro procedures? So maybe I could always update the file as soon as I download the new excel files to the "GEONAMES - FILE EXCEL" folder.
First button will do just that as explained before. Read my response above in post #29-part 1.
 
Back
Top