VBA excel宏跳过最后一列



我必须从excel表(范围(创建一个csv。仅仅将其保存为csv文件并不是一个解决方案,因为我的应用程序需要在每个单元格设置引号的csv。我的表的范围是A1:P998,其中A1:P1是带有标题的行。

我有以下代码来做这项工作:

Dim DstFileName As String, DstPfad As String
Dim Delimiter As String
Dim strZe As String
Dim lRow As Long, lCol As Integer
Dim Ze As Long, Sp As Integer
Dim ff As Integer

On Error GoTo ErrorHandler

Range("A1").Select

DstPfad = "C:Temp" 'Anpassen, muss bereits existieren
DstFileName = DstPfad & "LCMPartnerImport.csv" 'Anpassen
Delimiter = ","
With ActiveSheet
lRow = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lCol = .Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
ff = FreeFile
Open DstFileName For Output As #ff
'Zeile für Zeile lesen und schreiben ...
For Ze = 1 To lRow
For Sp = 1 To lCol - 1
strZe = strZe & Chr(34) & .Cells(Ze, Sp) & Chr(34) & Delimiter
Next Sp
strZe = strZe & .Cells(Ze, Sp)
Print #ff, strZe
strZe = ""
Next Ze

End With

这不正常,完整的p列没有报价,我不知道为什么。这里有人想帮我吗?感谢

使用分隔符时,我喜欢以下模式:

Dim sep
'...
For Ze = 1 To lRow
sep = ""
strZe = ""
For Sp = 1 To lCol
strZe = strZe & sep & Chr(34) & .Cells(Ze, Sp) & Chr(34)
sep = Delimiter
Next Sp
Print #ff, strZe
Next Ze

最新更新