如何让 Excel 在另一个 sheet 中搜索文本,合并长度,并相应地调整超链接?
How do I get Excel to search for text in another sheet, incorporating length, and adjust hyperlink accordingly?
我正在尝试更新一个 Excel 传播sheet 中的超链接,使用另一个 sheet(同一工作簿)中的查找。问题出现在“j = c.Find(k).Row”行,我收到消息“运行时错误'91:'未设置对象变量或块变量。”
它没有给我任何关于“d = c.Find(m).Row”的问题,看起来它的设置与我完全相同,所以我不明白是什么问题以及为什么.我对 VBA 中的错误处理一无所知 - 我从来没有用过它 - 所以搜索结果可能有问题?
我找遍了所有地方,但找不到解决方案。如果我在某处遗漏了这个问题的答案,我们深表歉意,在此先感谢您的帮助!
Sub HypFix()
Dim k As String
Dim m As String
Dim i As Long
Dim g As String
Dim d As String
Dim j As String
Dim c As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'CHANGE SHEET NAMES BELOW AS NEEDED
Set c = Sheets("Tables").Range("A1:A15071")
For i = 4 To 337
If Sheets("Contents").Cells(i, "A").Value <> "" Then
k = Sheets("Contents").Cells(i, "A").Value
m = Right(Sheets("Contents").Cells(i, "A").Value, 255)
g = Sheets("Contents").Cells(i, "A").Value
If Len(Sheets("Contents").Cells(i, "A").Value) > 255 Then
d = c.Find(m).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & d, _
TextToDisplay:=g
ElseIf Len(Sheets("Contents").Cells(i, "A").Value) <= 255 Then
j = c.Find(k).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & j, _
TextToDisplay:=g
End If
End If
Next i
'Message Box when tasks are completed
MsgBox "Task Complete!"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull
End Sub
您应该始终将范围设置为 Range.Find()
。这使您可以在不抛出错误的情况下测试是否找到了值。
Sub HypFix()
Dim i As Long
Dim c As Range
Dim Target As Range
Dim What As String
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'CHANGE SHEET NAMES BELOW AS NEEDED
Set c = Sheets("Tables").Range("A1:A15071")
With Sheets("Contents")
For i = 4 To 337
What = .Cells(i, "A").Value
If Len(What) > 0 Then
Set Target = c.Find(What:=What, LookIn:=xlValues)
Rem Test if anything was found
If Not Target Is Nothing Then
Rem Look for the Last 255 characters
Set Target = c.Find(What:=Right(What, 255), LookIn:=xlValues)
End If
Rem If something was found link it
If Not Target Is Nothing Then
.Hyperlinks.Add Anchor:=.Cells(i, "A"), Address:="", SubAddress:="'Tables'!A" & Target.Row
Else
Rem Leave yourself a message of what wasn't found
Debug.Print What, " in row "; i; "Not Found"
End If
End If
Next i
End With
'Message Box when tasks are completed
MsgBox "Task Complete!"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull
End Sub
注意:当 TextToDisplay
参数从 .Hyperlinks.Add
中省略时,将显示 Anchor 单元格的测试。
我正在尝试更新一个 Excel 传播sheet 中的超链接,使用另一个 sheet(同一工作簿)中的查找。问题出现在“j = c.Find(k).Row”行,我收到消息“运行时错误'91:'未设置对象变量或块变量。”
它没有给我任何关于“d = c.Find(m).Row”的问题,看起来它的设置与我完全相同,所以我不明白是什么问题以及为什么.我对 VBA 中的错误处理一无所知 - 我从来没有用过它 - 所以搜索结果可能有问题?
我找遍了所有地方,但找不到解决方案。如果我在某处遗漏了这个问题的答案,我们深表歉意,在此先感谢您的帮助!
Sub HypFix()
Dim k As String
Dim m As String
Dim i As Long
Dim g As String
Dim d As String
Dim j As String
Dim c As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'CHANGE SHEET NAMES BELOW AS NEEDED
Set c = Sheets("Tables").Range("A1:A15071")
For i = 4 To 337
If Sheets("Contents").Cells(i, "A").Value <> "" Then
k = Sheets("Contents").Cells(i, "A").Value
m = Right(Sheets("Contents").Cells(i, "A").Value, 255)
g = Sheets("Contents").Cells(i, "A").Value
If Len(Sheets("Contents").Cells(i, "A").Value) > 255 Then
d = c.Find(m).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & d, _
TextToDisplay:=g
ElseIf Len(Sheets("Contents").Cells(i, "A").Value) <= 255 Then
j = c.Find(k).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & j, _
TextToDisplay:=g
End If
End If
Next i
'Message Box when tasks are completed
MsgBox "Task Complete!"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull
End Sub
您应该始终将范围设置为 Range.Find()
。这使您可以在不抛出错误的情况下测试是否找到了值。
Sub HypFix()
Dim i As Long
Dim c As Range
Dim Target As Range
Dim What As String
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'CHANGE SHEET NAMES BELOW AS NEEDED
Set c = Sheets("Tables").Range("A1:A15071")
With Sheets("Contents")
For i = 4 To 337
What = .Cells(i, "A").Value
If Len(What) > 0 Then
Set Target = c.Find(What:=What, LookIn:=xlValues)
Rem Test if anything was found
If Not Target Is Nothing Then
Rem Look for the Last 255 characters
Set Target = c.Find(What:=Right(What, 255), LookIn:=xlValues)
End If
Rem If something was found link it
If Not Target Is Nothing Then
.Hyperlinks.Add Anchor:=.Cells(i, "A"), Address:="", SubAddress:="'Tables'!A" & Target.Row
Else
Rem Leave yourself a message of what wasn't found
Debug.Print What, " in row "; i; "Not Found"
End If
End If
Next i
End With
'Message Box when tasks are completed
MsgBox "Task Complete!"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull
End Sub
注意:当 TextToDisplay
参数从 .Hyperlinks.Add
中省略时,将显示 Anchor 单元格的测试。