我有下面的代碼,它試圖打開一個文件名,如果表是空的,每天都會改變。文件名始終被命名爲這樣:VBA EXCEL:修復「運行時錯誤'9'」:下標超出範圍
- 「K:\共享\民\ TEMP \ Available_list_」 &年(NOW())&月(NOW())&日(NOW())&「 .TXT」
這背後的原因是自動文件激活,而不是複製,粘貼,每次重命名文件。我更新代碼以包含上述路徑時收到此錯誤。 有人可以指出我的錯誤在哪裏嗎?您可能會發現下面的代碼:
Private Sub UserForm_Initialize()
Application.ScreenUpdating = False
If IsEmpty(A_Regular.Range("A2")) Then
Dim TxtPath, TxtName As String
TxtPath = "K:\Shared\Num\Temp\Available_list_" & Year(Now()) & Month(Now()) & Day(Now()) & ".txt"
TxtName = Year(Now()) & Month(Now()) & Day(Now()) & ".txt"
Workbooks.OpenText Filename:=TxtPath, Origin:=437, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=True, OtherChar:=",", FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 5), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
'-------------------------------------------------------------------------'
' Setup Sheet '
'-------------------------------------------------------------------------'
With Workbooks(TxtName)
'-------------------------------------------------------------------------'
Dim TotalFree, TotalFields As Double
TotalFree = Application.WorksheetFunction.CountIf(Range("D:D"), "FREE")
Range("A1:E" & TotalFree).Copy
End With
Workbooks("Matcher.xlsm").Activate
Activation.Visible = True
Activation.Activate
With Activation
Range("A:E").PasteSpecial
Range("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1") = "GSM"
Range("B1") = "Type"
Range("C1") = "Date"
Range("D1") = "Status"
Range("E1") = "Number"
Range("F1") = "Pattern"
Call Encryption
End With
Activation.Cells.Clear
Windows(TxtName).Close
Control.Activate
Activation.Visible = False
End If
Application.WindowState = xlMinimized
NumberManagement.Show 0
End Sub
使用F8或F8 +班次調試代碼,並告訴我們例外情況。 – 2014-10-17 11:57:59