Excel VBA: Dynamically Changing Function Based on Column - excel

Heylo, I am trying to write an excel function that takes a user-selected range and performs different calculations based on the column the cell being populated lines up with. The screenshot below shows the setup of the columns.
I want to set AA5 to be "=myFunction($AA1:$AD4)", and then I want click-and-drag to use the autofill feature to populate AB5, AC5, and AD5 with the same "=myFunction($AA1:$AD4)" but this myFunction will do different things based on which cell is being populated during the autofill.
I know how to do this in a subroutine where the user would select the first open cell AA5, and is prompted for the range to use for calculations. I would do something along the lines of:
Sub CalcCells()
Dim myRange As Range
Set myRange = Application.InputBox("Select the cells you want to use...", Type:=8)
Dim numColumn As Long
For numColumn = 0 To myRange.Columns.Count - 1
Select Case numColumn
Case Is = 0
ActiveCell.Offset(0, numColumn).Formula = "=SUM(" + myRange.Columns(1) + ")"
Case Is = 1
ActiveCell.Offset(0, numColumn).Formula = "=SUMPRODUCT(" + myRange.Columns(1) + "," + myRange.Columns(2) + ")"
Case Is = 2
ActiveCell.Offset(0, numColumn).Formula = "=SUMPRODUCT(" + myRange.Columns(1) + "," + myRange.Columns(3) + ")/SUM(" + myRange.Columns(1) + ")"
Case Is = 3
ActievCell.Offset(0, numColumn).Formula = "=SUMSQ(" + myRange.Columns(4) + ")"
End Select
Next numColumn
End Sub
So basically I want to do exactly this, but I want it to be a function that when I click and drag and autofill AB5:AD5 it knows which column the cell lines up with and performs a calculation based on that, and uses it as an argument/parameter almost. It will not always be 4 rows either, so it needs to be capable of accommodating varying row counts, but the .Columns should work with that as long as the user selects only the same datatype.
Is this possible and how can I do it? Thank you for any help in advance. I've done a lot of searching and I don't know if I'm not searching the right way, but i cannot find anything that really helps.

What about something like this? Basically, you get the column of the cell you enter the formula into with Application.Caller.Column. Then inputRange.Column gives you the leftmost column of your input range. Based on the difference of the two, you know which worksheet function you want to use. If the difference is 0, your formula is entered in the 1st column, so you use Sum. If the difference is 1, you use Sumproduct, and so on.
Function SummarizeCells(inputRange As Range) As Double
Dim col As Long
col = Application.Caller.Column - inputRange.Column
Select Case col
Case 0
SummarizeCells = WorksheetFunction.Sum(inputRange.Columns(1))
Case 1
SummarizeCells = WorksheetFunction.SumProduct(inputRange.Columns(1), inputRange.Columns(2))
Case 2
SummarizeCells = WorksheetFunction.SumProduct(inputRange.Columns(1), inputRange.Columns(3)) / WorksheetFunction.Sum(inputRange.Columns(1))
Case 3
SummarizeCells = WorksheetFunction.SumSq(inputRange.Columns(4))
End Select
End Function
A sample view here:

Related

If column A equals a certain value, generate a value in column B. Check each cell in column A --> Generate a value in each cell in column B

I have a very simple question that I can't find the answer to.
Here is an example of what I'd like to do:
Sample Data
If a row in column A = 'Sandwiches', I would like column B to display 'It's a Sandwich'
If a row in column A = 'Wraps', I would like column B to display 'It's a Wrap'
etc. etc.
So far, I am able to do this for the first row. I'm having trouble creating a for loop to loop through all the available cells.
Here is what I have so far (was thinking of adding Else If statements for different values later):
Current If Statement
If you prefer the use of VBA code instead a formule as Ozgun Senyuva suggested. You can try this code:
Sub Food_and_thing()
Dim myLine As Double
Dim myFood As String
Set SavedData = Sheets("SavedData")
myLine = 2
With SavedData
Do While .Cells(myLine, 1) <> "" 'Assume that Category is in column A, and Generated Asset Name is in column B
myFood = " an other thing"
If .Cells(myLine, 1) = "Sandwiches" Then
myFood = " a Sandwich"
ElseIf .Cells(myLine, 1) = "Wraps" Then
myFood = " a Wrap"
ElseIf .Cells(myLine, 1) = "Salads" Then
myFood = " a salad"
End If
.Cells(myLine, 2) = "It's" & myFood
myLine = myLine + 1
Loop
End With
End Sub
Akash you do not need to write vba codes for this. You may use a lookup table and apply Vlookup formula as #BigBen said.
Apart from this, this formula can make you happy if you ignore some spelling errors. Enter this in cell B2 and paste along the way down.
="It's " & A2

