Sum only visible cells in one ROW (not column)? - excel

I have a row and I want to sum only visible cells, i know if it's a column I can use subtotal (109,range), but this one doesn't seem to work for cells in one row. Anyone knows how to sum only visible cells in a row?
Please click here for picture

If a VBA solution is okay, this will work:
Function sumVisible(rng As Range) As Double
Dim cel As Range
For Each cel In rng
If cel.EntireColumn.Hidden = False Then
sumVisible = sumVisible + cel.Value
End If
Next cel
End Function
Pretty straightforward - just checks if a cell in your range has a hidden column, if it's visible, sum it.
=sumVisible(D2:M2) is how you'd use it.

You can check the width of the cell.
=IF(CELL("width",A1)=0,"hidden","open")
you can then sum your cells as need it using IF and CELL
=IF(CELL("width",A1)=0,0,A1)
more info below:
Ignoring a hidden column in an excel sum formula

You can do this using a single worksheet formula alone, provided that none of the columns which remain unhidden will have a width of less than or equal to 0.5 (which, in practice, would be so narrow as to be virtually hidden in any case).
Assuming a range of A1:E1
=SUMPRODUCT(0+(CELL("width",OFFSET(A1,,N(INDEX(COLUMN(A1:E1)-MIN(COLUMN(A1:E1)),,))))>0),A1:E1)
Unfortunately this formula will not update automatically as changes regarding hiding/unhiding columns within the range are made. As such, it will be necessary to 'recommit' it each time you make changes in that respect; one way to do this is via going into the formula as if to edit it and then recommitting by pressing ENTER.
Perhaps of interest is this post.
Regards

Related

Excel If one cell contains a number then highlight that number of cells in a row horizontally

I'm looking for a way by VBA or whichever works, even 1 command button but 4-5 processes to be called
Call process1
Call process2
End Sub
on one click which somehow seems impossible for me as I'm not that advanced in Excel VBA, but hopefully this challenge is a piece of cake for others. The task is to highlight cells horizontally by range based on the formula referring to a cell in Column AM. So "B8" is an amount of 100 which needs to be divided by the number of partitions appearing on "AM3". So 100/6. Now "AM3" is 6 so starting from "C8" a number of 6 cells (Merged in 4's) will be highlighted horizontally. "C9" is relating to "AM4" which is having a value of 9 and will highlight 9 cells (Total of 36 cells since merged) horizontally .
For now this is what i have applied but it limits to only within that range :
Sub HighlightRangeOfCells()
Dim rng As Range
For Each rng In Range("C8:AL12")
If IsNumeric(rng.Value) Then
If rng.Value <> 0 Then
rng.Interior.Color = vbRed
End If
End If
Next rng
End Sub
Thanks for your efforts and reply whatever it may be if possible or not.
As mentioned by SSlinky, the best way to do this is using conditional formatting. I have created a simple Excel sheet for explaining you how to do such a thing: I have put the formula =COLUMN()<=$J1 in cell "A1", I have used this formula for conditional formatting, I have dragged to the right, and then I have dragged down.
This is what it looks like:
As you see, cells get highlighted when their column number in some way corresponds with the value of column "J". All you need to do is replace "J" by "AM" and describe the correspondence as you see fit.
For your information: in the formula, I'm using the reference $J1, which means that while dragging and dropping, the row number might change, but the referred column always needs to be "J" (it's a combination of absolute and relative cell references).

Excel Conditional formatting formula and ranges (VBA)

