VBA: Convert all worksheet text to number - vba

I'm looking to to convert automatically all number stored as text in worksheet to numbers with VBA. Excel seems to detect automatically theses values, there is a way in VBA to convert all theses numbers ?
I found this following solution to convert a text to a number, but i want this apply to all the worksheet and not to a specify Range, because the worksheet is dynamic.
Range("F:F").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Somebody have an idea ?

with activesheet.usedRange
.numberFormat = "General"
.value = .value
End with

Guessing that the problem is in the different decimal separator, which is quite a big problem in Germany and France. If this is the case, this works:
Option Explicit
Public Sub TestMe()
On Error GoTo TestMe_Error
Dim myCell As Range
Dim tryCell As String
For Each myCell In Cells.SpecialCells(xlCellTypeConstants)
tryCell = Replace(myCell, ",", ".")
If IsNumeric(tryCell) Then
myCell = Replace(myCell, ",", ".")
End If
Next myCell
On Error GoTo 0
Exit Sub
TestMe_Error:
MsgBox "No contant values!"
End Sub
The code checks all the cells without formula in the ActiveSheet. Then, if by changing the , to a . the cell would become numeric, it changes it.

Related

Replace cell values in specific sheets with defined name

I am trying to run some code that replaces the cell values in a specific column with a defined name. In addition, I have a condition that the replacement should only take place if the first 9 characters of the values are xxxxxxxxx.
More precisely, it should change the values in C:C in 2 specific worksheets (I don't want to loop through the whole workbook).
I am not sure why nothing happens in the code (no error messages, nothing).
I presume, however, that I should not use With if I want the code to work in these 2 specific worksheets. I am also aware that my use of Range is probably not totally correct.
Sub ChangeMe()
Dim cl As Range
For Each cl In Worksheets("Sheet1").Range("C:C").End(xlUp)
With Worksheets("Sheet2").Range("C:C").End(xlUp)
If Left(cl.Value, 9) = "XXXXXXXXX" Then
cl.Value = ThisWorkbook.Names("MyDefinedName").RefersToRange
End If
End With
Next cl
End Sub
In answer your original questions:
I am not sure why nothing happens in the code (no error messages, nothing).
Nothing happens because your worksheet values are lowercase xxxxxxxxx, whilst your code checks for uppercase XXXXXXXXX.
I presume, however, that I should not use With if I want the code to work in these 2 specific worksheets.
Actually, you can use With with multiple sheets, as I will demonstrate below.
I am also aware that my use of Range is probably not totally correct.
That is true. If you were to fix the uppercase issue, only C1 would be changed. This is because .End() works on a single cell. If you supply a multi-cell range, it uses the top left most cell. So .Range("C:C").End(xlUp) is equivalent to .Range("C1").End(xlUp) which evaluates to just C1.
The following will answer your updated question:
Option Explicit
Public Sub ChangeMe()
Const l_xxxxxxxxx As String = "xxxxxxxxx"
Const l_MyDefinedName As String = "MyDefinedName"
Const s_Delimiter As String = ","
Const s_WorkSheetNames As String = "Sheet1,Sheet2"
Const s_ColumnToChange As String = "C:C"
Dim varWorkSheetName As Variant
For Each varWorkSheetName In Split(s_WorkSheetNames, s_Delimiter)
With Worksheets(varWorkSheetName).Range(s_ColumnToChange)
Dim rngCell As Range
For Each rngCell In .Resize(.Cells(Rows.Count).End(xlUp).Row)
With rngCell
Dim strCellValue As String: strCellValue = .Value2
If Left(strCellValue, Len(l_xxxxxxxxx)) = l_xxxxxxxxx Then
.Value2 _
= Names(l_MyDefinedName).RefersToRange.Value2 _
& Right$(strCellValue, Len(strCellValue) - Len(l_xxxxxxxxx))
End If
End With
Next rngCell
End With
Next varWorkSheetName
End Sub
Notes:
It is a good idea to use constants so all literal values are typed once only and kept grouped together.
Using .Value2, instead of .Value, is the recommended way to access a cell's value as it avoids implicit casting and is therefore faster. (Using .Value can also sometimes cause issues.)
Surprisingly, in VBA there are good reasons to put a variable declaration as close as possible to the first use of the variable. Two such reasons are 1) it improves readability, and 2) it simplifies future refactoring. Just remember that the variable is not reinitialised every time the Dim is encountered. Initialisation only occurs the first time.
If I understood your post correctly (which I doubt it), I think you want to loop through column "C" in both "Sheet1" and "Sheet2". Every cell that starts with 9 "XXXXXXXXX", should be replaced with the value in "MyDefinedName" Named Range.
Code
Option Explicit
Sub ChangeMe()
Dim cl As Range
Dim sht As Worksheet
For Each sht In ThisWorkbook.Sheets
With sht
If .Name = "Sheet1" Or .Name = "Sheet2" Then
For Each cl In .Range("C1:C" & .Cells(.rows.Count, "C").End(xlUp).Row)
If Left(cl.Value, 9) = "XXXXXXXXX" Then
cl.Value = ThisWorkbook.Names("MyDefinedName").RefersToRange
End If
Next cl
End If
End With
Next sht
End Sub
Let's imagine that this is your input:
In this case, you want to change the values in range A1:A2 to the value in C1 (named range xxxx123), because it starts with xxxx123. This is the code to achieve it:
Public Sub TestMe()
Dim myCell As Range
Dim myNamedRange As String
myNamedRange = "xxxx123"
For Each myCell In Range("A1:A2")
If Left(myCell, Len(myNamedRange)) = myNamedRange Then
myCell.Value = Range(myNamedRange)
End If
Next myCell
End Sub

