In a recent task, I was doing filter through VBA on a number column. But that number column had numbers in text format. When I run the VBA script, it didn't work of course, because the filter was a number and the column had numbers as text. In that case, I needed to convert the text into numbers manually and save it. Then the macro worked perfectly. But I wanted to convert these texts into numbers using the VBA script and I was able to do it. I wanted to share this as if anyone wanted to know how to convert text values into a number using VBA, they can have a look at it.
This first and easiest method. I prefer this method over other methods as it directly converts the selected excel range into number format.
So, if you have a fixed range that you want to convert to text then use this VBA snippet.
Sub ConvertTextToNumber() Range("A3:A8").NumberFormat = "General" Range("A3:A8").Value = Range("A3:A8").Value End Sub
When you run the above code it converts the text of range A3:A8 into text.
This code can look more elegant if we write it like this.
Sub ConvertTextToNumber() With Range("A3:A8") .NumberFormat = "General" .Value = .Value End With End Sub
How does it work?
Well, it is quite simple. We first change the number format of the range to General. Then we put the value of that range into the same range using VBA. This removes the text formatting completely. Simple, isn't it?
Change the number of formatting of a dynamic range
In the above code, we changed the text to number in the above code of a fixed range but this will not be the case most of the time. To convert text to a number of dynamic ranges, we can evaluate the last used cell or select the range dynamically.
This is how it would look:
Sub ConvertTextToNumber() With Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row) .NumberFormat = "General" .Value = .Value End With
Here, I know that the range starts from A3. But I don't know where it may end.
So I dynamically identify last used excel row that has data in it using the VBA snippet Cells(Rows.Count, 1).End(xlUp).Row. It returns the last used row number that we are concatenating with "A3:A".
Note: This VBA snippet will work on the active workbook and active worksheet. If your code switches through multiple sheets, it would be better to set a range object of the intended worksheet. Like this
Sub ConvertTextToNumber() Set Rng = ThisWorkbook.Sheets("Sheet1").Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row) With Rng .NumberFormat = "General" .Value = .Value End With End Sub
The above code will always change the text to the number of the sheet1 of the workbook that contains this code.
Another method is to loop through each cell and change the cell value to a number using CSng function. Here's the code.
Sub ConvertTextToNumberLoop() Set Rng = ThisWorkbook.Sheets("Sheet1").Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row) For Each cel In Rng.Cells cel.Value = CSng(cel.Value) Next cel End Sub
In the above VBA snippet, we are using VBA For loop to iterate over each cell in the range and convert the value of each cell into a number using the CSng function of VBA.
So yeah guys, this is how you can change texts to numbers in Excel using VBA. You can use these snippets to ready your worksheet before you do any number operation on them. I hope I was explanatory enough. You can download the working file here.
Change Text to Number Using VBA.
If you have any doubts regarding this text to number conversion or any other excel/VBA related query, ask in the comments section below.
Related Articles:
How to get Text & Number in Reverse through VBA in Microsoft Excel | To reverse number and text we use loops and mid function in VBA. 1234 will be converted to 4321, "you" will be converted to "uoy". Here's the snippet.
Format data with custom number formats using VBA in Microsoft Excel | To change the number format of specific columns in excel use this VBA snippet. It coverts the number format of specified to specified format in one click.
Popular Articles:
50 Excel Shortcuts to Increase Your Productivity | Get faster at your task. These 50 shortcuts will make you work even faster on Excel.
The VLOOKUP Function in Excel | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets.
COUNTIF in Excel 2016 | Count values with conditions using this amazing function. You don't need filter your data to count specific value. Countif function is essential to prepare your dashboard.
How to Use SUMIF Function in Excel | This is another dashboard essential function. This helps you sum up values on specific conditions.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.
Sub TEXTNUM()
On Error Resume Next
Dim rSelection As Range
Set rSelection = rSelection
rSelection.Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
rSelection.Select
Set rSelection = Nothing
End Sub
How do I convert text to number when I know the row and first column but don't know end column?
Do you want the last column to be dynamic?
Sub TEXTNUM()
On Error Resume Next
Dim rSelection As Range
Set rSelection = rSelection
rSelection.Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
rSelection.Select
Set rSelection = Nothing
End Sub
Sub TEXTNUM()
On Error Resume Next
Dim rSelection As Range
Set rSelection = rSelection
rSelection.Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
rSelection.Select
Set rSelection = Nothing
End Sub