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!
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! 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.
Alternatively avoid the use of ActiveSheet
and follow what you were doing in the first code. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.