录制宏时,我想将单元格的当前值更改为取决于原始值的新值。不要将其设置为“记录”值。
示例:单元格中的值需要从.1234(文本)更改为0,1234(数字)。和 .56789至0,56789
我的工作方法是:
record macro
"F2" : to change value,
"home" : go to beginning,
"del",
"del",
"0",
",",
"return",
stop record macro
但是当我在其他字段上使用宏时,即使原始值为.5678,该值也会更改为0,1234。
答案 0 :(得分:1)
这可以在VBA中完成,但最简单的解决方案(如果所有值都以“。”开头)是使用内置的“Text to Columns”Excel选项(只需选择整个列然后选择ALT + A, + E,+ F)。如果必须用VBA完成,请告诉我。希望这一点有所帮助。
编辑我写了这段代码来解决你的问题(根据单元格值的规则以“。”开头,然后通过添加“0”来切换到数字最初的文字)。然后Excel将“数字”格式作为隐式格式选取。
Sub ChangeFormat()
For i = 1 To 2 'This is the row index (it's now set to the 1st 2 rows.)
For j = 1 To 2 'This is the column Index (i.e. column 2 is B, 1 is A, etc.) (it's now set to the A and B columns)
If Left(ActiveSheet.Cells(i, j), 1) = "." Then
ActiveSheet.Cells(i, j).Value = "0" & ActiveSheet.Cells(i, j).Value
End If
Next j
Next i
End Sub