我試圖從Excel工作簿中導出表格到管道分隔的txt文件,這些文件以與相應工作表相同的方式命名。問題是我無法讓我的宏遍歷工作簿中的不同工作表以將所有非空白單元格導出到txt文件。以下是我的代碼:如何從第一個非黑色到最後一個非空白單元格(VBA)選擇單元格範圍?
Sub TableExtract()
Dim myFile As String, WS_Count As Integer, x As Integer, rng As Range, cellValue As Variant, i As Integer, j As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For x = 1 To WS_Count
myFile = "C:\Users\mohamednuri.beitelma\Desktop\" & ActiveSheet.Name & ".txt"
Set rng = Sheets(x).Range("A1").CurrentRegion
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue & "|",
End If
Next j
Next i
Close #1
Next x
End Sub
此代碼返回錯誤。任何想法如何在第一個和最後一個非空白單元格之間的範圍內選擇內容並將其導出?
你可以顯示你得到的錯誤嗎? –