Function for Adding Numbers in Multiple Cells - excel

https://i.stack.imgur.com/EXqon.png
I have tried to get results like the image, but I get these results using VBA, is there any way to use functions to get these results?
VBA code
Function SumNumbers(rng As Range) As Double
Dim a, e, m As Object
a = rng.Value
With CreateObject(“VBScript.RegExp”)
.Pattern = “\d+(\.\d+)?”
.Global = True
For Each e In a
If .test(e) Then
For Each m In .Execute(e)
SumNumbers = SumNumbers + Val(m.Value)
Next
End If
Next
End With
End Function
Please help me, so that I can get 61 results without using VBA ,thanks

If the numbers are always at the front of a string:
=SUMPRODUCT(--LEFT(B1:B4,SEARCH(" ",B1:B4)))

Here's how I solved your problem in my example.
I created a helper column. In fact, I used column C but it could be any column, preferably (but not necessarily) on the same sheet as your data.
I entered this formula in it's row 2 and copied down to as far as column B has data. Note that I trimmed the original text to ensure that there are no leading blanks that shouldn't be there.
=VALUE(LEFT(TRIM(B2),FIND(" ",TRIM(B2))))
I hid the helper column. That wouldn't be necessary if you have it on another sheet.
I entered =SUM(C2:C5) below the last row of the original column B.
Done.

Related

How to filter rows based on column value without using FILTER function in excel?

Hi all,
I want to use a formula to filter the rows based on the name chosen in cell D2. From what I searched in google, I only can see people using FILTER function which is very easy. However, FILTER function is only available if we subscribe to 365 office. May I know is there any way to achieve what I want for non 365 office user? Any help will be greatly appreciated!
As far as I understand, hiding the values different than D2 will take care of your need. I am using a similar macro for this task and below I modified it for you to hide the values different than D2. It will start checking values from active cell and loop through until it finds a null value. You can try it and modify it according to your needs. Then you can assign a keyboard shortcut or put a button for it into quick access toolbar, if you are going to use this frequently.
Sub hideByD2()
Dim i, j
i = ActiveCell.Row
j = ActiveCell.Column
k = Cells(2, 4).Value
Do Until Cells(i, j) = ""
If Cells(i, j) <> k Then
Rows(i).Select
Selection.EntireRow.Hidden = True
Else
End If
i = i + 1
Loop
MsgBox "hide process completed successfully"
End Sub
Manage to find the solution.
Formula:
G5 = =IFERROR(INDEX($C$5:$C$14,AGGREGATE(15,6,1/($C$5:$C$14=$D$2)*(ROW($C$5:$C$14)-ROW($C$4)),ROW()-ROW($C$4))),"")
H5 = =IFERROR(INDEX($D$5:$D$14,AGGREGATE(15,6,1/($C$5:$C$14=$D$2)*(ROW($C$5:$C$14)-ROW($C$4)),ROW()-ROW($C$4))),"")
I5 = =IFERROR(INDEX($E$5:$E$14,AGGREGATE(15,6,1/($C$5:$C$14=$D$2)*(ROW($C$5:$C$14)-ROW($C$4)),ROW()-ROW($C$4))),"")
Drag down these 3 formula to the cells below and should work.
Say you have this layout (just the first two columns of your data, moved to a1). Here are two formulas, one that contains FALSES (if you don't care) and one that removes them (because you probably do):
=IF(A4:A13=B1,B4:B13)
=IFERROR(SMALL(IF(A4:A13=B1,B4:B13), ROW(A4:A13)-3), "")
The first one is pretty straightforward. The second one is very similar. It just passes those results to SMALL, which will return the kth smallest value form the array ignoring FALSE values. To get it to evaluate the entire array, you also send it an array of 1 to n, generated with ROW(), and since the range starts in A4 you have to adjust by -3 to make the array start at 1. If you didn't want to have to figure out the offset, you could do this, but we're rapidly losing readability:
=IFERROR(SMALL(IF(A4:A13=B1,B4:B13), ROW(A4:A13)-MIN(ROW(A4:A13))+1), "")
When SMALL gets your list of matching values (with the falses), it will a match for each number in the ROW array you send it, and if it runs out of actual numbers it will start returning errors, which is why you wrap the whole thing in IFERROR.
This will work for numeric values. If you have to support any value, you can still do it:
=IFERROR(
INDEX(
B:B,
SMALL(
IF(A4:A13=B1, ROW(A4:A13)),
ROW(INDIRECT("1:"&ROWS(A4:A13)))
),
0
),
"")
In this case, instead of returning the matching values with SMALL, you will return the matching row numbers, then you will pass those to INDEX, wrapping the whole thing in IFERROR. I used a slightly different method to generate the dynamic indexes:
ROW(INDIRECT("1:"&ROWS(A4:A13)))
which will return an array from 1 to the number of rows in the passed range, but any of the methods to generate the sequence will work.

How to compare a string within a string list in Excel

There are some food names and prices as you can see between I2 and J22. For instance AYÇICEK YAĞI(SUNFLOWER OIL IN ENGLISH) is 4$ per kg. In the left of the sheet, you can see other list. What I need is;
I want to compare all A* columns with Strings between I2:I22 and get the price which is written between J2:J22 then write it to the D* columns.
There are more than 500 rows and I need to do it for all rows.
And there are some headings as u can see in bold font, they should be protected.
You seem to have come up with a formula; now you need a way to dispense it. Your worksheet design does not lend itself to simply filling down a formula. However, with the numbers in column C identifying valid entries that require a formula in columns D and E, a short sub procedure can make quick work of putting the formulas into the correct places.
Sub fillFormula()
Dim w As Long, vWSs As Variant, vFRMLs As Variant
vWSs = Array("ogle_aksam_gramaj", "kahvalt" & ChrW(305) & "_gramaj", _
"araogun_gramaj")
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
With .Columns(3) '<~~ going to look at column C for numbers
With .SpecialCells(xlCellTypeConstants, xlNumbers)
.Offset(0, 1).FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC1, 'urunler'!C1:C2, 2, FALSE), """")"
.Offset(0, 2).FormulaR1C1 = _
"=IFERROR(RC4*RC3, """")"
End With
End With
End With
Next w
End Sub
The IFERROR function has been used to 'wrap' the lookup and mulltiplication formulas. It catches errors and offers alternative results; in this case, zero-length strings that look blank.
The kahvaltı_gramaj worksheet causes problems in VBA due to the unicode character. You might try other methods of cycling through the worksheets.
      
