Using count, counta, countblank functions - vba

I've tried count, counta, and countblank functions for this code, but it doesn't work. My code is:
Sheet1.Activate
If WorksheetFunction.CountBlank(Range(Cells(3, 3), Cells(50, 3))) > 0 Then
MsgBox "First Enter Data!"
Else
...
I want excel to do some calculations if all of the cells in range C3 to C50 are containing a number, and return the msgbox if they aren't.
All the other codes are true. I've checked them several times.
The problem is that even when all of those cells have numbers, the msgbox appears. I've tried many ways, but it keeps going wrong.
Please help me. Thanks a lot.

Edit:
1) if your numbers stored as text, use following code (it change cells format to "number format"):
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
With rng
.NumberFormat = "0.00"
.Value = .Value
If WorksheetFunction.Count(.Cells) <> .Cells.Count Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
End With
2) You can also use this one:
Dim c As Range
Dim isAllNumbers As Boolean
isAllNumbers = True
For Each c In ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
If Not IsNumeric(c) Or c = "" Then
isAllNumbers = False
Exit For
End If
Next
If Not isAllNumbers Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
You may also want to read this: How to avoid using Select/Active statements

Related

VBA loop through range and output if complete range is empty

I have searched a lot about my question but could not find the answer I need.
I have a table A1:DT97138. Within this table I want to check per row, starting from cell B2 to DT2 if all the cells in one row are empty. Then output "Empty" or "Not Empty" in the next cell, DU2. Then do the same for row 3, 4 etc to 97138 (and output the same results row per row in DU2, DU3 etc).
I found out how to do this for 1 specific row, as you can see below, but I cannot find out how to iterate trough the whole range, row by row.
Sub rowEmpty()
Dim rng As Range, r As Range
Set rng = Range("B2:DT97138")
If WorksheetFunction.CountA(Range("B2:DT2")) = 0 Then
Cells(2, 125) = "Empty"
Else
Cells(2, 125) = "Not Empty"
End If
End Sub
Thanks for your help!
Your are doing well. Just need to loop thru the range like this.
Sub rowEmpty()
Dim rng As Range, r As Range
Set rng = Range("B2:DT97138")
For Each r In rng.Rows
If WorksheetFunction.CountA(r) = 0 Then
Cells(r.Row, 125) = "Empty"
Else
Cells(r.Row, 125) = "Not Empty"
End If
Next r
End Sub
Enter your formula at once in the last column:
With Range("DU2:DU97138")
.Formula = "=IF(COUNTA(B2:DT2)=0,""Empty"",""Not Empty"")"
'then eventually convert it to constants
.Value = .Value
End With
No loops, simpler, probably much faster :-)

Showing Type mismatch error on the line ".Caption = Workbooks("Nilesh Micro.xlsx").Sheets("Sheet1").Range("cell.Value" & rng).Value"

Workbooks("Nilesh Micro").Sheets("Sheet1").Activate
With ActiveSheet
Dim rng As Range, cell As Range
Set rng = Range("C1:C10")
If Not rng Is Nothing Then
For Each cell In rng.Cells
If cell.Value = "rng" Then
QuestionToMessageBox = "Do you want add New Opreator?"
YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "Message Box")
If YesOrNoAnswerToMessageBox = "6" Then
Workbooks("B2C_Tool.xlsm").Sheets("Dashboard").Activate
Range("M3").End(xlDown).Select
ActiveCell.Offset(1, 0).Select
With Sheets("Dashboard").CheckBoxes.Add(Selection.Left, Selection.Top, Selection.Width, Selection.Height)
.Caption = Workbooks("Nilesh Micro.xlsx").Sheets("Sheet1").Range("cell.Value" & rng).Value
End With
ElseIf YesOrNoAnswerToMessageBox = "7" Then
Else
End If
End If
Next
End Sub
The code is showing Type mismatch error on the line ".Caption = Workbooks("Nilesh Micro.xlsx").Sheets("Sheet1").Range("cell.Value" & rng).Value
". Please some one can help.
You're setting Set rng = Range("C1:C10")
and then expect to retrieve a String-variable - which you can use as .Caption - from
Workbooks("Nilesh Micro.xlsx").Sheets("Sheet1").Range("cell.Value" & rng).Value?
If I understand your question correctly you might check the following points in your code:
the Range()-method expects either (roughly)
a String like C1:C10
Objects like one Cell, two Cells (LeftTop and RightBottom cell), or a Range-object that consists of one cell
Now, with your code .Range("cell.Value" & rng) you mix up the String "cell.value" with your Range-object rng. Additionally, you typed "Cell.Value" (with double quotes) when you likely meant Cell.Value
Additionally: you can only retrieve Range.Value, if your Range-object consists of one Cell-object. However, in case of your code .Range("cell.Value" & rng).Value its 10 Cells (C1:C10)

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

