如何检查一个命令是否可以在vba中执行之前?

我想用一个macros来改变长长的一列单元格。 单元格是数字或文本。 数字,虽然,存储为文本,我想转换它们。 我想要

`Range("A1").Value = Int(Range("A1").Value)` 

,而且这个工作正常时转换为数字,作为文本存储的数字,但显然它不会工作的值是文本,没有数字。 如何设置命令来查看,如果这个转换是可能的,那么做到这一点?

使用Range.TextToColumns方法一次完成所有操作 。 VBA的开销将照顾错误控制。

 with worksheets("Sheet1") with intersect(.usedrange, .columns(1)) .numberformat = "General" .TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(0, 1) end with end with 

使用IsNumeric 。 为了避免将空格转换为零,请检查空白。

 if IsNumeric(Range("A1").Value2) and Not IsEmpty(Range("A1")) then Range("A1").Value = Int(Range("A1").Value) end if 

我认为这应该做到这一点:

 With Range("A:A") ' column A .NumberFormat = "General" .Value = .Value ' Excel should auto convert the strings to numbers End With