我正在尝试使用Excel宏重新格式化使用OLE-Automation导出的电子表格
下面的代码运行正常:
Application.FindFormat.NumberFormat = "#,##0.0000000"
Application.ReplaceFormat.NumberFormat = "#,##0.00"
Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True如果我将ReplaceFormat更改为
Application.ReplaceFormat.NumberFormat = "#,##0.0" 为了只显示一个小数位,我得到一个错误1004 (应用程序定义的错误或对象定义的错误)。"0.0“也失败了。
我可以将单元格格式(Cells.NumberFormat)设置为"#,##0.0“
我只在Excel-2003上尝试过,因为它是我唯一可用的版本。
发布于 2009-01-20 04:09:30
我找到了答案的一部分。若要执行此操作,工作簿中必须已存在NumberFormat。
解决方法是将单元格的格式设置为"#,##0.0",然后执行替换:
Worksheets("Sheet1").Range("A1").NumberFormat = "#,##0.0"
Application.FindFormat.NumberFormat = "#,##0.0000000"
Application.ReplaceFormat.NumberFormat = "#,##0.00"
Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True似乎没有任何集合允许我获取自定义数字格式(According to this site anyway)。
当将Application.FindFormat设置为新格式开始抛出错误时,我发现了这一点!
发布于 2016-02-02 21:19:00
运行时错误'1004':应用程序定义或对象定义的错误
若要避免此错误,必须至少有一次同时使用两种格式的现有单元格。我有第一行作为列名,所以它们只有字符串值,并且将前两个列名设置为dateformat不会对我的工作表数据产生任何明显的影响。
Sub datum_popravak_rucno()
' add find format to cell A1
ActiveSheet.Range("A1").NumberFormat = "m/d/yyyy"
' add replace format to cell B1
ActiveSheet.Range("B1").NumberFormat = "yyyy-mm-dd hh:mm:ss"
' define find format
Application.FindFormat.NumberFormat = "m/d/yyyy"
' define replace format
Application.ReplaceFormat.NumberFormat = "yyyy-mm-dd hh:mm:ss"
' do replace on all cells
Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByColumns, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
End Sub在Excel 2003,2007,2010中工作!
https://stackoverflow.com/questions/459524
复制相似问题