VBA search and copy

I'm automating an update I have to do and part of the macro I want to write needs specific text from what gets populated.
I have the following types of text in the same column for hundreds of rows:
ScreenRecording^naushi02^procr^10035
procr^10635^ScreenRecording^misby01
ScreenRecording^liw03^procr^10046
I've bold the text I need. I want to either replace the whole text with just what I need or place what I need in the next column, same row.
I had wrote something which worked for 60 or so lines before I realised that there are variations in the format. For the main, it's all the same which is why I didn't realise at first and I've spent a lot of wasted time writing something that is now useless... so I'm asking for expert help please.
Once I've got what I need from the first row, I need to move down until the last entry repeating.
I had some code which obviously didn't work fully.
I have thought about using the text 'ScreenRecording' in a search along with the special character which I can't find on my keyboard and then trying to copy all text from that point upto and including the 2nd numerical character. I don't know how to do this, if it would work or even if it's a good idea but because I've spent so much time trying to figure it out, I need some help please.
Thanks in advance
If you always want to return the value after the word 'ScreenRecording`, you can use the following function to do so.
Include it in a SubRoutine to replace in place if needed:
Function SplitScreenRecording(sInput As String) As String
Dim a As Variant
Const SDELIM As String = "^"
Const LOOKUP_VAL As String = "ScreenRecording"
a = Split(sInput, SDELIM)
If IsError(Application.Match(LOOKUP_VAL, a, 0)) Then
SplitScreenRecording = CVErr(2042)
Else
SplitScreenRecording = a(Application.Match(LOOKUP_VAL, a, 0))
End If
End Function
Sub ReplaceInPlace()
Dim rReplace As Range
Dim rng As Range
Set rReplace = Range("A1:A3")
For Each rng In rReplace
rng.Value = SplitScreenRecording(rng.Value)
Next rng
End Sub
if you want to replace:
Sub main2()
Dim key As String
Dim replacementStrng As String
key = "ScreenRecording"
replacementStrng = "AAA"
With Worksheets("mysheet01").columns("A") '<--| change "mysheet01" and "A" to your actual sheet name and column to filter
.Replace what:=key & "^*^", replacement:=key & "^" & replacementStrng & " ^ ", LookAt:=xlPart
.Replace what:="^" & key & "^*", replacement:="^" & key & "^" & replacementStrng, LookAt:=xlPart
End With
End Sub
while if you want to place what you need in the next column:
Sub main()
Dim myRng As Range
Set myRng = GetRange(Worksheets("mysheet01").columns("A"), "ScreenRecording^") '<--| change "mysheet01" and "A" to your actual sheet name and column to filter
myRng.Offset(, 1) = "value that I need to place in next row" '<--| change the right part of the assignment to what you need
End Sub
Function GetRange(rng As Range, key As String) As Range
With rng
.AutoFilter Field:=1, Criteria1:="*" & key & "*" '<--| apply current filtering
If Application.WorksheetFunction.Subtotal(103, .Cells) > 0 Then '<--| if there are visible cells other than the "header" one
With .SpecialCells(xlCellTypeConstants)
If InStr(.SpecialCells(xlCellTypeVisible).Cells(1, 1), key & "^") > 0 Then
Set GetRange = .SpecialCells(xlCellTypeVisible) '<--|select all visible cells
Else
Set GetRange = .Resize(.Parent.Cells(.Parent.Rows.Count, .Column).End(xlUp).row - 1).Offset(1).SpecialCells(xlCellTypeVisible) '<--|select visible rows other than the first ("headers") one
End If
End With
End If
.Parent.AutoFilterMode = False '<--| remove drop-down arrows
End With
End Function

