How to avoid adding a new row to the first row of subtotaled ranges to protect Subtotal formula - vba

I have a Excel sheet, which includes many groups and subgroups. Each subgroup has their own subtotal. And as long as I can't know how many lines users will require for the subgroups, I only left 3 rows blank for each subgroup and created a button which triggers Automatically Copy and Insert Copied Row macro.
Now I need to avoid users to trigger the button while they are on the First and the Last row of any subgroup. (If you copy and insert first row, subtotal formula doesn't recognise the new added line because it will be out of the range and Last row has another specific formulas which I don't want users to copy above)
I've been searching SO and the other Excel blogs for a long time but because of my list is very dynamic and have many different subtotals none of them was suitable to my case.
How can I define all of the first and last rows of my different subtotal ranges (and maybe their Title's rows) to avoid them being copied and then inserted above?

I can imagine 2 ways to do this. When the "New Row" button is pressed check if the current row is a headline/subheadline whatever …
by checking its format (eg specific background color etc).
If Not Worksheets("Sheet1").Range("A" & iRow).Interior.Color = 15004911 Then
'copy that row
End If
or by using an extra helper column that specifies the rows as header rows or non copyable rows. If you don't want the helper column to be seen you can hide it.
If Not Worksheets("Sheet1").Range("X" & iRow).Value = "Header" Then
'copy that row
End If
'where X is the helper column
And if it is a headline row then deny the copy process.

One way that doesn't involve programming have your sum range extend into rows that won't change. So you could start the subtotal in the header row (assuming there is no content in the relevant cell in that row). Another way is to have a hidden row at the top and bottom of each sum range and that is included in the sum range. So you would sum rows 10 to 14, but 10 and 14 would be hidden (and empty) and the user would just get shown rows 11, 12 and 13. Adding a new row would push the hidden rows down and extend the subtotal.
Another way is to use the indirect function.
Say your formula was
=SUBTOTAL(9,H7:H10)
If you use indirect for the lower bound, it will always refer to the cell immediately above, regardless of how many rows are added in between.
=SUBTOTAL(9,H7:INDIRECT("H"&ROW()-1))
And taking it one step further, use the upper title row as the anchor to always add up the gap between the title and the subtotal.
=SUBTOTAL(9,INDIRECT("H"&ROW(H6)+1):INDIRECT("H"&ROW()-1))

Related

How do I check to which row area field a cell in a pivottable belongs to?

I have a PivotTable in compact form layout (default layout) with multiple row area fields. This results in multiple grouping levels with buttons to collapse or expand rows.
I want to modify a cell depending on the grouping level the row is in.
How can I check in which row the cell is in?
The only idea I had so far was to check the value of the leftmost cell in the corresponding row and compare it with each item from each rowField.
But I guess there is a smarter way?
It took me a while. But at the end I just needed PivotCell.RowItems.Count
Depending on the Count result I can new manipulate the cell.

CheckBox to limit a data validation list

Moving forward, slowly.......
I added a column, that with the combination of my checkboxes, gives the name of active employees.
Screen Shot of active list of employees
If I use $T$6:$T$16 as my validation list, I still get 11 options in the drop down. I want the drop down to show only the 5 names that are in that list.
No need to use VBA here, Here's a quick solution.
Use a 1 or 0 in a column next to the employees' names corresponding to whether they are active or not.
Then create a column in the sales sheet which has a vlookup for this value
=VLOOKUP(E4,$A$4:$B$8,2,FALSE)
This looks for value E4 (the name), in range of all names, second column for their active status, FALSE for an exact match.
Then use a filter:
Highlight range of sales
"Data" tab
"Filter"
Then you can use the filter on the new Active column to only show the employees which are still active. See the linked image for the final outcome. The vlookup lives in column D.
Final outcome image in Excel
Of course if you want this in two sheets, you can split what I've done to different sheets with no extra effort other than adding sheet references to the range in the vlookup.
Hope this helps

fix for moving rows in google-spredsheet breaks array formula

I have an array formula in google sheets for an entire column, e.g. the following formula in C1
ArrayFormula(A1:A+B1:B)
And there is data in columns A and B.
If I were to grab a row and move it to another location. As soon as I move it the respective value in column C of that row is pasted as hard value and breaks the entire array formula.
Is there a way around this?
Unfortunately, there is no simple way around it. With arrays, the formula is usually tied with the positioning of the values as results vary according to the position of each value in an array. Hence, moving anything will result in the distortion of the formula.
The only (simple) way around it is to move your values by the cut-copy-paste method instead of dragging the whole row around. OR (For a more robust but complex implementation) write a script for Custom Functions in your sheet which will perform the necessary calculations and will not be affected when you move the values as it takes inputs from cell positions that have been pre-defined in the script.
Workaround found.
I was doing a manual sort which changed the row of my ARRAYFORMULA(), but has the same consequence as drag-n-dropping that "special row".
You will have to work with two sheets however.
Suppose that in your original data sheet (sheet 1), your have data on two columns (A and B), and you want to use ARRAYFORMULA() on column C, like in your example.
Leave sheet 1 "as is", create another sheet (sheet 2) and in top left cell type this:
={A1:B}
In sheet 2's column C, one cell below top (to leave room for header), enter:
=ARRAYFORMULA(A2:A+B2:B)
Then you can sort data as you wish in sheet 1, and ARRAYFORMULA() will always work in sheet 2 👍

VBA msgbox duplicate value base on 2 columns

I'm trying to get a msgbox when a value is duplicate base on 2 columns. The first column Value can be repeated but the second column will determine if it's a duplicate or not.
i.e.
Column B = Code,
Column L = Month
The user can enter the Code several times, but if he enters it on the same month I want the msgbox pop up
Is your intention to warn\inform the user? If so, I would do this without a macro. I would use conditional formatting to make the cell change color whenever the duplicate information is entered.
Create a column on your worksheet with a formula that concatenates the information in column B&L the formula would be =B1&L1 (copy this formula down the table). You can hide the column so nobody sees it. For this example, let's say you used column "M".
Select the entire Code or Month column (or both) and click the CONDITIONAL FORMATTING button on the Home tab, choose NEW RULE, USE FORMULA TO DETERMINE WHICH CELLS TO FORMAT, then enter the following formula: =COUNTIF($M$4:$M$1000,M1)>1 (note I am assuming your range of data is less than 1000 records, otherwise increase that number). Set the format to something like a red fill and instantly duplicates will be flagged. The user will also be able to quickly locate the record where this combination was already entered as that will turn red too.
If you really do want a macro to do this, you could simply write a loop to compares the active cell value of B(activerow) & L(activerow) to each previous B#&L# combination. If a match is found, use the intersect method to pop-up a the message. Here is really a good article about the intersect method: http://www.ozgrid.com/VBA/vba-intersect.htm.

Sorting by cell value or color in multiple cells

I am trying to sort data based upon either color (RGB(186,200,8)) or value ("AMP") within a cell. That part is easy but the problem comes when I want to look for the same value/color in multiple columns (it can occur up to four times) and put the ones with all for equal to the value at the top and then three values next and on down to no match.
I'm not sure how to go about, I think a for loop and/or else would work but I can't come up with one. Any suggestions?
My suggestion would be to calculate "hit" value for the row and based on that you could do the sort easily. For instance if you have two matches on the row value for that row = 2 etc. after each row validated just sort by the value and clean the data.