Get Sum of Column H based on sheet name - excel

I am trying to get the sum of column H from each individual sheet. The name of the sheet is given in column A
The way I have been doing it is using =SUM('BSF10003'!H:H), dragging this formula down and changing the last couple digits manually but this is a long process if there are 100+ columns.
I am new to VBA and am hoping there is a way to do this task quicker
Thanks again!

The INDIRECT function is another way. Example below,
=SUM(INDIRECT("'"& A2 &"'!H:H"))
A2 = The sheet name
INDIRECT lets you make references dynamically.

One way could be a UDF
Paste this in a module
Function SumSheets(ByVal SheetName As String) as Double
SumSheets=Application.WorksheetFunction.Sum(Worksheets(SheetName).Range.Columns(8))
End Function
And use it like a standard formula...
=SumSheets(A1)
Will return the sum of column H in the worksheet named in A1
Or as a variation, you could use this along with the UDF. You'd paste it in the same module like the UDF but you don't need to type the formula. Instead, you select a cell in a column and run this macro.
The formulas are added automatically.
Sub FillRangeWithSumSheets()
Dim n As Name: Dim s As String: s = "SumSheetsRange"
With ActiveWorkbook
For Each n In .Names
If n.Name = s Then
n.Delete
Exit For
End If`
Next
Set n = .Names.Add(s, "=OFFSET($A1,0," & ActiveCell.Column - 1 & ",COUNTA(Sheet1!$A:$A),1)")
n.Visible = 1
Range(s).Formula = "=SumSheets(A1)"
End With
End Sub

Related

How to use VLOOKUP in Excel VBA to find matching cells

I am just starting out with Excel VBA and to be honest I am not that skilled in using normal Excel either.
I've got one sheet that has unique codes in column C, I also have another sheet that has unique codes in column A (except first rows as they've got column labels).
In case this unique code from sheet 1 is also found in the column in sheet 2, I want column J in the sheet1 to have that code value, otherwise, if it cannot be found, I want it to have #N/A.
Normally in Excel I would do this by selecting J2 and entering following function:
=VLOOKUP(C2,Sheet2!A:A,1,False)
Then I'd just double click the corner of the cell to populate all rows.
How do I do it in VBA? I wrote this code hoping it would do something:
Worksheets("Sheet1").Activate
ActiveSheet.Range("J:J").Value = Application.VLookup(C2,Worksheets("Sheet2").Range("A:A"),1,False)
However, this does not work. I just get #N/A for all cells in the J column. Any suggestions what I can do?
The following code will give you what you want. Note that you would only want to put the formula into rows where values actually exist in column C on sheet1.
Option Explicit
Sub InsertVlookup()
Dim LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Row
With Sheet1.Range("J2:J" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC[-7],Sheet2!C[-9],1,FALSE)"
.Value = .Value
End With
End Sub

Excel formula: To sum the same cell values from multiple sheets with names as dates

Is there a way to sum all the values of the same cell of multiple sheets (tab) which the name of each tab is date, and show the summed value in the MasterSheet?
For example:
Currently, I use this formula =SUM(INDIRECT("'*-"&MONTH(C2)&"-2020'!$E$3")) in the cell under the "Oct-2020" cell, as shown in the figure below, and I will do the same for Nov-20 and Dec-20.
As you can see, I am getting this "#REF!" error. Currently, I have two tabs that I am trying to get the "$E$3" cell values from, "13-10-2020" and "14-10-2020". However, if I only have one sheet (let's say "14-10-2020" only), I am able to get the value.
Does anyone know what is going on with my formula? Why it works when there is only one sheet (tab) to read from, but does not work when there are multiple sheets (tabs), even I used "*" to include all dates.
Please advice. Thank you in advanced
I think this should solve your problem:
Option Explicit
Sub sub_sum_up_months()
Dim wks As Worksheet
Dim nr_month As Integer, nr_year As Integer
Dim str_month As String, str_year As String
Dim c As Integer
Dim my_Sum As Double
For c = 3 To 5 ' Iterating over the columns "C" to "E"
my_Sum = 0
nr_month = month(Worksheets("MasterSheet").Cells(2, c).Value)
If nr_month < 10 Then
str_month = "0" & nr_month
Else
str_month = CStr(nr_month)
End If
nr_year = Year(Worksheets("MasterSheet").Cells(2, c).Value)
str_year = CStr(nr_year)
For Each wks In ThisWorkbook.Worksheets
If Right(wks.Name, 4) = str_year And Left(Right(wks.Name, 7), 2) = str_month Then
my_Sum = my_Sum + wks.Cells(3, 5) 'cells(3,5) is "E3"
End If
Next wks
Worksheets("MasterSheet").Cells(3, c).Value = my_Sum
Next c
End Sub
You can use the sum function.
Follow this step by step and it will work.
In your MasterSheet C3 type =Sum(
Click on sheet 13-10-2020 and cell E3.
Now press and hold shift.
Click on the last sheet that is October and press Enter.
Now you should get the sum of all E3 in October sheets.
As far as I know there is no formula to do this dynamic but I'm quite sure it can be done in VBA if the sheet names follow a pattern.

Find the sum of a cell from difference worksheets without referencing sheet names

I have a workbook with multiple sheets. All sheet names follow this scheme "Sensor Status", "Sensor Status(1)" and so on. Is there a way to get the SUM of I4 from all these sheets without having to reference each individual sheet name? I tried this however I am getting the ref error. "A5" has the value of "sensor" as I am attempting to use wild cards in the sheet reference of the formula.
=SUM(INDIRECT("'*"&A5&"*'!K4"))
You can do something like this for the Sensor Status(#)
=SUM(INDIRECT("'Sensor Status("&ROW(1:5)&")'!K4"))
This is an array formula an must be confirmed with Ctrl-Shift-Enter instead of Enter.
NOTE: May be able to use SUMPRODUCT instead of SUM to avoid the CSE entry.
But it requires an array, which the ROW(1:5) provides. If this is not ideal then vba or listing the sheets in a column and referring to that would be needed.
See this answer with more options: How can you sum the same cell across multiple worksheets by worksheet index number in excel?
You can do a hybrid of vba and formula.
In a module attached to the workbook put:
Function SHEETNAME() As Variant
Application.Volatile
Dim temp() As Variant
ReDim temp(1 To Application.Caller.Parent.Parent.Worksheets.Count) As Variant
Dim i As Long
For i = 1 To Application.Caller.Parent.Parent.Worksheets.Count
temp(i) = Worksheets(i).Name
Next
SHEETNAME = temp
End Function
This returns all the worksheet names as an array. Then use something like this:
=SUMPRODUCT((ISNUMBER(SEARCH(A5,SHEETNAME())))*SUMIF(INDIRECT("'"&SHEETNAME()&"'!K4"),">0"))
in VBA:
Function SumbyCell(rng As Range) As Double
Dim sum As Double
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
sum = sum + ws.Range(rng.Address).Value2
Next ws
SumbyCell = sum
End Function
The function will work without depending on the sheet name.
Call the function by type: = Sumbycell(I4)
with function:
Suppose you have 2 sheets of names respectively: sheet1, sheet2
Your function is:
= CONCAT (SUM (INDIRECT ("sheet" & ROW (1: 2) & "! I4"))))
type: ctrl + shift + enter

Use CELL on an array to return string made of types of the cells

Lets say we have 5000 rows with random values (blanks, numbers, characters). I need to show type of all the cells in these 5000 rows in a single cell using a formula. Is it actually possible? I've tried to CONCATENATE(CELL("type";array)) and ctrl+shift+enter but it didn't work (it returns the type of the first cell in the array).
If you want to know, this is for finding a cell with text rather than values or blanks in a very big file. Maybe you have a better solution.
Thanks in advance.
UPD: thanks for macros but I can't use them in this workbook, I need a formula-solution.
UPD: I've got how to do it with conditional formatting => new rule => use a formula to determine... => use ISTEXT('first cell of the range') formula
But still, is it possible to create the formula?
The best way to go about this is to use MACROs
Here is my sample code:
Sub Button2_Click()
numRows = 10 ' Number fo rows to loop through, in your case 5000
'loop through each cell located in column 1
'Check its type
'Concatenate each one in 1 cell on the 8th column
For i = 1 To numRows
Sheet1.Cells(1, 8).Value = Sheet1.Cells(1, 8).Value & TypeName(Sheet1.Cells(i, 1).Value) & ","
Next i
End Sub
You can adapt this small user defined function to your needs.
Say we are looking at cells in the first row, from A1 through D1 and we want to concatenate their types into a single cell. Enter the following UDF() in a standard module:
Public Function KonKaType(rIN As Range) As String
Dim r As Range
For Each r In rIN
s = "cell(""type""," & r.Address(0, 0) & ")"
KonKaType = KonKaType & Evaluate(s)
Next r
End Function
and then in the worksheet cell E1 enter:
=KonKaType(A1:D1)

If/Then/Replace formula

I know VBA is probably the way to go but I believe this can be done using a few basic formulas.
I need "E2" to be replaced (cut/copy) with the contents from "A3" but only if "D2" = "Status:Active"...and so on down the sheet
the yellow and blue color-coding are only for this example and do not represent the whole sheet
this is a 7,000 line spreadsheet that was a report generated off some old system and I'm trying by best to collate and format.
Try in Column F starting with cell F2
=if(AND(E2="",D2="Status: Active"), A2, E2)
This will test to see if D2 has "Status: Active" and if it does, it will pull the value form A2. If it isn't then it will use the address already in E2
As explained in your comments that you are looking for A to be blank when F accepts a value from it (Cut/Paste)... there is no way for a formula to affect another cell, but... You could add a new Column B inserted after A and put the following formula in there: =if(A2<>G2, "", A2). Then hide Column A. The new B column will then only show values of Column A when it's not already in Column G (formerly column F before the insertion of the new column).
You could also do all of this through VBA, but that seems like more effort than it's worth when some simple formulas will get you there.
Seeing as you want the column A to be blank you can try this macro out:
Option Explicit
Sub SwapCols()
Dim oWs As Worksheet
Dim lRowNum As Long
Dim i As Long
Set oWs = ActiveWorkbook.Worksheets("Sheet1")
lRowNum = oWs.Range("A2").End(xlDown).Row
For i = 2 To lRowNum
If oWs.Range("D" & CStr(i)).Value = "Status:ACTIVE" Then
oWs.Range("E" & CStr(i)).Value = oWs.Range("A" & CStr(i)).Value
oWs.Range("A" & CStr(i)).Value = Null
End If
Next i
End Sub
Make sure you replace "Sheet1" with the name of your sheet. the macro basically checks if a cell in column D has the value "Status:ACTIVE" and if it does it copies the corresponding cell in column A to column E.
Just make sure if you do run this and you do not like the results do not save.

Resources