作者lulumiya2000 (igneous)
看板Office
標題[問題] EXCEL VBA陣列索引超出範圍(錯誤代碼9)
時間Wed Apr 26 14:56:54 2023
(若是和其他不同軟體互動之問題 請記得一併填寫)
軟體:excel
版本:excel 2019
各位大大好
目前總表有超連結過去後面試算表
然後我想在試算表建立一個超連結回總表的超連結
VBA碼是這樣
目前有些總表料號沒有後面試算表
所以我有做跳過
但是還是在
Set targetSheet = Sheets(myStr)這行跳出陣列索引超出範圍
全部代碼在下面
感謝大大解惑
Sub three()
Dim cell As Range
Dim targetSheet As Worksheet
Dim returnLink As Hyperlink
Dim targetRange As Range
Dim myStr As String
Dim i As Integer
'從第一分頁中的C3單元格獲取超連結
For Each cell In Sheets("庫存總表").Range("C3:C1056")
For i = 3 To 1056
'在每次循環中定義myStr變數,以確保其值不會超出範圍
myStr = Sheets("庫存總表").Range("C" & i).Value
If cell.Hyperlinks.Count > 0 Then
'獲取目標工作表和儲存格
Set targetSheet = Sheets(myStr)
Set targetRange = targetSheet.Range("E2")
Set cell = Sheets("庫存總表").Range("C" & i)
'創建返回超連結
Set returnLink = targetRange.Hyperlinks.Add(Anchor:=targetRange, _
Address:="", SubAddress:="庫存總表!" & cell.Address, _
TextToDisplay:="返回總表")
Else: GoTo Nextcell
End If
Next i
Nextcell:
Next cell
End Sub
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.147.76 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Office/M.1682492216.A.B3B.html
推 dennis699: 推一個 04/26 15:22
→ newacc: For迴圈一個就夠了 04/26 16:02
→ newacc: Range("C" & i)改成cell 04/26 16:03
→ newacc: 還有中間Set cell可以直接刪掉 04/26 16:05
→ newacc: cell是流程變數,不要在流程中改變它 04/26 16:06
→ newacc: 控制* 04/26 16:07