Select 一列中的超链接基于相邻列中的 "X"
Select a hyperlink in one column based on "X" in adjacent column
所以我是 VBA 的新手,我一直在努力让我的宏发挥作用。
本质上,我要做的是让程序读取一列,对于该列中的每个 "X",相邻列中的相应超链接将被 selected .
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,我尝试将其获取到 select 并激活相邻列中的单元格,但也没有成功。似乎我总是被 .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
你的意思是,如果一栏中有一个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
所以我是 VBA 的新手,我一直在努力让我的宏发挥作用。
本质上,我要做的是让程序读取一列,对于该列中的每个 "X",相邻列中的相应超链接将被 selected .
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,我尝试将其获取到 select 并激活相邻列中的单元格,但也没有成功。似乎我总是被 .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
你的意思是,如果一栏中有一个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