无法删除 Excel 中的 rows/cell 个值;保存为 CSV 文件后,TXT 中显示额外的行
Unable to remove rows/cell values in Excel; extra lines are shown in TXT after being saved as CSV File
我正在编写代码将数据从 Excel 文件转换为 CSV 文件。但在转换为 CSV 文件之前,我想 运行 处理数据以删除任何空行。转换后的代码会在记事本中打开,供用户查看。我意识到只要任何单元格中有格式,它就会被视为空白单元格。
在下面显示的屏幕截图中,我更改了 3 行(第 14、15 和 16 行)的格式。 After adding and deleting of the rows, these are blank fields.
转换后,我发现多了3行,看起来像","。 Extra 3 lines at the bottom
我试过去除格式,但是无法去除字体等格式。我唯一能做的就是用格式删除多余的空白行。
在下面的 VBA 代码中,我设法检测到包含空格的字段的列,但我似乎无法删除行。我认为这是格式问题并使用了 .ClearFormats 和 .ClearContents,但它们也不起作用。我不确定我还能如何删除这些行。
但是,我确信任何单元格中任何形式的格式更改都会导致额外的行。任何帮助将不胜感激。
我的当前代码:
Option Explicit
Sub CreateCSV()
'Declare the data type of the variables
Dim wks As Worksheet
Dim lastCol As Integer
Dim lastRow As Long
Dim iCol As Integer
Dim iRow As Long
Dim sFilename As String
Dim cellValue As String
Dim MyTextFile
'Dim previous As Dataset
'Set the Wkb to the current active workbook and set Wks to the current sheet opened
Set wks = ActiveWorkbook.ActiveSheet
'Set the location of the csv file to a variable
sFilename = "C:\Users\Public\Desktop\cp_resi_nostro.csv"
'Set the column and row number of last cell that is not empty to a variable
lastCol = wks.Cells.SpecialCells(xlCellTypeLastCell).Column
lastRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Check through each cell to identify any empty cells
For iRow = 1 To lastRow
For iCol = 1 To lastCol
cellValue = wks.Cells(iRow, iCol).Value
Next iCol
'If Trim(cellValue) = "" Then
'Rows(iRow).Delete
'wks.AcceptChanges()
'End If
Next iRow
'Save as .CSV file and in a specific folder stated
wks.SaveAs Filename:=sFilename, FileFormat:=xlCSV
Application.DisplayAlerts = True
'Notify users that the .CSV file has been saved
MsgBox sFilename & " saved"
'To ask Ash which format he wants
MyTextFile = Shell("C:\Windows\notepad.exe C:\Users\Public\Desktop\cp_resi_nostro.csv", vbNormalFocus)
'Set Wkb and Wks to its default value
Set wks = Nothing
End Sub
替换这里的所有内容,
'Set the column and row number of last cell that is not empty to a variable
lastCol = wks.Cells.SpecialCells(xlCellTypeLastCell).Column
lastRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Check through each cell to identify any empty cells
For iRow = 1 To lastRow
For iCol = 1 To lastCol
cellValue = wks.Cells(iRow, iCol).Value
Next iCol
'If Trim(cellValue) = "" Then
'Rows(iRow).Delete
'wks.AcceptChanges()
'End If
Next iRow
...与
with wks
with .cells(1, 1).currentregion
lastCol = .columns.count
lastRow = .rows.count
end with
.cells(lastRow + 1, 1).resize(rows.count - lastRow, 1).entirerow.CLEAR
.cells(1, lastCol + 1).resize(1, columns.count - lastCol).entirecolumn.CLEAR
.usedrange
with .cells(1, 1).currentregion
for lastCol = 1 to .columns.count
.columns(lastCol).cells = .columns(lastCol).cells.value2
next lastCol
end with
end with
Range.Clear should completely clear the cell of any residual properties and calling the Worksheet.UsedRange property 会自行重置。
我正在编写代码将数据从 Excel 文件转换为 CSV 文件。但在转换为 CSV 文件之前,我想 运行 处理数据以删除任何空行。转换后的代码会在记事本中打开,供用户查看。我意识到只要任何单元格中有格式,它就会被视为空白单元格。
在下面显示的屏幕截图中,我更改了 3 行(第 14、15 和 16 行)的格式。 After adding and deleting of the rows, these are blank fields.
转换后,我发现多了3行,看起来像","。 Extra 3 lines at the bottom
我试过去除格式,但是无法去除字体等格式。我唯一能做的就是用格式删除多余的空白行。
在下面的 VBA 代码中,我设法检测到包含空格的字段的列,但我似乎无法删除行。我认为这是格式问题并使用了 .ClearFormats 和 .ClearContents,但它们也不起作用。我不确定我还能如何删除这些行。
但是,我确信任何单元格中任何形式的格式更改都会导致额外的行。任何帮助将不胜感激。
我的当前代码:
Option Explicit
Sub CreateCSV()
'Declare the data type of the variables
Dim wks As Worksheet
Dim lastCol As Integer
Dim lastRow As Long
Dim iCol As Integer
Dim iRow As Long
Dim sFilename As String
Dim cellValue As String
Dim MyTextFile
'Dim previous As Dataset
'Set the Wkb to the current active workbook and set Wks to the current sheet opened
Set wks = ActiveWorkbook.ActiveSheet
'Set the location of the csv file to a variable
sFilename = "C:\Users\Public\Desktop\cp_resi_nostro.csv"
'Set the column and row number of last cell that is not empty to a variable
lastCol = wks.Cells.SpecialCells(xlCellTypeLastCell).Column
lastRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Check through each cell to identify any empty cells
For iRow = 1 To lastRow
For iCol = 1 To lastCol
cellValue = wks.Cells(iRow, iCol).Value
Next iCol
'If Trim(cellValue) = "" Then
'Rows(iRow).Delete
'wks.AcceptChanges()
'End If
Next iRow
'Save as .CSV file and in a specific folder stated
wks.SaveAs Filename:=sFilename, FileFormat:=xlCSV
Application.DisplayAlerts = True
'Notify users that the .CSV file has been saved
MsgBox sFilename & " saved"
'To ask Ash which format he wants
MyTextFile = Shell("C:\Windows\notepad.exe C:\Users\Public\Desktop\cp_resi_nostro.csv", vbNormalFocus)
'Set Wkb and Wks to its default value
Set wks = Nothing
End Sub
替换这里的所有内容,
'Set the column and row number of last cell that is not empty to a variable
lastCol = wks.Cells.SpecialCells(xlCellTypeLastCell).Column
lastRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Check through each cell to identify any empty cells
For iRow = 1 To lastRow
For iCol = 1 To lastCol
cellValue = wks.Cells(iRow, iCol).Value
Next iCol
'If Trim(cellValue) = "" Then
'Rows(iRow).Delete
'wks.AcceptChanges()
'End If
Next iRow
...与
with wks
with .cells(1, 1).currentregion
lastCol = .columns.count
lastRow = .rows.count
end with
.cells(lastRow + 1, 1).resize(rows.count - lastRow, 1).entirerow.CLEAR
.cells(1, lastCol + 1).resize(1, columns.count - lastCol).entirecolumn.CLEAR
.usedrange
with .cells(1, 1).currentregion
for lastCol = 1 to .columns.count
.columns(lastCol).cells = .columns(lastCol).cells.value2
next lastCol
end with
end with
Range.Clear should completely clear the cell of any residual properties and calling the Worksheet.UsedRange property 会自行重置。