I have a FormatCondition. I am reading the .Formula1 and the .AppliesTo
I want to find if the Formula contains a cell reference that will change across the range specified by the AppliesTo, eg =NOT(ISNUMBER(C3)) in a range C3:D10.
From what I can tell, that reference might be relative (eg C3) or mixed (eg $C3 or C$3) but not absolute. It will also be the 'top-left most cell in the applied range' based on https://www.ablebits.com/office-addins-blog/2014/08/07/relative-absolute-cell-references-excel-conditional-formatting/ and some other sites.
If the range is something like C3:D10 then that's easy. Find the top left using eg myRange.Cells(1, 1), convert it to a string using .Address or similar and then look for that string in the Formula, along with relevant variations adding a $. Sure, I'd have to be careful about things like strings (eg top left cell is C3' and formula is="CC3NE"`), but it's possible (still, I'd welcome suggestions on how to do that better, but that's not the question).
The real problem is if the range is more complex, as seems to happen often with Conditional Formatting. An example is $CQ$39:$FT$39,$BE$39,$BE$8:$BE$9,$CU$8:$FT$9,$CU$12:$FT$14,$BE$12:$BE$14,$BE$16:$BE$30,$CQ$16:$FT$30,$CQ$32:$FT$36,$BE$32:$BE$36. .Cells(1, 1) doesn't give the right answer for that (BE8, according to the formula that Excel made).
Further, what if the range were something like $A$5,$E$1 - there is no 'top left'. So how does excel (or the user) decide which should be in the formula?
Thus the primary question is: What is the system for Excel determining which cell to use in a formula, because I don't believe it is 'top left' - it might be 'find the topmost row, then find the leftmost cell there', or it might be the reverse, or something completely different.
The second question is: how best can I find that cell from a given range?
The bonus question (I'm happy if this doesn't get answered here) is: Is there a good way to then find references to that cell in a formula, including relative and mixed versions of the cell?
Note that these strange ranges are coming about from Rows and Columns being Cut and Pasted on a sheet with Conditional Formatting, with Excel chopping up the conditional formatting as a result (changing the range, and changing the formula, both without user input). So the priority is to deal with whatever Excel would set the formula to be on its own in such a scenario, not necessarily what a user might do - but handling both would be even better.
Thanks for updating your question and find below my comments
What is the system for Excel determining which cell to use in a formula, because I don't believe it is 'top left' - it might be 'find the topmost row, then find the leftmost cell there', or it might be the reverse, or something completely different.
Usually range in Excel is similar to a box or rectangle with a fixed length and width, e.g. B3:C8. In case of this simple box type ranges the top left most cell is the base for all formulas in conditional formatting. In other words at first formula evaluation this cell's value is checked against the formula applied in conditional formating. (Let’s call this cell as mother cell)
For example
If conditional formatting is applied to cell A1:D10 and a conditional
formula is B2 > 0, it means apply formatting to cell A1 if value of
B2 > 0, and for others apply formatting to any cell if value of a
cell with an offset of (1, 1) (that was B2 in A1 case) has value
greater then 0. i.e for formatting of B2 value of C3 will be checked.
If conditional formatting is applied to cell A1:D10 and a conditional
formula is AND($B1 >= 30, $B1 <= 60) it means apply formatting to
cell A1 if value of condition is true, but now the difference comes,
because B is now static because of $B, for conditional formatting of
B2 the condition will again be AND($B1 >= 30, $B1 <= 60) same for C1
and D1, but for A2, B2, C2 and D2 it will be AND($B2 >= 30, $B2 <=
60)
In first case the first cell that was checked against the formula was A1 and even in second case the cell that was checked against the conditional formula was A1 (which we called mother cell)
Now if the range is not like a box, it is a mixed range separated by commas, even in this case if we make a box around that range the mother cell is the cell in upper left corner, it is not the first cell of the range but first cell of the box.
For your 2nd question how best can I find that cell from a given range?
If you want to find the mother cell in a box like simple Range, it is just cells (1, 1)
If you want to find that cell in mixed range may be there are some functions out there but I don’t know of any; I would find that by using loop like below
Sub find_topleft()
Dim r, full_range As Range
Set r = Range("$O$8:$O$17,$Q$4:$Q$13,$S$4:$S$6")
Set full_range = Sheets("Sheet1").UsedRange
row_num = full_range.Rows.Count + full_range.Row - 1
col_num = full_range.Columns.Count + full_range.Column - 1
For Each c In r
If c.Row < row_num Then
row_num = c.Row
End If
If c.Column < col_num Then
col_num = c.Column
End If
Next
Debug.Print row_num
Debug.Print col_num
End Sub
Is there a good way to then find references to that cell in a formula, including relative and mixed versions of the cell?
That cell is directly linked with the formula as stated in answer to your first question.
First answer:
Short version: Excel does indeed use the most top left cell - but it does that EVEN IF THE CELL IS NOT PART OF THE RANGE.
I created a blank sheet, selected A5,E1, and put in a conditional format of NOT BLANK (which Excel does with a formula). Excel created the following:
Range: $E$1,$A$5
Formula: =LEN(TRIM(A1))>0
So even though A1 is not part of the range, that's what got used in the formula.
I checked this by duplicating it around the sheet, and it stayed consistent.
Thus Excel finds the leftmost column in the range, and the topmost row in the range, and combines them to produce the cell that should be considered the 'top left' of the range, even if the resulting cell is not actually part of the range.
This suggests an answer to the second question too: Find the top most row in the range; find the left most column; then use a cell that combines the two to compare with the formula. You could do that by going through the range string, or maybe cell by cell through the range itself; perhaps there is a better way to do it (suggestions welcome).
It's not pretty, but it would work, if I can find a good way to do those two things.
I would still welcome:
anyone who can improve on this answer about which cell is used in the formula (I did not test this extensively)
anyone who can suggest a good way to find the 'top left cell' as above
anyone who can suggest a good way to search the formula string for that top left cell
EDIT - Here's an alternate code method to Usmanhaq's excellent method - this goes through the range as a string:
Private Function FindTopLeft(rangeStr As String, rowabs As Boolean, colabs As Boolean) As String
newRange = Replace(rangeStr, ":", ",")
newRangeArray = Split(newRange, ",")
Dim lowestRow As Long
Dim lowestCol As Long
lowestRow = 2147483647
lowestCol = 2147483647
For Each cell In newRangeArray
cell = Trim(cell)
If cell <> "" Then
cCol = range(relativeCell).Column
cRow = range(relativeCell).Row
If cCol < lowestCol Then lowestCol = cCol
If cRow < lowestRow Then lowestRow = cRow
End If
Next
FindTopLeft = Cells(lowestRow, lowestCol).Address(rowabs, colabs)
End Function

Getting adjacent cell values around the current cell in Excel VBA

I want to get the adjacent cell values for calculation in VBA.
It is easy to get the left cell of the current cell(e.g.H19) usually like this:
=H18
If I copy this cell to other cells, each of them is changed to an appropriate cell number.
But in VBA code, I am not sure if I am right to get the value of the left cell.
Public Function LeftCell()
LeftCell = ActiveCell.Offset(0, -1).Value
End Function
I am not sure this is correct, I tested copying this cell to other cells but each result is not changed automatically.
I clicked all kinds of Calcuation buttons on the Menu, changed Calculation as Automatic, but there is no calculation occur.
The only way I can do is to manually select each cell and press enter.
Is there any way to calculate all cell values?
Otherwise, "The Active Cell" means "The Selected Cell by Cursor"?
Thanks for your help in advance.
Adding a formula as #Chris Harper suggests would work, but then you may as well just write the formula in the cell.
Rather than the ActiveCell you want the cell that called the formula.
Public Function LeftCell()
LeftCell = Application.Caller.Offset(, -1).Value
End Function
Edit: If you want the cell to update whenever you change the value add Application.Volatile True as your first line in the function.
https://msdn.microsoft.com/en-us/library/office/ff193687.aspx
Calculate method in Excel VBA do all kind of calculations. You can even define a range to calculate only a range of specific cells like Worksheets("Sheet1").Calculate.
Yes, ActiveCell is always the Selected Cell.
As an alternative to setting value by Offset, you can use ActiveCell.FormulaR1C1 = "=RC[-1]"

Excel: How to refer to entire column in a formula by column title?

I want to write a formula like =SUM(tab2!A:A) but instead use the column title of A which is say "count". How can I modify this to look more like: =SUM(tab2!"count")?
The reason I want to do this is because I copy and paste a spreadsheet from another source in tab2 and the column referring to "count" may be in a different column. I want the formula to give me the correct calculation as soon as I paste the new spreadsheet by automatically finding the column to sum up by title name.
I must be missing something because this seems like a very basic question and I can't find the answer anywhere...
Thanks for your help!
I like the idea of naming ranges proposed by #Doug, but if the issue is that you are dumping your data in [and you don't know in advance which column is going to be where] and would need to rename your range every time, there are other options - I suggest using OFFSET. OFFSET allows you to dynamically create a range, starting at a particular point and moving down/up / right/left for as many rows and columns as you determine.
In your case, you will need to combine that with a method for searching the columns to see which one says "Count". I am assuming that your column headings are always in row 1. If they aren't [or if they aren't always in row 2, or any row you know in advance]... you can get around that but then I'd recommend you try to make your data more uniform instead of creating unnecessary Excel workarounds.
In total your formula would look like this:
=SUM(OFFSET(A1,,MATCH("Count",1:1,0)-1,ROWS(A:A),1))
What this does is first determine which column the word "Count" is found in, in row 1. Then it subtracts 1 from that number - this now equals the number of columns to the right that it is, from column A. It uses offset to create a reference to that column, for all rows, and sums those rows together.
Check out the name manager for naming ranges :)
You didn't say whether you would consider a macro solution. If so, this may work.
If the sheet you are pasting into is Sheet2 and the sheet you want the result in is Sheet1, then this macro, if placed in the Worksheet_Activate event of Sheet1 will give you the result as soon as you click on the Sheet1 tab afetr pasting your data into Sheet2:
Private Sub Worksheet_Activate()
Dim r As Range
Dim rCol As Range
Dim rFound As Range
Dim ws As Worksheet
Dim lTotal As Long
Set ws = Sheet2
Set r = ws.Cells
Set rFound = r.Find("count")
If Not rFound Is Nothing Then
Set rCol = rFound.EntireColumn
lTotal = Application.WorksheetFunction.Sum(rCol)
End If
Cells(1, 1) = lTotal
End Sub
It does assume there is only one cell with the word "count" in it on Sheet2.

How to make a Cell in Excel Reference itself

I have a table that contains a column that has a risk score. I want to write a formula to examine each cell and determine if the cell is blank, then make the cell value 100, else leave the cells current value. The code I'm trying to use( For cell D2) is as follows:
=IF(ISBLANK(D2),100,D2)
I'm having trouble doing this due to a cell's inability to reference itself easily and I believe inserting this formula into the cell simply overwrites the cells current value, any thoughts?
Say the values you want to process are from D2 through D100. Try this small macro:
Sub OneHundred()
Dim r As Range
Set r = Range("D2:D100").Cells.SpecialCells(xlCellTypeBlanks)
r.Value = 100
End Sub
Adjust the range to meet your needs.
It can be done because I have a spreadsheet that allows this to happen. Part of my formula in cell T3 is as follows: IF(B3="","",IF(B3="Ready","",IF(T3="",TODAY(),T3))) First it checks some other things but at the end it checks itself to see if it is blank. If it is, it writes todays date. Otherwise it just writes itself.
I remember I had to change something in the settings to allow this to happen but I can't find it now.

Resources