Is there a function for finding "ends with" - excel

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

Related

Count merged cells that have substring

I have some code by a former coworker that counts merged cells with a particular string. For example, if there was a merged cell with size 3 with the name "Youtube" on it, it would return 3.
This is the function in question:
Function MergedCellsCount(rRange As Range, crit As Variant) As Double
Application.Volatile
MergedCellsCount = 0 'in case there are no matches
For Each c In rRange
If LCase(c.Value) = LCase(crit) Then
MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
prev_rng = c.MergeArea.Address
End If
Next
' MergedCellsCount = MergedCellsCount / 5
End Function
But now, i want to count the cells that have a substring within that string.
If for example there was a merged cell with size 3 with "Youtube | Spotify", I want to know how to change that function to search for the substring "Youtube".
Any suggestions on how to achieve this?
Thanks in advance!
Count Cells Containing a String (Merged Cells) (UDF)
Note that if you merge or unmerge cells within the Criteria Range, the cells count will not be updated until the next recalculation of the worksheet.
To trigger recalculation, a nice 'trick' is to autofit a column by double clicking the right border of its column header (e.g. for column A, the short line between A and B).
The Code
Option Explicit
Function MergedCellsCount(CriteriaRange As Range, _
Criteria As String) _
As Long
Application.Volatile
Dim c As Range
For Each c In CriteriaRange.Cells
If Not IsError(c) Then
If InStr(1, c.Value, Criteria, vbTextCompare) > 0 Then
MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
End If
End If
Next
End Function

vba code that is in several cells and checks several ranges

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.

VBA to count cells matching color and specific text

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

How do I count cells with in a range in excel with specific value that has a comment using vba or formula?

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)

Excel MAXIF function or emulation?

I have a moderately sized dataset in excel from which I wish to extract the maximum value of the values in Column B, but those that correspond only to cells in Column A that satisfy certain criteria.
The desired functionality is similar to that of SUMIF or COUNTIF, but neither of those return data that is necessary. There isn't a MAXIF function; how do I emulate one?
You can use an array formula.In the cell in which you want the max calculated enter: =Max(If([test],[if true],[if false]) where you replace the values in square brackets with the test, what to return if true and what to return if false. For example:
=MAX(IF(MOD(A2:A25,2)=0,A2:A25,0)
In this formula I return the value in column A if the value divided by 2 has no remainder. Notice that I use a range of cells in my comparison and in the value if false rather than a single cell.
Now, while still editing the cell, hit Ctrl+Shift+Enter (hold down the Ctrl key and the Shift together and then hit enter).
This creates an array formula that acts on each value in the range.
EDIT BTW, did you want to do this programmatically or manually? If programmatically, then what environment are you using? VBA? C#?
EDIT If via VBA, you need to use the FormulaArray property and R1C1 references like so:
Range("A1").Select
Selection.FormulaArray = "=MAX(IF(MOD(R[1]C:R[24]C,2)=0,R[1]C:R[24]C,0))"
Array formulas don't work very well when you want to use dynamic or named ranges (e.g., "the maximum amount due for rows above the current row that have the same counterparty as the current row). If you don't want to use an array formula, you can always resort to VBA to do something like this:
Function maxIfs(maxRange As Range, criteriaRange As Range, criterion As Variant) As Variant
maxIfs = Empty
For i = 1 To maxRange.Cells.Count
If criteriaRange.Cells(i).Value = criterion Then
If maxIfs = Empty Then
maxIfs = maxRange.Cells(i).Value
Else
maxIfs = Application.WorksheetFunction.Max(maxIfs, maxRange.Cells(i).Value)
End If
End If
Next
End Function
A limitation with the code provided thus far is that you are restricted to 2 conditions. I decided to take this code further to not restrict the number of conditions for the MaxIfs function. Please see the code here:
Function MaxIfs(MaxRange As Range, ParamArray Criteria() As Variant) As Variant
Dim n As Long
Dim i As Long
Dim c As Long
Dim f As Boolean
Dim w() As Long
Dim k As Long
Dim z As Variant
'Error if less than 1 criteria
On Error GoTo ErrHandler
n = UBound(Criteria)
If n < 1 Then
'too few criteria
GoTo ErrHandler
End If
'Define k
k = 0
'Loop through cells of max range
For i = 1 To MaxRange.Count
'Start by assuming there is a match
f = True
'Loop through conditions
For c = 0 To n - 1 Step 2
'Does cell in criteria range match condition?
If Criteria(c).Cells(i).Value <> Criteria(c + 1) Then
f = False
End If
Next c
'Define z
z = MaxRange
'Were all criteria satisfied?
If f Then
k = k + 1
ReDim Preserve w(k)
w(k) = z(i, 1)
End If
Next i
MaxIfs = Application.Max(w)
Exit Function
ErrHandler:
MaxIfs = CVErr(xlErrValue)
End Function
This code allows 1 to multiple conditions.
This code was developed with reference to multiple code posted by Hans V over at Eileen's Lounge.
Happy coding
Diedrich

Resources