Search code examples
excelexcel-2013vba

Add Hyperlink in Last Row from Column


I'm using Excel 2013 and want to achive a goal with VBA.

I have a Worksheet (Table1), a FileDialog and a AddHyperlink-Function. What I want to do is adding the hyperlinks to the sheet in the first empty row in column A.

Sub addLink()

Dim ws As Worksheet

Dim rng As Range

Set ws = Sheets("Tabelle1")

Dim fd As FileDialog
Dim selectedPaths() As String
Dim I As Integer

Set fd = Application.FileDialog(msoFileDialogOpen)

Set rng = Sheets("Tabelle1").Range("A1")

With fd
    .AllowMultiSelect = True
    .Title = "Select your File(s)"
    .InitialFileName = ""
    If .Show = -1 Then
        ReDim selectedPaths(.SelectedItems.Count - 1)
        For I = 0 To .SelectedItems.Count - 1
            selectedPaths(I) = .SelectedItems(I + 1)
            With ws
            .Hyperlinks.Add Anchor:=rng, Address:=selectedPaths(I)
            End With
        Next I
    End If
End With

Set fd = Nothing

End Sub

This code is working and doing good. Only thing that I can't build is the last Row. I have seen, that I need something like this

Sub LastRowInOneColumn()
Dim LastRow As Long
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
End Sub

Can anyone help me bring this two code blocks together? Thanks in advance.


Solution

  • Instead of

    Set rng = Sheets("Tabelle1").Range("A1")
    

    use

    With Sheets("Tabelle1")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rng = .Cells(LastRow + 1, "A")
    End With