从列

时间:2017-04-13 13:52:20

标签: excel-vba excel-2013 vba excel

我使用Excel 2013并希望通过VBA实现目标。

我有一个工作表(Table1),一个FileDialog和一个AddHyperlink-Function。我想要做的是在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

这段代码很有效。我唯一无法构建的是最后一行。我看过,我需要这样的东西

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

任何人都可以帮我把这两个代码块放在一起吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

而不是

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

使用

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