簡體   English   中英

excel宏:根據原始單元格值更改單元格值

[英]excel macro : Change cell value dependant on original cell value

錄制宏時,我想根據原始值將單元格的當前值更改為新值。 不要將其設置為“已記錄”值。

示例:單元格中的值需要從.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。

這可以在VBA中完成,但是最簡單的解決方案(如果所有值都以“。”開頭)是使用內置的“文本到列” 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM