Can anyone help
I have this formula in several cells, checking several ranges!
Is there a way for me to convert this to VBA so that i only have to select one range in each cell?
Thanks for the help.
=IF(COUNTIF(BE95:BE99;"Falhou")>0;"Falhou";IF(COUNTIF(BE95:BE99;"Falhou Condicionamente")>0;"Falhou Condicionamente";IF(COUNTIF(BE95:BE99;"Passou Condicionamente")>0;"Passou Condicionamente";IF(COUNTIF(BE95:BE99;"Passou")>0;"Passou"))))
Perhaps this is what you want, for it to work let's insert a module and put this code into this module.
Then just type following formulas:
= Test (BE95: BE99)
Function test(rng As Range)
Dim str As Variant
For Each str In Array("Falhou", "Falhou Condicionamente", "Passou Condicionamente", "Passou")
If Not IsError(Application.Match(str, rng, 0)) Then test = str:Exit for 'if match exist then return value
Next str
End Function
The function below has one feature your worksheet function doesn't have: It returns "Falhou" if the range contains none of the 4 count criteria.
Function Passou(Rng As Range) As String
' '=IF(COUNTIF(BE95:BE99;"Falhou")>0;"Falhou";
' IF(COUNTIF(BE95:BE99;"Falhou Condicionamente")>0;"Falhou Condicionamente";
' IF(COUNTIF(BE95:BE99;"Passou Condicionamente")>0;"Passou Condicionamente";
' IF(COUNTIF(BE95:BE99;"Passou")>0;"Passou"))))
Dim Sp() As String
Dim i As Integer
Sp = Split("Falhou,Falhou Condicionamente,Passou Condicionamente,Passou", ",")
For i = UBound(Sp) To 1 Step -1
If Application.CountIf(Rng, Sp(i)) Then Exit For
Next i
Passou = Sp(i)
End Function
Call the UDF from the worksheet specifying the range to be searched.
= Passou($BE$95:$BE$99)
Absolute addressing of the range enables copying of the formula across rows and columns.
Related
Is there a function to find from entries in a column that ends with ".5" using vba?
I have a live feed that I take from a html page, in values in column B are float numbers and I want to know if I can use a VBA function to find out how many values are ending with 0.5
Well without VBA:
=SUMPRODUCT(--(RIGHT(B1:B23,2)=".5"))
and with vba, then:
Sub dural()
MsgBox Evaluate("SUMPRODUCT(--(RIGHT(B1:B23,2)="".5""))")
End Sub
EDIT#1:
The worksheet formula treats column B like Strings. and counts how many in column B end with .5. This is expressed as an array of 0/1 by the expression within the --().
SUMPRODUCT() just adds up this array.
And with VBA, as a user-defined function (UDF):
Public Function CountThePointFive(ByRef theArea As Range) As Long
Dim count As Long
Dim cell As Variant
For Each cell In theArea
Dim value As String
value = CStr(cell.value)
Dim integerPart As Long
integerPart = CLng(Left$(CStr(value), Len(value) - InStr(1, value, ".")))
If (cell.value - integerPart) = 0.5 Then
count = count + 1
End If
Next cell
CountThePointFive = count
End Function
I am trying to get VBA code to count cells in a range that match by cell fill color and by the text within.
I can get one criteria at a time easy enough, but every time I try to combine the conditions I get VALUE error. Please help!
This is what I have:
G23-100 is the range
F19 is the color and text to match
.
Function CountColorValue()
Dim text As String
Dim cell As Range
For Each cell In Range("G23:G210")
If cell.Interior.ColorIndex = Range("F19").Interior.ColorIndex Then
If cell.text Like Range("F19").text Then
text = text + 1
End If
End If
Next cell
Cells(1, 2).Value = numbers
End Function
It doesn't work, hence I am here..
Thanks in advance for any help.
Your function is not a function in fact, but a routine because you have no out put. You have multiple mistakes that I fixed without trying, it should work, if it did, please mark it as the correct answer on the upper left
Function CountColorValue() as integer
Dim iColor as long, cnt as long
Dim text As String, str as string
Dim cell As Range
iColor=Range("F19").Interior.ColorIndex
str=Range("F19").value
For Each cell In Range("G23:G210")
If cell.Interior.ColorIndex = iColor and cell.text Like str Then
cnt=cnt+1
End If
Next cell
CountColorValue=cnt
End Function
Base on my test, please try the VBA code as below:
Option Explicit
Sub CountColorValue()
Dim text As Long
Dim cell As Range
For Each cell In Range("G23:G210")
If cell.Interior.ColorIndex = Range("F19").Interior.ColorIndex And cell.text Like Range("F19").text Then
text = text + 1
End If
Next cell
Range("A1").FormulaR1C1 = text
End Sub
Hopefully it helps you.
Copy and paste the code in a module.
Select the cell you want to have the results
Use this Function as the usally functions
The first Argument is the range you want to count, the second Argument is the cell with the interior.color you want to compare and the third Argument is the value or the cell you want to count the function in the range of first Argument. e.g =CountColorValue(G23:G210,$F$19,$F$19) or =CountColorValue(G23:G210,$F$19,"a")
Option Explicit
Function CountColorValue(Rng_dt As Range, criteria As Range, Val As Variant) As Double
Application.Volatile
Dim datar As Range
Dim xcolor As Long
xcolor = criteria.Interior.Color
For Each datar In Rng_dt
If datar.Interior.Color = xcolor And WorksheetFunction.CountIf(datar, Val) = 1 Then
CountColorValue = CountColorValue + 1
End If
Next datar
End Function
I'm struggling with a problem I thought would be easy to solve. Unfortunately I wasn't able to. I have a cell containing a text, actually an series of codes divided by ";". Furthermore I have a range of cells containing single codes. Now I want to count how many of the single codes are in the text.
Text-Cell: AA01;AA05;AB03;AB05;CD07
Range:
AA01
AB05
AB07
CD07
CD09
Result: 3
The range is dynamic, so the number of cells I'm looking at do differ. I'm looking for a code like:
Function NumberContaining(RANGE As Variable, TEXT as Variable) as Integer
Dim RESULT as Integer
Dim CELL as Variable
RESULT=0
For(CELL in RANGE)
RESULT=RESULT+WorksheetFunction.CountIf(Text,”=*” & CELL & “*”)
Next
NumberContaining=RESULT
End Function
Unfortunately I couldn't make it work. I would be glad if your could help me. Thanks.
If the separator for your strings is always going to be the same (;) you can use the Split function then loop through the array to see if any of the variants match any of the cells in your range.
Try this instead:
Function NumberContaining(rng As Range, txt As String) As Integer
Dim sArray() As String
Dim element As Variant
sArray = Split(txt, ";")
For Each element In sArray
If WorksheetFunction.CountIf(rng, Trim(element)) > 0 Then NumberContaining = NumberContaining + 1
Next element
End Function
Using your template... (you had some issues with your syntax). However, use Jordan's approach as this would flag e.g. AA01 as a match for AA011.
Function NumberContaining(rRANGE As RANGE, vTEXT As Variant) As Integer
Dim RESULT As Integer
Dim CELL As RANGE
For Each CELL In rRANGE
RESULT = RESULT + WorksheetFunction.CountIf(vTEXT, "=*" & CELL & "*")
Next
NumberContaining = RESULT
End Function
I need help on this. I'm doing a report and inserting comments on cells. How do I count cells with in a range in excel with specific value that has a comment using vba or formula?
Here is one way. It loops through each cell you pass in the range and checks if there is a comment. If so, it adds it to a counter. This is probably going to be pretty expensive if used on large range, but it should at least get you started:
Add to a regular module:
Function CommentCounter(rng As Range) As Integer
Dim cell As Range
Dim counter As Integer
Dim currentComment As String
For Each cell In rng
On Error Resume Next
currentComment = cell.Comment.Text
If Len(currentComment) > 0 Then counter = counter + 1
currentComment = ""
Next cell
CommentCounter = counter
End Function
Just saw the part about having a specific value AND a comment. This should get you going:
Function CommentCounter(rng As Range) As Integer
Dim cell As Range
Dim counter As Integer
Dim currentComment As String
Dim specificValue As String
specificValue = "Something Specific"
For Each cell In rng
On Error Resume Next
currentComment = cell.Comment.Text
If cell.Value = specificValue And Len(currentComment) > 0 Then counter = counter + 1
currentComment = ""
Next cell
CommentCounter = counter
End Function
=COUNTIF(A:A;"comment")
Where A:A specifies that you want to examine the whole column of A. Instead of A:A, you could also use A1:A3, which means examine A1, A2 and A3.
EDIT:
If you want to count the cells with comments (not with the word "comment"), I would suggest to do the following:
=COUNT(A1:A3) - COUNTBLANK(A1:A3)
I am trying to "COUNT" the number of a certain object in column I (in this instance) across multiple sheets. That value in column I is the result of a formula (if it matters). So far I have:
=COUNTIF('Page M904'!I:I,A13)+COUNTIF('Page M905'!I:I,A13)+COUNTIF('Page M906'!I:I,A13)
which works, but I am going to have 20 something pages to scan through. I would like to avoid having a page long formula.
I have tried
=COUNTIFS('Page M904:Page M906'!I:I,A13) and
=COUNTIF('Page M904:Page M906'!I:I,A13)
but that results in a #VALUE.
And I think
=COUNTIFS('Page M904'!I:I,A14,'Page M905'!I:I,A14,'Page M906'!I:I,A14)
is a misapplication of the COUNTIFS because I get 0 when it should be 35.
I am trying to avoid using VBA for this application. But if has to be, then it has to be :) Thanks in advance for your time and help.
This could be solved without VBA by the following technique.
In this example I am counting all the threes (3) in the range A:A of the sheets Page M904, Page M905 and Page M906.
List all the sheet names in a single continuous range like in the following example. Here listed in the range D3:D5.
Then by having the lookup value in cell B2, the result can be found in cell B4 by using the following formula:
=SUMPRODUCT(COUNTIF(INDIRECT("'"&D3:D5&"'!A:A"), B2))
I am trying to avoid using VBA. But if has to be, then it has to be:)
There is quite simple UDF for you:
Function myCountIf(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
myCountIf = myCountIf + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
Next ws
End Function
and call it like this: =myCountIf(I:I,A13)
P.S. if you'd like to exclude some sheets, you can add If statement:
Function myCountIf(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.name <> "Sheet1" And ws.name <> "Sheet2" Then
myCountIf = myCountIf + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
End If
Next ws
End Function
UPD:
I have four "reference" sheets that I need to exclude from being scanned/searched. They are currently the last four in the workbook
Function myCountIf(rng As Range, criteria) As Long
Dim i As Integer
For i = 1 To ThisWorkbook.Worksheets.Count - 4
myCountIf = myCountIf + WorksheetFunction.CountIf(ThisWorkbook.Worksheets(i).Range(rng.Address), criteria)
Next i
End Function
My first post...
UDF I managed quickly to compile.
Usage:
Select 3D range as normal and enclose is into quotation marks like below...
=CountIf3D("'StartSheet:EndSheet'!G16:G878";"Criteria")
Advisably sheets to be adjacent to avoid unanticipated results.
Public Function CountIf3D(SheetstoCount As String, CriteriaToUse As Variant)
Dim sStarSheet As String, sEndSheet As String, sAddress As String
Dim lColonPos As Long, lExclaPos As Long, cnt As Long
lColonPos = InStr(SheetstoCount, ":") 'Finding ':' separating sheets
lExclaPos = InStr(SheetstoCount, "!") 'Finding '!' separating address from the sheets
sStarSheet = Mid(SheetstoCount, 2, lColonPos - 2) 'Getting first sheet's name
sEndSheet = Mid(SheetstoCount, lColonPos + 1, lExclaPos - lColonPos - 2) 'Getting last sheet's name
sAddress = Mid(SheetstoCount, lExclaPos + 1, Len(SheetstoCount) - lExclaPos) 'Getting address
cnt = 0
For i = Sheets(sStarSheet).Index To Sheets(sEndSheet).Index
cnt = cnt + Application.CountIf(Sheets(i).Range(sAddress), CriteriaToUse)
Next
CountIf3D = cnt
End Function
I was looking to do the same thing, and I have a work around that seems to be less complicated using the Frequency and Index functions. I use this part of the function from averaging over multiple sheets while excluding the all the 0's.
=(FREQUENCY(Start:End!B1,-0.000001)+INDEX(FREQUENCY(Start:End!B1,0),2))