Feb 16 2024 02:29 AM
Hi,
I'm not a programmer so turned to ChatGP for help. My first couple of queries returned macros that worked but then I refined the search to be more accurate and ran into an issue. This line is being rejected by Excel but I can't figure out why;
If WorksheetFunction.CountIf(ws.Cells(i, "B").Value, "*[a-zA-Z]*") = 0 Then
Excel returns an error 'Run-time error 424' Object required
The macro is designed to select any cells with numbers and move them from column B to D. The line above is to confirm if there are any letters contained and then ignore them. Variable i is the line number that increments.
Any help appreciated
Feb 16 2024 02:57 AM
Feb 16 2024 03:01 AM
Solution@Bakad01 The Run-time error is occurring because the first argument of COUNTIF is expecting a Range and you're passing it a value. Having said that, the formula will not work as expected anyways, because the criteria "*[a-zA-Z]*" is searching for the literal text string "[a-zA-Z]", NOT for each letter in the alphabet individually.
If the objective is to identify cells in column B that contain numbers only, try this instead:
If IsNumeric(ws.Cells(i, 2).Value) Then
Feb 18 2024 08:46 AM
Note that VBA's function IsNumeric will not always act as you might expect. As you can see here and in the attached workbook:
Those are as expected. However:
As you can see in columns F and G, the VBA alternative of using WorksheetFunction.IsNumber gives different results for Boolean values and empty cells.
Feb 19 2024 12:42 AM
Feb 16 2024 03:01 AM
Solution@Bakad01 The Run-time error is occurring because the first argument of COUNTIF is expecting a Range and you're passing it a value. Having said that, the formula will not work as expected anyways, because the criteria "*[a-zA-Z]*" is searching for the literal text string "[a-zA-Z]", NOT for each letter in the alphabet individually.
If the objective is to identify cells in column B that contain numbers only, try this instead:
If IsNumeric(ws.Cells(i, 2).Value) Then