Use User-defined range as input for cell parsing

I'm writing a macro in Excel 2010 in order to remove line breaks in multiple cells of a column. This cells need to be selected by the user. Following this previous post I was able to create an InputBox to let the user select the range but now, I am unable to process the data within the selection.
My previous code without the selection range parsed an entire column with a regexp to find a pattern in the string within the cells and change its contents.
I did this with a For i To Rows.Count block of code like this:
For i = 1 To Rows.Count
If Not IsEmpty(Cells(i, 5).Value) Then
varString = Sheets(ActiveSheet.Name).Cells(i, 5).Text
Sheets(ActiveSheet.Name).Cells(i,5).Value=objRegExp.Replace(varString, "$1 ")
End If
Next i
Now I want to replace the static column so I can process only the user range.
In order to achieve that I tried this:
Set selection = Application.InputBox(Prompt:= _
"Please select a range to apply the remove break lines procedure.", _
Title:="Remove Line Breaks", Type:=8)
If selection Is Nothing Then
Exit Sub
End If
Set RowsNumber = selection.CurrentRegion -> This line gives me an error: "Object required"
Set RowsNumber = RowsNumber.Rows.Count
For i = 1 To RowsNumber
If Not IsEmpty(Cells(i, 5).Value) Then
varString = Sheets(ActiveSheet.Name).Cells(i, 5).Text
Sheets(ActiveSheet.Name).Cells(i, 5).Value = objRegExp.Replace(varString, "$1 ") 'Replace pattern found with regular expression in the same line
End If
Next i
How can I access the cells in the range returned by the InputBox?
I also tried changing RowsNumber with selection.Rows.Count but that way, although it doesn't gives an error, the cells used have blank string within them when I run the debugger. I think this is because I try to access row = 5 when the range could be less, i.e 3 if user just selects 3 cells.
I tried a For Each Next loop but then again, I know not how to access the cells withing the selection range.
You can iterate through the cells of a range by using For Each loop.
Below is your code modified. I have changed the name of variable Selection to rng, because Selection is Excel library built-in function and this name should be avoided.
Sub x()
Dim rng As Excel.Range
Dim cell As Excel.Range
Set rng = Application.InputBox(Prompt:= _
"Please select a range to apply the remove break lines procedure.", _
Title:="Remove Line Breaks", Type:=8)
If rng Is Nothing Then
Exit Sub
End If
For Each cell In rng.Cells
If Not IsEmpty(cell.Value) Then
varString = cell.Text
cell.Value = objRegExp.Replace(varString, "$1 ") 'Replace pattern found with regular expression in the same line
End If
Next cell
End Sub

Counting non blank cell results without looping in Excel VBA -- e.g., with Specialcells

