根据" X"选择一列中的超链接。在相邻列中

时间:2015-02-16 17:15:33

标签: excel vba hyperlink

所以我对VBA很新,我一直在努力让我的宏工作。

基本上我要做的是让一个程序读取一列,对于该列中的每个“X”,将选择相邻列中的相应超链接。

Sub Select_Hyperlinks()

Dim rng As Range, cell As Range, sel As Range
Dim sht As Worksheet

For x = 1 To 6
    Set sht = Sheets("Generator")
    Set sel = cell.Offset(-1, 0)

    Set rng = Intersect(sht.Range("D4:D9"), sht.UsedRange)
    For Each cell In rng.Cells
        If (cell.Value) <> "X" _
        Then
            If sel Is Nothing Then
            Set sel = cell.Offset(-1, 0)
            sel.Select
        End If
        Next cell
    End If
    Next x

End Sub

我还尝试了一个更简单的想法,使用Find和FindNext函数,对于每个X,我试图让它选择并激活相邻列中的单元格,但也没有运气。看起来我总是被.Offset功能搞砸了。

编辑:

经过一些进一步的研究,这是我设法提出的。我已经从一个旨在删除所有空行的宏中进行了调整。

Sub AutoOpen()

Dim xlastcell As Integer
Dim xcell As Integer
xcell = 1

Range("C200").End(xlUp).Select
xlastcell = ActiveCell.Cells   'This used to say ActiveCell.Row but I want a single cell'


Do Until xcell = xlastcell

    If Cells(xcell, 1).Value = "X" Then
    Cells(x, 1).Select
    ActiveCell.Offset(0, -1).Select   'I'm also unable to get this function to work'
    Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

    xcell = xcell - 1
    xlastcell = xlastcell - 1

    End If

xcell = xcell + 1

Loop


End Sub

1 个答案:

答案 0 :(得分:0)

您是说如果一列中有X,您想要打开超链接吗?

编辑: 使用此选项并更改内容以匹配您的变量。

Sub asdhkl()
Dim c As Hyperlink
Dim i As Range
For Each i In Sheets(1).Range("b1:b3")
    If i = "x" Then
        Set c = i.Offset(0, -1).Hyperlinks(1)
        c.Follow
    End If
Next i
End Sub