简体   繁体   English

VBA运行时错误9:下标超出范围

[英]VBA run-time Error 9: subscript out of range

I'm running a vba to extract the url from a column of hyperlinked texts: 我正在运行一个vba来从一列超链接文本中提取url:

Sub RemoveHyperlinks()
Dim Cell As Range

Dim i As Long
Dim k As Long
k = 1
Set Cell = Workbooks("Company_List.xlms").Sheets("Sheet1").UsedRange

i = 0

Do Until i = Cell.Hyperlinks.Count
If Cell.Hyperlinks.Count > 0 Then
Workbooks("Company_List.xlms").Sheets("Sheet1").Cells(k, 1).Value = Cell.Hyperlinks.Item(1).Address
i = i + 1
k = k + 1
End If

Loop

End Sub

And the runtime error 9 occurs at the line: 并且运行时错误9发生在以下行:

Set Cell = Workbooks("Company_List.xlms").Sheets("Sheet1").UsedRange 设置Cell = Workbooks(“Company_List.xlms”)。表格(“Sheet1”)。UsedRange

Can anyone help with this? 有人能帮忙吗? The Workbook name and Sheet name is correct, and my column data starts at A1 with heading title of column. 工作簿名称和工作表名称是正确的,我的列数据从A1开始,标题标题为列。

EDIT (2 Apr 2017): This piece of code has another error as it generated a list of url that appears to be the first one on the list. 编辑(2017年4月2日):这段代码有另一个错误,因为它生成了一个列表,似乎是列表中的第一个。 The entire list looks the same, but clicking through to each one would bring me to the correct hyperlink. 整个列表看起来都一样,但点击每个列表会带我到正确的超链接。 I would like to have the list to have all the correct respective urls, how should I edit this code? 我希望列表中包含所有正确的相应网址,我应该如何编辑此代码?

EDIT: I found the error, it should be Workbooks("Company_List.xlms").Sheets("Sheet1").Cells(k, 1).Value = Cell.Hyperlinks.Item( k ).Address 编辑:我发现错误,它应该是工作簿(“Company_List.xlms”)。表格(“Sheet1”)。单元格(k,1).Value = Cell.Hyperlinks.Item( k ).Address

If Workbooks("Company_List.xlms").Sheets("Sheet1").UsedRange returns subscript out of range then there is either not a workbook named "Company_List.xlms" in Workbooks or there is not a sheet named "Sheet1" in that workbook. 如果Workbooks("Company_List.xlms").Sheets("Sheet1").UsedRange返回subscript out of range则工作簿中没有名为“Company_List.xlms”的Workbooks或者没有名为“Sheet1”的工作表工作簿。

I suspect the first since the name is strange and should be "Company_List.xlsm" instead. 我怀疑第一个,因为这个名字很奇怪,应该是“Company_List.xlsm”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM