我正在嘗試編寫一個宏來檢查所有工作表名稱是否符合某些條件(特別是在名稱中包含'TUBA'),如果符合,將這些表單上的範圍導出爲文本文件,其中表單名稱爲文件名。我得到錯誤91:對象變量或塊變量未設置,並在調試If WS.name Like "TUBA*" Then
線突出顯示。我怎樣才能解決這個問題?有問題的代碼如下。我以前使用幾乎相同的代碼獲得成功,但沒有If
聲明(如下面第二個塊所示),所以我認爲它是我將其添加進來的方式。如果我需要設置變量,哪一個我錯過了?Excel VBA錯誤91,試圖導出某些工作表的數據到文本文件
Sub ExportTubatoText()
Dim c As Range, r As Range
Dim output As String
Dim lngcount As Long
Dim WS As Worksheet
Dim Name As String
Dim strFolder As String
strFolder = GetFolder("L:TUBA\")
'\ dialog box opens in that folder as default
'strFolder = GetFolder("L:TUBA\")
If strFolder <> "" Then
MsgBox strFolder
End If
For Each sh In ThisWorkbook.Worksheets
'if worksheet has 'TUBA' in the title, then it is exported to text
If WS.Name Like "TUBA*" Then
output = ""
For Each r In sh.Range("F3:F200").Rows
For Each c In r.Cells
output = output & c.Value
Next c
output = output & vbNewLine
Next r
Name = sh.Name
Open strFolder & "\" & Name & ".txt" For Output As #1
Print #1, output
Close
End If
Next
End Sub
成功代碼:
For Each sh In ThisWorkbook.Worksheets
output = ""
For Each r In sh.Range("O2:O500").Rows
For Each c In r.Cells
output = output & c.Value
Next c
output = output & vbNewLine
Next r
Name = sh.Name
Open strFolder & "\" & Name & ".txt" For Output As #1
Print #1, output
Close
Next
正如後續在這個 - Option Explicit'會立即標記這個錯誤... – bobajob
豐富,如果這解決了您的問題,將其標記爲答案。如果沒有,請回復,以便有人可以嘗試提供幫助。 – Rdster
我現在得到一個類型不匹配的錯誤,輸出爲'output = output&c.Value'。我已經將'output'變量指定爲字符串,但這是否與單元格中包含的小數位一起使用? Rdster,道歉,一直忙於工作 –