That binary (macro-enabled) workbook is available from my own public dropbox here.
In the workbook you have attached, VLOOKUP will return #N/A when there is no available value.
In Sheet ogle_aksam_gramaj Cell D4 use the following Formula:
=SUMIF($I:$I,$A4,$J:$J)
You can then drag it down and it should be giving you the prices based on the details provided in the same sheet (Range I:J)
The good thing (or bad, depends on you) of sum if is that it will return 0 if there is nothing to sum. in your sheet, the items must be unique in the list, otherwise, it will keep summing every instance. So if AYÇICEK YAĞI is there 2 times, it will be summed twice.
You can use Advanced Filter with (unique values only) to make sure that all the ingredients are accounted for and are mentioned only once.
Thanks.

SumIf with Strings?

This may be a stupid question, and if it is, I apologise. I have a table in excel:
Column a...........Column b
1 property1.......problem x
2 property2.......problemy
3 property3.......problemz
4 property1......problem a
I was wondering if I could use sumif (or any similar formula) to add the problems, referring to a certain property, in one cell. for ex:
I would have
Column a...........Column b
1 property1.......problem x problem a
The problem is I can't figure out where to start. I tried using sumif but I get an error. Probably because I'm trying to add strings. I tried to mix a vlookup with sumif but that didn't produce anything too. Im stuck here. Thanks for any help!
I am not 100 % sure, but I think you might need to use VBA for this. You could try to create the following custom function:
Create named ranges properties and problems in your sheet.
Click ALT+F11 to open VBA editor.
Press Insert --> Module
Write code
'
Function ConcatIF(Property As String, PropertyRange As Range, ProblemRange As Range) As String
Dim counter As Integer
Dim result As String
result = ""
Dim row As Range
counter = 1
For Each row In PropertyRange.Rows
If (Property = row.Cells(1,1).Value) Then
result = result + ProblemRange.Cells(counter,1).Value + " "
End If
counter = counter +1
Next row
ConcatIF = result
End Function
As I do not have excel on the machine I am writing this on, I could only test it on another machine, and therefore there could be spelling mistakes in this code.
Ensure that you write the code in the module you created, it cannot be written in a Sheet's code, must be module.
This function can then be called as a regular function, like sum, average and if. Create a unique list of all your properties on another sheet. Properties in column A, and then in column B you can call the cuntion. Assume row 1 is used for headings, write the following and copy down.
=ConcatIF(A2,properties,problems)
NOTE!!!! This code gets out of hand very quickly. It needs to do (number of properties) x (number of property/problem pairs) comparisons, so if this number is huge, it could slow down your sheet.
There could be faster methods, but this was from the top of my head.

How can I get the length of the longest string in a column (Excel)?