Looping through a table and changing formula

I've got a relatively small table (2-5 rows / 7-9 columns) that I'm trying to loop through and calculate values using .formula. I'm having some issues figuring out how I refer to the absolute values of the column or row of the cell that I'm on. Here's what I've hard-coded so far:
For i = 1 To Range("GapTable").Rows.Count
For i2 = 2 To Range("GapTable").Columns.Count
Range("GapTable").Cells(i, i2).Formula = "=IFERROR(ROUND(AVERAGEIFS(prof_gap, series, $A21, grade, B$20),2), ""N/A"")"
Next i2
Next i
I'm trying to figure out how to change $A21 to lock the column in as an absolute, and B$20 to lock the row in, with the formula copying to the adjacent cells correctly (the same as if you took the cursor and filled in the values). The code works now, but obviously the values are all the same. Any suggestions how I address this issue, or a better way of doing this?
As written in my comment, you can paste over the range, where the dynamic references are to the upper left cell, e.g.,
with Range("GapTable")
.range(.cells(1,2),.cells(.Rows.Count,.Columns.Count)).Formula = "=IFERROR(ROUND(AVERAGEIFS(prof_gap, series, $A21, grade, B$20),2), ""N/A"")"
end with
If you have to loop, you can use counters, e.g.:
dim colnum as long, rownum as long
For i = 1 To Range("GapTable").Rows.Count
For i2 = 2 To Range("GapTable").Columns.Count
Range("GapTable").Cells(i, i2).Formula = "=IFERROR(ROUND(AVERAGEIFS(prof_gap, series, $A" & rownum & ", grade, " & cells(20,colnum).address & "),2), ""N/A"")"
colnum = colnum + 1
Next i2
rownum = rownum + 1
Next i
Note that you have to convert the column number to an address, which is why the cell() reference includes the 20.
I did not include the error handling for resetting rownum/colnum to reset back to 20/2, respectively. Just throwing out the concept if for some reason you can't just paste over the range.
Edit1: forgot the . in front of cells(1,2) in the withstatement for pasting over a range.

Excel VBA Sum from Multiple Sheets

