导出为没有格式化空白值的 CSV
Exporting to CSV without formatted blank values
我有一个带有数据选项卡的 .xlsm,旨在用数据自动填充一些单元格,然后将这些行导出到 CSV 文件,但是当我 运行 我的代码时,它会复制整个活动范围,包括已格式化为空白的单元格并将它们导出为逗号,我尝试编写代码以删除新 CSV 中的逗号,并且我尝试编写代码以首先不复制格式化的空白值但是到目前为止,我尝试过的都没有奏效。
到目前为止,我已经在这上面花了几个月的时间,论坛上的其他人也有同样的问题,但似乎没有人解决它。
Sub Mcam_Order_Entry()
'
' Mcam_Order_Entry Macro
' GETS ORDER ENTRY FORM READY FOR TRUNEST
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Dim SB As Worksheet
Set SB = Worksheets("Sandbox")
Set n = SB.Cells(3, 2)
Worksheets("PNM").Copy
With ActiveSheet.UsedRange
.Copy
.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End With
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="I:\Group\DNC\MAGESTIC\Multicam\NEW MULTICAM DXFS\" & "Nest" & n & ".csv", _
FileFormat:=xlCSVMac
ActiveWorkbook.RefreshAll
Range("F15").Select
ActiveWorkbook.Save
ActiveWindow.Close
Worksheets("Sandbox").Select
Range("B3") = Range("B3") + 1
Worksheets("PNM").Select
Range("F2:F15").Select
Selection.ClearContents
Range("F2").Select
End Sub
试试,
Sub setRangToCsv()
Dim Filename As String
Dim rngDB As Range
Dim Ws As Worksheet
Set Ws = ActiveSheet
Set rngDB = Ws.UsedRange
Filename = "comacsvtest.csv"
TransToCSV Filename, rngDB
End Sub
Sub TransToCSV(myfile As String, rng As Range)
Dim vDB, vR() As String, vTxt()
Dim i As Long, n As Long, j As Integer, k As Integer
Dim objStream
Dim strFile As String
Set objStream = CreateObject("ADODB.Stream")
vDB = rng
For i = 1 To UBound(vDB, 1)
n = n + 1
k = 0
Erase vR ' add this line
For j = 1 To UBound(vDB, 2)
If vDB(i, j) <> "" Then
k = k + 1
ReDim Preserve vR(1 To k)
vR(k) = vDB(i, j)
End If
Next j
ReDim Preserve vTxt(1 To n)
vTxt(n) = Join(vR, ",")
Next i
strtxt = Join(vTxt, vbCrLf)
With objStream
'.Charset = "utf-8"
.Open
.WriteText strtxt
.SaveToFile myfile, 2
.Close
End With
Set objStream = Nothing
End Sub
我有一个带有数据选项卡的 .xlsm,旨在用数据自动填充一些单元格,然后将这些行导出到 CSV 文件,但是当我 运行 我的代码时,它会复制整个活动范围,包括已格式化为空白的单元格并将它们导出为逗号,我尝试编写代码以删除新 CSV 中的逗号,并且我尝试编写代码以首先不复制格式化的空白值但是到目前为止,我尝试过的都没有奏效。
到目前为止,我已经在这上面花了几个月的时间,论坛上的其他人也有同样的问题,但似乎没有人解决它。
Sub Mcam_Order_Entry()
'
' Mcam_Order_Entry Macro
' GETS ORDER ENTRY FORM READY FOR TRUNEST
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Dim SB As Worksheet
Set SB = Worksheets("Sandbox")
Set n = SB.Cells(3, 2)
Worksheets("PNM").Copy
With ActiveSheet.UsedRange
.Copy
.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End With
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="I:\Group\DNC\MAGESTIC\Multicam\NEW MULTICAM DXFS\" & "Nest" & n & ".csv", _
FileFormat:=xlCSVMac
ActiveWorkbook.RefreshAll
Range("F15").Select
ActiveWorkbook.Save
ActiveWindow.Close
Worksheets("Sandbox").Select
Range("B3") = Range("B3") + 1
Worksheets("PNM").Select
Range("F2:F15").Select
Selection.ClearContents
Range("F2").Select
End Sub
试试,
Sub setRangToCsv()
Dim Filename As String
Dim rngDB As Range
Dim Ws As Worksheet
Set Ws = ActiveSheet
Set rngDB = Ws.UsedRange
Filename = "comacsvtest.csv"
TransToCSV Filename, rngDB
End Sub
Sub TransToCSV(myfile As String, rng As Range)
Dim vDB, vR() As String, vTxt()
Dim i As Long, n As Long, j As Integer, k As Integer
Dim objStream
Dim strFile As String
Set objStream = CreateObject("ADODB.Stream")
vDB = rng
For i = 1 To UBound(vDB, 1)
n = n + 1
k = 0
Erase vR ' add this line
For j = 1 To UBound(vDB, 2)
If vDB(i, j) <> "" Then
k = k + 1
ReDim Preserve vR(1 To k)
vR(k) = vDB(i, j)
End If
Next j
ReDim Preserve vTxt(1 To n)
vTxt(n) = Join(vR, ",")
Next i
strtxt = Join(vTxt, vbCrLf)
With objStream
'.Charset = "utf-8"
.Open
.WriteText strtxt
.SaveToFile myfile, 2
.Close
End With
Set objStream = Nothing
End Sub