vba help - Countif Formula not working

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am checking are there any string value "-" in a Columns. Using countif.
its not working, getting wrong number of argument or invalid property.


VBA Code:
With ws_Master
    lr_new = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      For i = 0 To UBound(Arys) '10 columns
    'Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, Arys(i)), .Cells(lr_new, Arys(i)), "" - ""))  'not working
    Count_Dash = "=COUNTIF(R5C" & Arys(i) & ":R" & lr_new & "C" & Arys(i) & ",""-"")"  ''not working
   Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, Arys(i)), .Cells(lr_new, Arys(i)), "" - ""))  ''not working
        If Count_Dash > 0 Then
            MsgBox "Found string in Amount columns " & Arys(i) & " columns", vbCritical
        End If
      Next i
End With


Thanks
mg

Thanks
mg
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, arys(i)), .Cells(lr_new, arys(i))), "" - "")  
Place of bracket?
 
Upvote 0
Hi Takae,

Thanks for your help, Now getting type mismatch for below line.

Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, Arys(i)), .Cells(lr_new, Arys(i))), "" - "")

VBA Code:
With ws_Master
    lr_new = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For i = LBound(Arys) To UBound(Arys)
      
    Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, Arys(i)), .Cells(lr_new, Arys(i))), "" - "")
        If Count_Dash > 0 Then
            MsgBox "Found string in Amount columns " & Arys(i) & " columns", vbCritical
        End If
      
      Next i
      
End With

Thanks
mg
 
Upvote 0
Hi Takae,

I am getting address correctely , $O$5:$O$14686, But still macro is showing type mismatch

but Countif not working.


Thanks
mg
 
Upvote 0
I think not ""-"" it should be "-"
Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, arys(i)), .Cells(lr_new, arys(i))), " - ")
 
Upvote 0
Hi Takae,

Thanks now macro not throwing error. if I provide string value "abc" it counts and return counts.
But if I use "-" it is not giving count" formatting issue i think.

Below is a sample data.
Book5
ABC
1Notional
25,317.59
3-
4########
57,013.27
6-
7-
81,592.95
91,225.89
10-
11-=COUNTIF($A$2:$A$11,"-")
12
132
Sheet1
Cell Formulas
RangeFormula
A13A13=COUNTIF($A$2:$A$11,"-")


Thanks
mg
 
Upvote 0
Hi takae,

All blanks kept as "-" using below format.
"_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"

User sometimes update Manually "-", I have to identify it. exit macro if found.



Thanks
mg
 
Upvote 0
I think
Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, arys(i)), .Cells(lr_new, arys(i))), "-")
is correct but your code is
Count_Dash = WorksheetFunction.CountIf(.Range(.Cells(5, arys(i)), .Cells(lr_new, arys(i))), " - ")
You are going to search "a space and - and a spece"
My code is going to search only "-".
 
Upvote 0
Hi Takae,

Perfect ! thanks a lot for your help, it worked finally. (y) ?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top