Excel VBA - How do I refer to a cell's visible content rather than its formula?

I'm trying to loop through a particular range in my Excel spreadsheet(("B13:B65"), to be specific) and hide all rows that have an "X" in them. Something like this:
For i = 13 to 65
If Cells(i, 2) = "x" Or "X" Then Rows(i).RowHeight = 0
Next i
The problem is that I'm getting a type mismatch error.
I assume this is happening because all the cells in this range are formulas rather than text strings. For example, the contents of cell B13 are:
='Monthly'!$C$13
I want my code to evaluate the visible output of the cell, not the actual content.
I get the feeling there's a very easy solution here, but I've been searching for a while with no success. I'm a rookie, obviously...
Based on this example: https://msdn.microsoft.com/en-us/library/office/ff195193.aspx
Sub Main()
For Each c in Worksheets("Sheet1").Range("A1:D10") 'Change for your range
If Lcase(c.Value) = "x" Then
'''Rest of your code
End If
Next c
end sub
You're getting the error because of the OR. There has to be something that can be evaluated to true or false after the Or. Or "X" won't ever be true or false. You need...
If Cells(i, 2) = "x" Or Cells(i, 2) = "X" Then Rows(i).RowHeight = 0
As long as you wanted to use the same code everywhere else.
Use Value property:
If Cells(i, 2).Value = "x"
Loop through static range
Dim rng As Range, c As Range
Set rng = Range("B13:B65")
For Each c In rng.Cells
If UCase(c) = "X" Then
c.EntireRow.Hidden = True
End If
Next c
You could use an AutoFilter
Sub HideEm()
Dim rng1 As Range
Set rng1 = ActiveSheet.Range("$B$1:$B$65")
rng1.Parent.AutoFilterMode = False
rng1.AutoFilter Field:=1, Criteria1:="<>x", Operator:=xlOr, Criteria2:="<>X"
End Sub

Excel VBA: Can't get a match, error "Unable to get the Match property of the WorksheetFunction class"

For the love of all that is good, I cannot seem to get this to work. I keep getting the error mentioned above.
I have this table, and I'm trying to find out whether the code matches it's own sub-code somewhere within the other column, however it's erroring out. Your help is greatly appreciated.
Sub testing()
Dim m1 As long
Dim myrange As Range
Set myrange = Worksheets("Sheet1").Range("B2:B23")
For e = 2 To 23
m1= Application.WorksheetFunction.Match(Cells(e, 1).Value, myrange, 0)
If m1 > 0 Then
Cells(e, 3).Value = "Yes"
Else
Cells(e, 3).Value = "No"
End If
Next e
MsgBox "Complete!"
End Sub
Use the Application.Match function which allows for better ability to trap errors. When using the WorksheetFunction.Match, when a match is not found, it returns an error, which is what you're experiencing.
If Not IsError(Application.Match(Cells(e, 1).Value, myrange, 0)) Then
'Do stuff when the match is found
Cells(e, 3).Value = "Yes"
Else:
Cells(e, 3).Value = "No"
End If
You could also potentially use the CountIf function:
If Application.WorksheetFunction.CountIf(myRange, Cells(e,1).Value) > 0 Then
Cells(e,3).Value = "Yes"
Else:
Cells(e,3).Value = "No"
End If
Neither of these approaches requires you to use the m1 variable, you can assign this variable within the True part of the If/Then statement, if you need to identify where the match is found.
Just as another option, this can also be done by putting the formula below in cell C2, and dragging it down to C23.
=IF(COUNTIF($A$2:$A$23,B2)>=1,"YES","NO")