简体   繁体   English

Excel VBA不能从单元格中删除货币格式

[英]Excel VBA Cant' Remove Currency Format From Cell

I am trying to change an excel file column format from to "Currency" to "Text" by using this VBA code but it is not changing! 我正在尝试通过使用此VBA代码将Excel文件列格式从“货币”更改为“文本”,但它没有改变!

Private Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, Header As Long

    Header = 2     '<~~ Start row for formatting
    LastRow = 1000 '<~~ Last Row

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        With .Range("C" & Header & ":C" & LastRow)
            '
            '~~> Change format here
            '
            '~~> Number with 5 decimal places.
            .NumberFormat = "@"
        End With
    End With
End Sub

I also tried on new empty (general) column which worked but not on C. Please let me to let you know that even when i try to insert a new column Excel automatically insert it with Currency format! 我也试过了新的空(常规)列,但对C却没有用。请让我知道,即使我尝试插入新列,Excel也会自动将其以Currency格式插入! is there any other setting which may affect on this? 还有其他设置可能会影响此设置吗? Can you please let me know how I can fix this? 您能告诉我如何解决此问题吗?

Thanks 谢谢

Update: I also tried this code which didnt work ! 更新:我也尝试了此代码,它不起作用!

Sub Clear_Cell_Backgroud()

    Dim ws As Worksheet
    ActiveSheet.UsedRange.Columns("C").NumberFormat = "General"
    For Each ws In Worksheets
        Cells.Interior.ColorIndex = xlNone
    Next ws

End Sub

Your code works for me. 您的代码对我有用。 Ensure that you have the sheet name correct in 确保您的工作表名称正确

Set ws = ThisWorkbook.Sheets("Sheet1")

Also in your second code, when you say Activesheet , ensure that it is the relevant sheet which is active. 同样在第二个代码中,当您说Activesheet ,请确保它是相关的工作表。

Alternatively avoid the use of ActiveSheet and follow what you were doing in the first code. 或者,避免使用ActiveSheet并按照第一个代码中的操作进行操作。 Declare an object and work with it. 声明一个对象并使用它。

If you are working with multiple sheets then your 2nd code can be written as 如果您使用多张纸,那么您的第二个代码可以写成

Sub Clear_Cell_Backgroud()
    Dim ws As Worksheet

    For Each ws In Worksheets
        ws.Columns(3).NumberFormat = "General"
        ws.Cells.Interior.ColorIndex = xlNone
    Next ws
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM