I'm very new to Excel VBA and I'm trying to getting figure out certain things. I want to populate cells in a range K26:K386 with a value that comes from cell N47 (call it "Income)". The cell to be populated (among those rows in K26:386) is determined by a number in cell N46 (call it "Month"). "Income" can be any figure. "Month" can be any figure from 1 to 360.
I want to plug in two inputs into cells N47 and N46 - income and the respective month - such that the respective cell in range K26:K386 is populated with the "Income" value for the respective month from 1 to 360. Can somebody please advise?
I only know how to populate a single cell like this:
Sub Ievietot()
Dim Sum As Integer
Sum = Range("N47").Value
Range("K30").Value = Sum
End Sub
you could use
Range("K26:K386").Cells(Range("N46").Value) = Range("N47").Value
I think that this can be done as follows:
Sub CopyData()
Dim iMonthNum as Integer
Dim income as Integer
iMonthNum = Range("N46").Value
income = Range("N47").Value
Range("K" & (25 + iMonthNum)).Value = income
End Sub
Related
Hi'm new to VBA and i'm trying to work on a QA report(call audit) that will automatically calculate values based on the data of another worksheet. I have 2 Sheets: Elements which contains the data, and AutoMacro which shows the number of cells that meets a specific criteria.
The Elements sheet has columns named Element and Rate, and WeekNumber. I made a code that will list all distinct week numbers to the "AM" column of the Automacro Sheet. Now, i want to count how many calls got a rate of 'Developing'(which is in column Rate of the Elements sheet) in Compassion(which is in column Element of the Elements sheet) on a specific week(AM column in the AutoMacro Sheet)
this code works fine:
CompDevCount = Application.WorksheetFunction.CountIfs( _
Sheets("Elements").Range("f2:F1048576"), "Compassion", _
Sheets("Elements").Range("G2:G1048576"),"Developing", _
Sheets("Elements").Range("C2:C1048576"), "4")
however, instead of putting a fixed value in the last criterion which is the weeknumber, i want my code to get the value of the corresponding cell in column AM(if macro is calculating values for row2 in the AutoMacro sheet, it should change the "4" to whatever value AM2 has) and i cant figure out how to do it.
any help is greatly appreciated
here's a link to my sample file:
https://drive.google.com/file/d/1ZXuMsQv_mzvycPXnGCGY7bUL6-gWBrBr/view?usp=sharing
Try the code bellow:
Sub LetsCount()
Dim lastElementsRowIndex As Long
Dim wsElements As Worksheet
Dim wsAutoMacro As Worksheet
Dim elementRange As Range
Dim rateRange As Range
Dim weekRange As Range
Dim i As Long
Dim weekNumber As Integer
Set wsElements = Sheets("Elements")
Set wsAutoMacro = Sheets("AutoMacro")
'//Get the index of the last filled row based on column A (Analyst)
lastElementsRowIndex = wsElements.Cells(Rows.Count, "A").End(xlUp).Row
'//Get ranges
Set elementRange = wsElements.Range("F2:F" & lastElementsRowIndex)
Set rateRange = wsElements.Range("G2:G" & lastElementsRowIndex)
Set weekRange = wsElements.Range("C2:C" & lastElementsRowIndex)
'//Looping through all filled rows in the AutoMacro sheet
For i = 2 To wsAutoMacro.Cells(Rows.Count, "A").End(xlUp).Row
'//Get week number from cell in column "AM"
weekNumber = wsAutoMacro.Cells(i, "AM").Value
'//Get the number of calls with: Rate = 'Developing', Element = 'Compassion' and Week Number = weekNumber
CompDevCount = Application.WorksheetFunction.CountIfs( _
elementRange, "Compassion", _
rateRange, "Developing", _
weekRange, weekNumber)
'//Insert the value in the corresponding cell of the AN column (column that I adopted to put the value)
wsAutoMacro.Cells(i, "AN").Value = CompDevCount
Next
End Sub
Say I have an Excel sheet with 10,000 rows and two columns. All 20,000 cells are filled with numbers and there is no missing data. I want to have a third column, the values of which are the sum of Column A and Column B. For example, C70 = A70 + B70, and C82 = A82 + 82, and the like.
The only problem is I want to do it for only a portion of the rows, say from row 125 to row 8954. I don't care about the rest of the values. And I don't want to do it by dragging the grid using the mouse. Is that possible?
If you have access to SEQUENCE() (Currently only available to Office 365 Insiders) then yes it is possible:
=INDEX(A:A,SEQUENCE(1000,,ROW(),1))+INDEX(B:B,SEQUENCE(1000,,ROW(),1))
Where 1000 is the number of rows desired. Place the formula in the first cell desired and it will automatically fill the rest.
I believe you need some logic about what is going on, related to the start and end row.
You can use an if-statement or sumifs() for this... will do an if-statement so i can specify not meeting the requirements as null.
With Start row as 2 and end row as 4 (see image), you can use this formula, and drag it down to the bottom of all columns:
=IF(AND(ROW(A2)<=F$2,ROW(A2)>=E$2),SUM(A2:B2),"")
Notice in the image that C5 has no value; this is due to the conditions of the if-statement being false.
Another idea, a simple macro that will do what you want by asking the user what the starting and end row is.
Sub test()
Dim startrow As Integer 'variable to hold first row
Dim endrow As Integer 'variable to hold last row
startrow = InputBox("Enter the start row")
endrow = InputBox("Enter the end row")
'loops through you desired range calculating what you want
Dim i As Integer
For i = startrow To endrow
Cells(i, 4).Value = Cells(i, 1).Value + Cells(i, 2).Value
Next
End Sub
Just change the values to suit your needs in what cells you want to add up and where you want the sum to go.
I am trying to write a excel formula that will calculate a data set based off of the column header for an area. Essentially, I am using it to help automate some of the mundane reconciliations that I do.
The data: I am wanting to search the column header for "Car_ERN" and sum that entire column. My formula I have been trying to get work is in two versions.
One version is using sumifs:
SUMIFS('Sum Payroll Dept Dump'!$Y$2:$CG$85,'Sum Payroll Dept Dump'!$Y$1:$CG$1,"ERN_CAR_AMT")
The other version is incorporating Sumproduct but I get a zero return. The "2" is searching a column that has multiple 2s in it because I want all rows for this criteria.
`SUMPRODUCT('Sum Payroll Dept Dump'!$Y$2:$CG$200,('Sum Payroll Dept Dump'!$Y$1:$CG$1='Filter Sheet'!A8)*('Sum Payroll Dept Dump'!A2:A200="2"))`
If there is a better way to do it without macros that would be much appreciated.
Thanks for the help!
Formula only solution, assuming your desired column header to look for is in sheet 'Filter Sheet' cell A8 (using your example, that cell would contain "ERN_CAR_AMT"):
=SUMIF('Sum Payroll Dept Dump'!$A:$A,2,INDEX('Sum Payroll Dept Dump'!$Y:$CG,0,MATCH('Filter Sheet'!A8,'Sum Payroll Dept Dump'!$Y$1:$CG$1,0)))
You could make a user-defined function (UDF) in VBA.
function sumByHeader(header as string, headerRng as range) as Double
Dim hdr as Range
Dim i as Long
Dim thisSum as Double
for each hdr in headerRng
if hdr.value = header then
thisSum = 0
for i=2 to ActiveSheet.UsedRange.Rows.Count
thisSum = thisSum + cells(i, hdr.Column)
next i
end if
next i
sumByHeader = thisSum
End Function
This function loops through your header row (headerRng) and looks for a particular header (header). When it finds it, it sums ever cell under it in the column it is in, starting from row 2 and going to the end of the last cell of your used range.
Then in a cell in your worksheet, you can use the function like you would any other
=sumByHeader("Car_ERN", A1:Z1)
Currently I have a medical spread-sheet with a list of clients that we have serviced. We have 8 different clinical categories which are denoted by different acronyms - HV,SV,CV,WV,CC,OV,TS and GS.
A client can receive multiple therapies i.e. HV,SV,CV - in the background we have a counter mechanism which would increment each of these records by 1.The formula used for this counter is:
=(LEN('Parent Sheet'!F25)-LEN(SUBSTITUTE('Parent Sheet'!F25,'Parent Sheet'!$P$4,"")))/LEN('Parent Sheet'!$P$4)
At the bottom of the sheet we then have a sum which ads up all the treatments that occurred for that week.
Now the tricky part about this is that we have almost a year's worth of data in this sheet but the summing formulas are set as: SUM(COLUMN 6: COLUMN 53) but due to a need to increase the entries beyond this limit, we have to adjust the sum formula. We have 300 SUM Formulas adding up each of the 8 Criteria items and assigning them to the HV,SV,SC,WV etc. counters.
Would we have to adjust this manually one by one or is there a easier way of doing this?
Thank you very much!
To me, I think you should change the sheet layout a little, create a User Defined Function (UDF) and alter the formulas in your Sum rows for efficient row/column adding (to make use of Excel's formula fill). The only issue is that you need to save this as a Macro-Enabled file.
What you need to change in the formulas is to utilize $ to restrict changes in column and rows when the formula fill takes place.
To illustrate in an example, consider:
Assuming the first data starts at row 6, and no more than row 15 (you can use the idea of another data gap on the top). Alter the Sum row titles to begin with the abbreviation then create a UDF like below:
Option Explicit
' The oRngType refers to a cell where the abbreviation is stored
' The oRngCount refers to cells that the abbreviation is to be counted
' Say "HV" is stored in $C16, and the cells to count for HV is D$6:D$15,
' then the sum of HV for that date (D16) is calculated by formula
' `=CountType($C16, D$6:D$15)`
Function CountType(ByRef oRngType As Range, ByRef oRngCount) As Long
Dim oRngVal As Variant, oVal As Variant, oTmp As Variant, sLookFor As String, count As Long
sLookFor = Left(oRngType.Value, 2)
oRngVal = oRngCount.Value ' Load all the values onto memory
count = 0
For Each oVal In oRngVal
If Not IsEmpty(oVal) Then
For Each oTmp In Split(oVal, ",")
If InStr(1, oTmp, sLookFor, vbTextCompare) > 0 Then count = count + 1
Next
End If
Next
CountType = count
End Function
Formulas in the sheet:
Columns to sum are fixed to rows 6 to 15 and Type to lookup is fixed to Column C
D16 | =CountType($C16,D$6:D$15)
D17 | =CountType($C17,D$6:D$15)
...
E16 | =CountType($C16,E$6:E$15)
E17 | =CountType($C17,E$6:E$15)
The way I created the UDF is to lookup and count appearances of a cell value (first argument) within a range of cells (second argument). So you can use it to count a type of treatment for a big range of cells (column G).
Now if you add many columns after F, you just need to use the AutoFill and the appropriate rows and columns will be there.
You can also create another VBA Sub to add rows and columns and formulas for you, but that's a different question.
It's isn't a great idea to have 300 sum formulas.
Name your data range and include that inside the SUM formula. So each time the NAMED data range expands, the sum gets calculated based on that. Here's how to create a dynamic named rnage.
Sorry I just saw your comment. Following is a simple/crude VBA snippet.
Range("B3:F12") is rangeValue; Range("C18") is rngTotal.
Option Explicit
Sub SumAll()
Dim WS As Worksheet
Dim rngSum As Range
Dim rngData As Range
Dim rowCount As Integer
Dim colCount As Integer
Dim i As Integer
Dim varSum As Variant
'assuming that your said mechanism increases the data range by 1 row
Set WS = ThisWorkbook.Sheets("Sheet2")
Set rngData = WS.Range("valueRange")
Set rngSum = WS.Range("rngTotal")
colCount = rngData.Columns.Count
'to take the newly added row (by your internal mechanism) into consideration
rowCount = rngData.Rows.Count + 1
ReDim varSum(0 To colCount)
For i = 0 To UBound(varSum, 1)
varSum(i) = Application.Sum(rngData.Resize(rowCount, 1).Offset(, i))
Next i
'transpose variant array with totals to sheet range
rngSum.Resize(colCount, 1).Value = Application.Transpose(varSum)
'release objects in the memory
Set rngSum = Nothing
Set rngData = Nothing
Set WS = Nothing
Set varSum = Nothing
End Sub
Screen:
You can use named ranges as suggested by bonCodigo or you could use find and replace or you can insert the columns within the data range and Excel will update the formula for you automatically.
Apologies beforehand for this, I'm very new to VBA and I'm sure this is simple coding that I'm struggling with.
Based on a user defined number of months (just entered into the ssheet) I have a row of column headings giving the month number and the heading "Total" after the last month.
i.e. User says 12 months, cell A2 = "Jan-15" and M2 = "Total".
Underneath the headings the user then adds some sales data.
What I need to do is use VBA to add a total to the end of each of the rows of data where the header = "total".
I was thinking an If Then makes sense but I'm struggling with how to get the macro to put the formula in the correct cell and then have the formula look at the variable range. This is the best I've come up with so far (please don't laugh!)
Sub Add_total()
Dim criteria As String
criteria = Range("A:A").Value
If criteria = "Total" Then
Range("12:12").Select
ActiveCell.Formula = "=sum($c12:c12)"
End If
End Sub
What I'd like is, for example, where M2 = "Total", then C12 = sum(c1:L12)
I realise the easiest way would be to just get the user to add the total themselves or have a total column far away to the left of the sheet but this is going across the business and needs to restrict manual input/guarantee accuracy of the calcs.
Any help much appreciated.
Why not simply put in cell M3 the function =sum(A3:L3) and copy downwards?
If you need it in VBA anyway should this work:
Sub testsum()
i = 3 'start in cell3
While Not IsEmpty(Cells(i, 1))
Cells(i, 13) = WorksheetFunction.Sum(Range((Cells(i, 1)), (Cells(i, 12))))
i = i + 1
Wend
End Sub
Thanks very much for that - not quite what I needed but you pointed me in the direction. Went for the following in the end:
Sub Total() Set Find_total = Range("6:6").Find("Total",_
LookIn:=xlValues,searchorder:=xlByColumns)
Set Revenue_total = Find_total.Offset(rowoffset:=3, columnoffset:=-1)
Set Revenue_cell = Find_total.Offset(rowoffset:=3, columnoffset:=0)
Revenue_cell = WorksheetFunction.Sum(Range((Cells(9, 2)), Revenue_total))
End Sub
Thanks again