Count and Sum if column name is exact match - excel

I am working with a large dataset (500+ columns X 10,000 rows). I am trying to get a count and sum of all variables within a row. All column names are listed on a separate worksheet/table. Whats the best way to go about it? Thanks
Main Data: Raw Data
Summary: Where I am trying to get the count and sum

Formula:
One way with ms365 could be:
Formula in G3:
=LET(z,B2:E7,WRAPROWS(TOCOL(VSTACK(TAKE(z,1),BYCOL(DROP(z,1),LAMBDA(x,COUNT(x))),BYCOL(DROP(z,1),LAMBDA(y,SUM(y)))),,1),3))
It can be less verbose, but this way you'd just need to select B2:E7 once, on whatever worksheet it is located.
PowerQuery:
Another way would be PowerQuery, where you'd:
Select (a single cell or ) the whole range B2:E7;
On the 'Data' tab, click 'From Table/Range' in the 'Get & Transform Data' group;
Choose to include headers, and hit 'OK'. PQ launches;
Select all column and in the 'Transform' tab, click 'Unpivot Columns' under the 'Any Column' group;
Select the 1st column, 'Attribute' in this case, and on the same tab 'Transform' choose to 'Group By' under the 'Table' group;
Hit the advanced option and create two aggregations before hitting 'OK':
Close and load data back to Excel.

If you Summary (Trying) column name is not fixed ... something like this :
So, the Sheet1 is data with 4 columns header, but in Sheet2, the "demand" is only 3 columns and not in sequence. (and later on, maybe the "demand" change to only 2 columns, later on the "demand" to all 4 columns, etc and also the header name always change).
The expected result in Sheet2:
So, if the cell has value, even if it's a zero value in the data column A then it is counted. Only a blank cell is not counted.
Sub test()
Dim rgSrc As Range: Dim rgTrg As Range: Dim c As Range
Set rgSrc = Sheets("Sheet1").UsedRange 'change as needed
With Sheets("Sheet2")
Set rgTrg = .Range("A2", .Range("A2").End(xlDown)) 'change as needed
End With
For Each cell In rgTrg
Set c = rgSrc.Rows(1).Find(cell.Value, lookat:=xlWhole)
If Not c Is Nothing Then
With rgSrc.Columns(c.Column)
cell.Offset(0, 1).Value = Application.CountA(.Cells) - 1
cell.Offset(0, 2).Value = Application.Sum(.Cells)
End With
End If
Next
End Sub
rgSrc is the range of the data in sheet1.
rgTrg is the range from cell A2 to whatever lastrow in sheet2. The code assumed that there'll be no blank cell in between data in column A of sheet2.
It loop to each cell in rgTrg (loop to each demanded header name), then put the count of the data in rgSrc with the looped cell value (which is the header name) in cell.offset(0,1) and put the sum of it in cell.offset(0,2).

Related

How to fill blank cells in Excel with the date of 30/06/YEAR by picking the year from the cell in the next column

