我有一个excel文件。我想检查单元格值。如果单元格包含不需要字符(如换行符、VbCrLf、vbLf、vbCr),我想从单元格中删除此字符,然后保存excel。
我如何用visual basic来实现这一点呢?我不懂visual basic。我无法将单元格值转换为字符串。这段代码不起作用:
Dim wb, ws As Object
Dim excel, sheet, range As Object
Dim Success
Dim oneCell As Object
Try
wb = GetWorkbook(Handle, Workbook)
ws = GetWorksheet(Handle, Workbook, Worksheet)
wb.Activate()
ws.Activate()
excel = ws.Application
sheet = excel.ActiveSheet
ws.UsedRange.Select()
For Each oneCell In excel.Selection
oneCell.Value = excel.Substitute(excel.Substitute(CStr(oneCell.Value),vbLf, vbCr), vbCr, "-").Trim()
oneCell.Value = excel.WorksheetFunction.Clean(oneCell)
Next oneCell
Success = True
Catch e As Exception
Success = False
'' Message = e.Message
Finally
wb = Nothing
ws = Nothing
excel = Nothing
sheet = Nothing
range = Nothing
End Try发布于 2020-06-30 21:24:03
尝试在Range对象上调用Application.Clean。一个非常基本的例子是这样使用它:
Range("A1").Value = Application.Clean([A1])但是,您可以在更大的范围内调用此函数。Application.Clean将删除所有不可打印的字符。
发布于 2020-06-30 21:24:10
以下是从工作表的单元格中删除ASCII-10和ASCII-13的示例:
Sub KleanUp()
With Cells
.Replace what:=Chr(10), replacement:=""
.Replace what:=Chr(13), replacement:=""
End With
End Subhttps://stackoverflow.com/questions/62658293
复制相似问题