Here is the code I'm trying to count with in VBA, hoping to return
a count return variable of "3" from 'FormulaResultCount'. Why can't I
count what is visibly returned by the formulas within each cell; from the grey box (see photo below)?
Sub countNonBlanks()
Worksheets("Sheet1").Select
Range("C:C").Select
FormulaResultCount = Selection.SpecialCells(xlCellTypeFormulas).Count
'SpecialCells options from MSFT
' xlCellTypeAllFormatConditions. Cells of any format -4172
' xlCellTypeAllValidation. Cells having validation criteria -4174
' xlCellTypeBlanks. Empty cells 4
' xlCellTypeComments. Cells containing notes -4144
' xlCellTypeConstants. Cells containing constants 2
' xlCellTypeFormulas. Cells containing formulas -4123
' xlCellTypeLastCell. The last cell in the used range 11
' xlCellTypeSameFormatConditions. Cells having the same format -4173
' xlCellTypeSameValidation. Cells having the same validation -4175
' xlCellTypeVisible. All visible cells
'
End Sub
See formula as reference:
Note: Since I will have many more cells when working dynamically, loops will likely slow the process down too much. Also, I tried using CountA without result.
Maybe this:
FormulaResultCount = WorksheetFunction.CountIf(Range("C:C"), "?*")
Thus counting all cells in range that start with any character?
xlCellTypeFormulas. Cells containing formulas -4123
This would not return the cell based on their values but if they have any formula or not. As per your worksheet, you should get 5
Also, PLEASE PLEASE do not use .Select INTERESTING READ
Your code can also be written as
FormulaResultCount = Worksheets("Sheet1").Columns(3).SpecialCells(xlCellTypeFormulas).Count
Another Tip: When using SpecialCells, use appropriate error handling so that if there are no cells which match the SpecialCells criteria, your code won't break. See this example.
Sub Sample()
Dim ws As Worksheet
Dim Rng As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
On Error Resume Next
Set Rng = .Columns(3).SpecialCells(xlCellTypeFormulas)
If Err <> 0 Then
MsgBox "No Cells with formulas were found"
Exit Sub
End If
On Error GoTo 0
End With
FormulaResultCount = Rng.Count
Debug.Print FormulaResultCount
End Sub
FOLLOWUP From Comments
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Debug.Print Evaluate("=COUNTA(C1:C" & lRow & _
")-COUNTIF(C1:C" & lRow & ","""")")
End With
End Sub
What you really might want is:
FormulaResultCount = Evaluate("CountA(C:C)")
I just learnt about the evaluate command. It's awesome!
And it gives you 3 :)
You can do this without VBA, using only formulas.
=ROWS(range)*COLUMNS(range)-COUNTBLANK(range)
If you're trying to do this in VBA, you can use this:
Function non_blank_cell_results_count(r As Range) As Long
non_blank_cell_results_count = r.Cells.Count - WorksheetFunction.CountBlank(r)
End Function

Substitute Function in Excel VBA for cell range

I have to replace one character with another in Excel file.
I have used following Replace function, but due to exceeds of 1024 character limit in some cells, it stops there.
Sub Replace()
With Sheets("Sheet1").Range("A1:A629")
.Cells.Replace ",", ";", xlPart, xlByRows, False
End With
End Sub
I got to know Substitute function would do
Cells(1, 2) = "=SUBSTITUTE(A1,"","","";"")"
But how do I use that for cell range?
Try this
Sub Replace()
Dim rng As Range, cell As Range
Set rng = Sheets("Sheet1").Range("A1:A629")
For Each cell In rng
cell = WorksheetFunction.Substitute(cell, ",", ";")
Next
End Sub
Try this. Note that it uses the VBA Replace function, so you need to rename your 'Replace` subroutine.
Sub ReplaceText()
For Each c In Sheets("Sheet1").Range("A1:A629").Cells
c = Replace(c.Value, ",", ";")
Next c
End Sub
Note: This will only work if you have values in the cells, no Formulas. Because Excel has a formula length limit of 1024 characters. But given you have this specific error, your cells must not be formulas.