I am trying to create a function or functions that can sum daily hours from time cards for each client to come up with the total hours worked per day. Each client has it's own sheet inside of a single workbook.
Currently, I have a function that determines the sheet that goes with the first client (the third sheet in the workbook):
Function FirstSheet()
Application.Volatile
FirstSheet = Sheets(3).Name
End Function
And one to find the last sheet:
Function LastSheet()
Application.Volatile
LastSheet = Sheets(Sheets.Count).Name
End Function
The part that I am having trouble with it getting these to work within the sum function.
=sum(FirstSheet():LastSheet()!A1
That is basically what I want to accomplish. I think the problem is that I don't know how to concatenate it without turning it into a string and it doesn't realize that it is sheet and cell references.
Any help would be greatly appreciated.
So, an example formula would look like this:
=SUM(Sheet2!A1:A5,Sheet3!A1:A5,Sheet4!A1:A5)
That would sum Sheet2-Sheet4, A1:A5 on all sheets.
Is there a reason you need to write the VBA code to do this?
Can't you just enter it as a formula once?
Also, if you're going to the trouble of writing VBA to generate a formula, it may make more sense to just do the sum entirely in VBA code.
If not, try this:
Sub GenerateTheFormula()
Dim x, Formula
Formula = "=SUM(" 'Formula begins with =SUM(
For x = 3 To Sheets.Count
Formula = Formula & Sheets(x).Name & "!A1," 'Add SheetName and Cell and Comma
Next x
Formula = Left(Formula, Len(Formula) - 1) & ")" 'Remove trailing comma and add parenthesis
Range("B1").Formula = Formula 'Where do you want to put this formula?
End Sub
Results:
The functions return strings and not actual worksheets. The Worksheet does not parse strings well. So add a third function that uses the Evaluate function:
Function MySum(rng As Range)
MySum = Application.Caller.Parent.Evaluate("SUM(" & FirstSheet & ":" & LastSheet & "!" & rng.Address & ")")
End Function
Then you would simply call it: MySum(A1)
It uses the other two function you already have created to create a string that can be evaluated as a formula.
I didn't understand ur question completely but As I understood u have different sheets of different clients which contains supoose column 1 date and column 2
contains hours on that particular date wise hours and a final sheet which column1 contains name of client and column 2 contains total hoursPlease try it
Sub countHours()
Dim last_Row As Integer
Dim sum As Double
sum = 0
'Because I know number of client
For i = 1 To 2 'i shows client particular sheet
last_Row = Range("A" & Rows.Count).End(xlUp).Row
Sheets(i).Activate
For j = 2 To last_Row
'In my Excel sheet column 1 contains dates and column 2 contains number of hours
sum = sum + Cells(j, 2)
'MsgBox sum
Next j
'Sheet 3 is my final sheet
ThisWorkbook.Sheets(3).Cells(i + 1, 2).Value = sum
sum = 0
Next i
End Sub
Happy Coding :

Create a two column list excluding 0 values

This seems extremely easy, but I just can't seem to get my head around it. So I have two columns. A has names and B has values. All I want to do is duplicate this list (into column D and E) just excluding 0 values in column B.(I put an example of what I am looking for below)
I am not that great at excel so this has been a bit of a challenge. I am trying to get it to work with a formula as I need it to automatically update when the values in column B change.
Any help would be greatly appreciated.
You could use a pivot table. Drag Goals to the rows, Values to the values field well. Then right-click any of the goals in the pivot table and select Filter > Value Filters, and filter on the value total to be not zero.
When more data is added to the sheet, just refresh the pivot table, which could be automated with VBA.
I believe I have a formula version that should work. Pull the first nonzero row over to D2:E2 (either manually or with simple index formulas) and then use the following in D3 and E3:
D3=IFERROR(INDEX(OFFSET($A$1,MATCH(D2&E2,$A$1:$A$99&$B$1:$B$99,0),0,99),MATCH(TRUE,(OFFSET($B$1,MATCH(D2&E2,$A$1:$A$99&$B$1:$B$99,0),0,99)<>0),0)),"")
E3=IFERROR(INDEX(OFFSET($B$1,MATCH(D2&E2,$A$1:$A$99&$B$1:$B$99,0),0,99),MATCH(TRUE,(OFFSET($B$1,MATCH(D2&E2,$A$1:$A$99&$B$1:$B$99,0),0,99)<>0),0)),"")
Then auto-fill to the bottom of your list.
Note that these are array formulas, so they must be entered using Ctrl+Shift+Enter. Also, if your list is longer than 99 then update all the 99's in the formula to a number at least as large as your list length.
The way this works is by using an OFFSET formula to start the search for non-zero values just below the previously listed result.
You could simply use if formulas to check the value like this...
'Column D formula:
=IF(E1="","",A1)
'Column E Formula:
=IF(B1=0,"",B1)
Although I doubt this is what you are looking for as it would just leave blank cells in column D and E if the value in column B is 0.
I would recommend using a macro that can be much more dynamic in nature.
Sub Check_Values()
Application.ScreenUpdating = False
Range("D:E").Select
Selection.ClearContents
Dim name As Integer, val As Integer
Dim name1 As Integer, val1 As Integer
Dim Emptyrw As Long, Emptyrw1 As Long, Emptyrw2 As Long
Dim i As Integer
name = 1
val = 1
name1 = 1
val1 = 1
Emptyrw = WorksheetFunction.CountA(Range("B:B")) + 1
Emptyrw1 = WorksheetFunction.CountA(Range("D:D")) + 1
Emptyrw2 = WorksheetFunction.CountA(Range("E:E")) + 1
i = 1
Do While val < Emptyrw
If Cells(val, 2) <> 0 Then
Range(Cells(name, 1), Cells(val, 2)).Select
Selection.Copy
Range(Cells(Emptyrw1, 4), Cells(Emptyrw2, 5)).Select
Selection.PasteSpecial
Application.CutCopyMode = False
End If
name = name + 1
val = val + 1
name1 = name1 + 1
val1 = val1 + 1
Emptyrw1 = Emptyrw1 + 1
Emptyrw2 = Emptyrw2 + 1
Loop
Do While i < Emptyrw1
If Cells(i, 4) = "" Then
Range(Cells(i, 4), Cells(i, 5)).Select
Selection.Delete Shift:=xlUp
End If
i = i + 1
Loop
End Sub
You can just copy this code into your VBA and assign a command button to it and should work for you.
Good Luck!
My effort...
Enter into D2 using Ctrl+Shift+Enter:
=INDEX(A:A,SMALL(IF($B$2:$B$20<>0,ROW($A$2:$A$20),999),ROWS(A$2:A2)),1)&""
Then fill down and across.
Make the "999" large enough to always be beyond the list of values.
Downside: appending the "" to prevent zeros showing up makes numbers behave as text.

Excel VBA - Loop through range and set formula in each cell

I've got a workbook where I have one worksheet which contains a lot of data.
My goal is to create a macro that inserts a formula in a separate sheet to copy the data from the first sheet. Lets call the first sheet "Numbers1" and the second sheet "TidyNumbers1".
In the sheet "TidyNumbers1" I want to loop through each cell from column A to M and rows 1 to 60. So I've got a macro that so far looks like this:
Sub updateFormulasForNamedRange()
Dim row, col, fieldCount As Integer
colCount = 13
RowCount = 60
For col = 1 To colCount
For row = 1 To RowCount
Dim strColCharacter
If col > 26 Then
strColCharacter = Chr(Int((row - 1) / 26) + 64) & Chr(((row - 1) Mod 26) + 65)
Else
strColCharacter = Chr(row + 64)
End If
Worksheets("TidyNumbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & col & "<>0;Numbers1!" & strColCharacter & row & ";"")"
Next row
Next col
End Sub
But the formula is supposed to looks like this for Column A, row 2:
IF(Numbers1!E2<>0;Numbers1!A2;"")"
And the formula in Column A, row 3 should look like this:
IF(Numbers1!E3<>0;Numbers1!A3;"")"
Formula in Column B, row 2 should look like this:
IF(Numbers1!E2<>0;Numbers1!B2;"")"
In other words, the formula looks to see if the value in Column E, row % is anything but 0 and copies it if conditions are met.
But, I see that I need to translate my integer variable Row with letters, because the formula probably needs "A" instead of 1. Also, I get a 1004 error (Application-defined or object-defined error) if I just try to use:
Worksheets("Numbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & row & "<>0;Numbers1!" & col & row & ";"")"
I clearly see that the integer row should be translated to letters, if that's possible. Or if anyone has any other suggestions that might work. Also, the 1004 error is unclear to me why happens. I can define a string variable and set the exact same value to it, and there's no error. So it's probably the formula bar that whines about it I guess?
Here is a former post of mine containing functions for conversion of column numbers to letters and vice versa:
VBA Finding the next column based on an input value
EDIT: to your 1004 error: Try something like this:
=IF(Numbers1!E" & row & "<>0,Numbers1!A" & row & ","""")"
(use ; instead of ,, and "" for one quotation mark in a basic string, """" for two quotation marks).
Would not it be easier to get the cell address with the Cells.Address function?
For example:
MsgBox Cells(1, 5).Address
Shows "$E$1"
Best Regards

Resources