Consider the following table:
I have a series of blank cells with missing data. From this missing data I only have the year in the next column. I need to fill any blank cells with a standard day/month of 30/06. The year of each cell however needs to be the year in the next column. The attached file shows how my data is arranged. So at cell B 2091, the date shall be 30/06/2011 while for cell B 2098 the date shall be 30/06/2018 and at cell B 2100 the date shall be 30/06/2008.
Filter on the blank cells in column B. Then, in the topmost cell (which I'll assume to be B1 but will likely be different), enter a formula similar to the following and fill down
=DATE(C1,6,30)
where the row number in C1 is the same as your first row of data.
You can achieve this with a helper column (any blank column in the same worksheet where you need the dates). In that column enter this formula in the first cell (here in row 2) and copy down.
=IF(ISBLANK(B2),DATE(C2,6,30),B2)
Then copy the Values from the helper column to the date column and delete the helper.
Below is a small macro that is doing the same job. It needs no helper column and over-writes your existing blanks. Before you run it make sure to check the values of the 2 constants at the top and the name of the worksheet (especially the latter!) against your requirements.
Sub WriteStandardDate()
'293
Const FirstDataRow As Long = 2 'change to suit
Const DateClm As Long = 2 'change to suit
' year column must be adjacent to DateClm
Dim R As Long
Dim Arr As Variant
Dim Rng As Range
With Worksheets("Sheet1") ' change name as required
Set Rng = .Range(.Cells(FirstDataRow, DateClm), _
.Cells(.Rows.Count, DateClm).End(xlUp)) _
.Resize(, 2)
Arr = Rng.Value
For R = 1 To UBound(Arr)
If IsEmpty(Arr(R, 1)) Then
Arr(R, 1) = DateSerial(Arr(R, 2), 6, 30)
End If
Next R
Rng.Value = Arr
End With
End Sub
Update: I used the formula suggested by Variatus: =IF(ISBLANK(B2),DATE(C2,6,30),B2) and worked fine through a helper column. There was no need to copy / paste the new dates into the Dates column. I just used the helper column as the new Dates column since full dates from the original column were not changed and got inserted in the helper column thanks to the IFBLANK portion of the formula. Thanks.

Excel fetch rows based on a cell value in another sheet

sorry for being a total noob in excel!
I have two sheets, sheet 1 named "Stocks" and sheet 2 named "Stocks search".
In "Stocks" I have from A1 to B700 values. In A column I have the stocks symbols and in B column I have the stocks' issuers symbols, so every entry in A column is unique, yet there can be repeating entries in column B.
So in sheet "Stocks search", if I enter in A1 an issuer's symbol, I want for the formula to go search in sheet "Stocks" and fetch all stocks that this issuer has in new rows.
How can this be done in a formula? Thanks in advance!
This is a VBA solution to the question. IMHO, this is more appropriate than a formula (in this case). The formula approach is OK, but there are drawbacks - you have to remember the CSE rule, and then copy the formulas down the right number of rows (which you don't know in advance), etc, etc.
This code uses the same assumptions as the formula approach.
1 - sheets = Stocks and Stock report
2 - Data in Sheets, columns A and B (header in row 1)
3 - lookup code is on Stock report
4 - Output is on Stock report
One of the advantages is that if new data is added to the Stocks sheet (i.e. the bottom row > 700), the vba automatically adjusts.
The code is self-documented. But the essence is that it creates an autofilter on "Stocks" using the lookup value as the criterion; copies the rows that meet the criteria; and pastes the result to an output range on "Stock reports". The output range is cleared before the copy/paste takes place so that there are no left-overs from any previous lookup.
I think there's something to be said for creating a dropdown list for the lookup cell. No doubt that could be automated too by getting the codes from Column A, getting the unique values, and then apply them to the lookup cell. Just a thought;)
Sub so_52537740()
' CREDITS
'
' Refer: https://stackoverflow.com/questions/17531128/copy-paste-calculate-visible-cells-from-one-column-of-a-filtered-table
' Date: 8 July 2013
' Submitted by: Jon Crowell (https://stackoverflow.com/users/138938/jon-crowell)
Dim src As Worksheet, tgt As Worksheet
Dim filterRange As Range, copyRange As Range
Dim lastRow As Long
Dim stocks As String, stockreport As String
' set values for sheet names
stocks = "Stocks"
stockreport = "Stock report"
' set values for Sheet variables
Set src = ThisWorkbook.Sheets(stocks)
Set tgt = ThisWorkbook.Sheets(stockreport)
' clear the exist target data
tgt.Range("A4:B" & Rows.Count).ClearContents
' turn off any autofilters that are already set
If src.AutoFilterMode Then src.AutoFilter.ShowAllData
' find the last row in the Stocks sheet with data in column A
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
' the range that we are auto-filtering (all columns)
Set filterRange = src.Range("A1:B" & lastRow)
' the range we want to copy (only columns we want to copy)
' in this case we are copying both columns A and B
' we set the range to start in row 2 to prevent copying the header
Set copyRange = src.Range("A2:B" & lastRow)
' filter range based on column A being equal the the value in Cell A1 of the stockreport
' consider making this a dropdown list so that there are no errors
filterRange.AutoFilter field:=1, Criteria1:=Format(Sheets(stockreport).Range("a1").Value)
' copy the visible cells to our target range
' note that you can easily find the last populated row on this sheet
' if you don't want to over-write your previous results
copyRange.SpecialCells(xlCellTypeVisible).copy tgt.Range("A4")
' turn off any autofilters that are already set
If src.AutoFilterMode Then src.AutoFilter.ShowAllData
End Sub
Giving due credit: There is, as they say, nothing new under the sun. I have based this answer on an excellent piece of work by Jon Crowell on a question in StackOverflow "Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table" in July 2013. Just goes to show what a bit of Googling and perseverance can achieve.
I believe I have an answer for you.
Try
=IFERROR(INDEX('Stocks Search'!$A$1:$A$700,SMALL(IF('Stocks Search'!$B$1:$B$700=$A$1,ROW('Stocks Search'!$A$1:$A$700)-MIN(ROW('Stocks Search'!$A$1:$A$700))+1),COLUMNS($A$1:A1))),"")
This is a CSE formula. What that means is once you enter it into cell B1, you will need to press Control+Shift+Enter. Once you do this, these brackets will appear around your formula {}
Click the fill button in the bottom right of the cell and drag the formula to the right (you will need to do this for as many cells as it is possible for answers). So if Company A has 40 possible answers, you will need to have this formula go at least 40 cells to the right.
The application of CSE formulas can be tricky. Essentially you need to go to the end of the formula in the formula bar, and then use Control+Shift+Enter.
I hope this helps.

How can I duplicate different rows in Excel?

I have single values in Ad Group column that I need to duplicate in the blank cells in the rows below. There should be 3 of each Ad Group name.
My first screenshot shows how it currently is:
My second screenshot shows how I need it:
There are 30k rows so I need a formula or dynamic way to achieve this.
Here is my answer:
Using VBA:
Sub selectBlankCells()
Dim r 'to store the number of the last row of data
Dim rng As Range
r = Range("A1").End(xlDown).Row 'to find the last row
Set rng = Range(Cells(1, 2), Cells(r, 2)) ' set the range of column B, of the data
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C" ' this is where you put the formula to
'set the data of above cell in every empty cell of
'column B
End Sub
Or you can use the Excel option:
Select the the data of column B, from the last row to the cell B1, then press F5, and this window appears.
Then press Special...
After that, select Blanks and OK
You will select only the blank cells, and then you can put the formula to link the above cell from the active cell.
This way using ([Ctrl]+[Enter] is needed to put the formula into all of the cells at once as Jeeped says):
You will get this:

How to add values from a cell list to existing values in cells

In Microsoft Excel, I have two columns in a sheet. The first column has values like B2,A13,BB125 and the next column has some number values like 12,569,78.
I want to find the cell from the first column in another sheet and to that cell add the value from the second column.
Does anyone know how to do that?
You can retrieve the value from the cell in column A and pass it to Range() to address the cell in your other sheet.
For example:
' Use A1 as an example...
Dim r As Range
Set r = Range("A1")
' Use A1's value to address a cell on Sheet2 and assign the value from B1...
Sheet2.Range(r.Value).Value = r.Offset(, 1).Value

Unable to copy data from one Excel sheet to another when not copying to column A

I have this code:
Sub Button26_Click()
Dim s1, s2
Set s1 = Worksheets("Invoice Generator")
Set s2 = Worksheets("Past Invoices")
With s2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
.Cells(, "a").Value = s1.Range("f27").Value
End With
End Sub
To copy the cell f27 (initially) from sheet one to sheet two (on a new row each time) when the button is clicked. However, if I change "a" to any other cell reference then the data only gets copied once - subsequent clicks do not work.
Anyone have any ideas? Thanks.
You are setting the position by starting at the bottom of column A and looking up to the first non-blank cell then offsetting down one row to a blank cell. If you take this position but stuff values into the row starting at column B, you never fill that empty cell in column A so subsequent calls to the same routine will reposition to exactly the same spot. If you are going to use column B as the target, you need to position the new transfer of values based on the first blank cell in columns B, not column A.
Dim targetCOL as long
targetCOL = 2
With s2.Cells(Rows.Count, targetCOL).End(xlUp).Offset(1, 0).EntireRow
.Cells(, targetCOL).Value = s1.Range("f27").Value
End With
Since you have to change the column of both the positioning method and the target of the values it makes sense to put either the alphabetic columns reference or the numerical column index into a variable so that changing it there will change it in both required places. In the above suggested modification, I've used the column's numerical index to set column B.

Resources