我想迭代整个excel文件。
Sub Rechteck1_KlickenSieAuf()
Dim Zieldatei As String
Dim Line As Integer
'activate and protetct file
ThisWorkbook.Worksheets(1).Activate
ActiveWorkbook.Protect
'Create desired file
Zieldatei = Application.GetSaveAsFilename(FileFilter:="AVL (*.rtf), *.rtf", InitialFileName:="AVL.rtf")
'Open desired file
Open Zieldatei For Output As #1
With ThisWorkbook.Worksheets(1)
For Line = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
'Write Read-In Data into target data
Print #1, Join(Application.Transpose(Application.Transpose(.Range(.Cells(Line, 1), .Cells(Line, .Columns.Count).End(xlToLeft)).Value)), "|")
Next
End With
Close #1
Exit Sub
此代码抛出消息:"不兼容的类型"。 我不知道为什么。我很感激你的每一个帮助。
答案 0 :(得分:0)
尝试以下操作,我相信它应该可行,在保护工作表之前无需激活工作表,因此我删除了该行,将声明从Integer更改为Long,并将Exit Sub更改为End Sub最后:
Sub Rechteck1_KlickenSieAuf()
Dim Zieldatei As String
Dim Line As Long
'protect file
ThisWorkbook.Worksheets(1).Protect
'Create desired file
Zieldatei = Application.GetSaveAsFilename(FileFilter:="AVL (*.rtf), *.rtf", InitialFileName:="AVL.rtf")
'Open desired file
Open Zieldatei For Output As #1
With ThisWorkbook.Worksheets(1)
For Line = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
'Write Read-In Data into target data
Print #1, Join(Application.Transpose(Application.Transpose(.Range(.Cells(Line, 1), .Cells(Line, .Columns.Count).End(xlToLeft)).Value)), "|")
Next
End With
Close #1
End Sub
<强>更新强>
而不是使用Or Error Resume Next,您可以检查给定行中的列数,并检查是否只有一列,如果它有任何数据,然后转到下一行,如下所示:
Sub Rechteck1_KlickenSieAuf()
Dim Zieldatei As String
Dim Line As Long
Dim LineData As String
'protect file
ThisWorkbook.Worksheets(1).Protect
'Create desired file
Zieldatei = Application.GetSaveAsFilename(FileFilter:="AVL (*.rtf), *.rtf", InitialFileName:="AVL.rtf")
'Open desired file
Open Zieldatei For Output As #1
With ThisWorkbook.Worksheets(1)
For Line = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
'Write Read-In Data into target data
LastCol = .Cells(Line, .Columns.Count).End(xlToLeft).Column
If Not LastCol = 1 And Not .Cells(Line, 1).Value = "" Then
LineData = Join(Application.Transpose(Application.Transpose(.Range(.Cells(Line, 1), .Cells(Line, .Columns.Count).End(xlToLeft)).Value)), "|")
Print #1, LineData
End If
Next
End With
Close #1
End Sub