I have an Excel spreadsheet that I am writing out to a file. I am currently using the following method to reproduce the contents of each cell:
cell_contents = Right(Space(10) & Trim(Cells(iRow, iColumn)), 10)
However, if the contents of the cell are longer than 10 characters long (or however long I choose to specify), then I loose parts of the data. Is there a way to quickly and easily get the length of the longest string in a range of cells?
I have seen people suggest using the following loop, but I was hoping that I might be able to do it without having to loop over all the cells:
For Each c In SourceRange.Cells
if len(c) > b then b = len(c)
Next c
Record these steps as a macro in the cell you want to calculate the max length.
1) enter this formula
=MAX(LEN(A1:A4), 1) -- Edit for your range.
2) press control-shift enter (FormulaArray)
3) now stop recording macro
4) view the VBA. copy what you need to your VBA.
Should give you something like this:
Selection.FormulaArray = "=MAX(LEN(R[-10]C[1]:R[-1]C[1]))"
Press Alt-F11, insert new module, paste code bellow.
Public Function maxRangeLength(data As Range) As Integer
Dim ret As Integer
ret = 0
For Each cell In data
ret = Application.Max(ret, Len(cell))
Next cell
maxRangeLength = ret
End Function
Usage:
=maxRangeLength(A8:D11)
Could just use ... {=MAX(LEN(A2:A200))}
In the top cell put =MAX(LEN(A2:A200)) .. and then press CTRL-SHIFT-ENTER to get the curly braces.
Suppose that the data is in column A and there is a heading row for your spreadsheet.
Create a new column next to it and enter in the formula: =LEN(A2)
Autofill down that formula.
Turn on filtering for that column and the click the drop-down box on the column heading cell.
Scroll down to the bottom of the list where the largest summarized value is.
That will be the largest value in the column.

Excel conditional formatting for the entire row with more than one formula

After 3 hours of searching I still didn't find an answer, here is what I am trying to do:
I am trying to fill with green any row that has WBS in it and with Red any row that has ACT in it and Blue any row that has EPR in it. It works for the first formula then when I try to add the second one every thing get messed up.
what i have understood is that you need to search a keyword in a row and if its found in any cell of that row then color it.
May be we can do it with conditional formatting but i have another idea. We can create a simple search function in Excel VBA. Something like this:
=search_row(A1:F1,"EPR")
The function will return 1 if EPR is found in any cell of specified row. Now if you create two different columns at the end of data columns, name first with WPS and second with EPR and write this function in it. Like
G1 =search_row(A1:F1,"WPS")
H1 =search_row(A1:F1,"EPR")
Drag it to end. Now sort the columns. First for WPS from higher to lower. Then color all rows having 1 in a single select. Similarly do the same with EPR (H1) column.
To use this function you can download the macro file from the following URL:
http://asimishaq.com/myfiles/SearchHighlight.xlsm
Now to run it first of all enable macros, and then re-open your data file and then open this macro file. As long as this macro file is opened you can use this function. Following is the VBA code if you want to create the macro yourself:
Function search_row(sRow As Range, Keyword As String)
Dim i As Integer
Dim Found As Integer
For i = 1 To sRow.Columns.Count
If InStr(1, LCase(sRow.Cells(1, i)), LCase(Keyword)) > 0 Then
search_row = 1
End If
Next
End Function
I had a go at making a function similar to asim-ishaq to determine if your search term exists in the row for fun :) then tried to apply it to highlighting rows, turns out I dont know how to use conditional formatting very well! Figured it out in the end, hopefully I've explained it well enough.
With this you will have to have (one) extra column at the end of your data to contain the result.
It might be possible to not require the extra column by putting the function inside the conditional formatting, however I couldn't get it to work (didn't try very hard). This isn't a great loss as it's much simpler to edit the formula if its on the workbook, instead of having to go through each conditional rule to edit it, should you need to edit it in the future.
To get the formatting to work you will need to create a number of rules (one per keyword)
You want to create a rule of the type shown below, in the formula box you need something along the lines of: =INDIRECT("D" & ROW())=0 where D is the column containing the result of the function below and 0 is the index of the keyword you're highlighting.
In my example, the formula in the D Column is: =SearchRow(An:Cn,"ABS","KBS","JBS") (where n is the row the formula is on)
Set the formatting as desired then press OK, when you return to the rule manager you will need to update the Applies to value, which should be a range that covers all the data you want to highlight. In my example it was $A$1:$C$3
My function below takes 2+ Arguments, The first is the range to search. The second (and any subsequent ones) are search terms.
The function will return a number. -1 for no matches and 0+ for the found search term. The number depends on the position in the arguments.
A1 = "ABS"
B1 = "SBA"
A2 = "SBA"
B2 = "ABS"
A3 = ""
B3 = ""
C1 = "=SearchRow(A1:B1, "ABS", "SBA")"
C2 = "=SearchRow(A2:B2, "ABS", "SBA")"
C3 = "=SearchRow(A3:B3, "ABS", "SBA")"
C1 > 0
C2 > 1
C3 > -1
The function will always return the first result, searching left to right comparing each cell to the Keywords in order. Using my example, if a cell contained "SBA ABS" the result would be 0 (for ABS). I guess your cells will probably only contain one keyword though so this shouldn't be a problem?
Public Function SearchRow(ByVal Row As Range, ParamArray Keyword() As Variant) As Integer
Dim Column As Integer
Dim Value As String
Dim Index As Integer
Dim Result As Integer
For Column = 1 To Row.Columns.Count
Value = LCase(Row.Cells(1, Column))
Result = -1
For Index = LBound(Keyword) To UBound(Keyword)
If InStr(1, Value, LCase(Keyword(Index))) > 0 Then
Result = Index
Exit For
End If
Next Index
If Result > -1 Then
Exit For
End If
Next Column
SearchRow = Result
End Function

Resources