Google Sheets - Add new row in Named Range - google-sheets

I have Dynamic Named Range in Google Sheet B3:C and call to another cell F1, H9:H were input manually as Pic 1 and Pic 2.
My question is: anytime I add new row in Range1 B2:C, data input in column H didn't expand corresponding Pic 3.
How can the column H auto add new row follow Range1 and still keep data relate to cell next to it as shown in
Pic 4?

Related

xlwings - assigning value from cell1 to cell2 rounds the value

Trying to copy currency with 6 digits to the right of the decimal point (Treasury Bill price) in an Excel spreadsheet using xlwings; I use 2 methods. I first insert a new row (row 2 post insertion) above the original row (with values to be copied) with formatting copied from original row (row 3 post insertion).
Method 1 in column A. Assign the value of the new cell (A2) to the value of the original cell (A3)
ws1.range("A2").value = ws1.range("A3").value
The currency value in the new cell is rounded to two digits to the right of the decimal place (SBF accounting?). THIS IS NOT DESIRED.
Method 2 in column B. Step 1. Copy the value of the the original cell (B3). Step 2. Paste the clipboard to the new cell (B2).
ws1.range("B3").copy()
ws1.range("B2").paste(paste="values")
The number is copied correctly.
In both columns in Excel the number format is Currency with $ symbol and 6 decimal places
My questions are:
Why the difference?
Is there a way to use method 1 and have the number copied correctly?
The code is below:
import xlwings as xw
wb = xw.Book("copyFormula.xlsx")
ws1 = wb.sheets["oneRow"]
ws1.range("A2").value = ws1.range("A3").value # assign A2 to A3, this rounds the number
ws1.range("B3").copy()
ws1.range("B2").paste(paste="values") # These two steps copy the number correctly
The initial spreadsheet is:
Column A
Column B
price
price
$97.689885
$97.689885
After running the code the spreadsheet is
Column A
Column B
price
price
$97.690000
$97.689885
$97.689885
$97.689885
The value in A2 ($97.690000) is incorrect; it has been rounded to 2 decimal places.
I looked at Converters and Options but don't see a solution https://docs.xlwings.org/en/stable/converters.html
fzumstein (https://github.com/xlwings/xlwings/issues/2131) said the issue is a limitation of the underlying COM technology (the same as you would see in VBA).
He suggested using the .api.Value2 property in the originating cell instead of the .value property.
I can confirm that works.
ws1.range("A2").value = ws1.range("A3").api.Value2
accurately copies the value in A3 to A2.
In contrast
ws1.range("A2").value = ws1.range("A3").value
rounds the value in A3 to 2 decimal places.
I dont know why this happens, I'm not aware its documented anywhere and may be a bug. It is an issue with the number formatted as currency or accounting. Perhaps you can raise as an issue with the dev team on github.
Therefore if you change the number_format of the values to say a number with 6 decimal places it should copy with all decimal places.
Or
Preferrably do this in the code. Just about any other number format should work, in the example code below the number format for now cell A3 is changed to 'General' before the A2 value is updated from A3. A2 number format is already currency from the row insert so then just need to change the A3 format back to currency.
import xlwings as xw
wb = xw.Book("copyFormula.xlsx")
ws1 = wb.sheets["oneRow"]
ws1.range('2:2').insert('down', copy_origin='format_from_right_or_below')
ws1.range("A3").number_format = '#' # change number format to general
ws1.range("A2").value = ws1.range("A3").value # assign A2 from A3,
ws1.range("A3").number_format = '$#,##0.000000' # Set number format back to currency
Another option is to use the formula attribute as this holds the value as a string so is copied across to A2 complete but again due to the insert copying the cell formatting from A3 the value will have the number format 'currency' in that cell already so doesn't need any change.
ws1.range('2:2').insert('down', copy_origin='format_from_right_or_below')
ws1.range("A2").value = ws1.range("A3").formula

Dynamically referencing different sheets by changing sheet range argument depending on another cell's value

[Goal]
I want to be able to have a cells with formulas (such as COUNTIFS) that can change the referencing sheet range depending on the value in another cell.
[Example sheet]
To demonstrate, I've created the below example simple Spreadsheet where it has 3 different sheets. 2 with raw data (2022 Data, 2023 Data) and another with a table that will use the both sheets' raw data.
https://docs.google.com/spreadsheets/d/1Viz3SUibpaIRu77SLwLxjfcd0ZOVmHPQpu8jTdCL92I/edit?usp=sharing
Cell A2 is for selecting the date (formatted to only show the month) and cell E4 is referencing A2 to get the date/month that was selected. D4 and all month cells adjacent to each references each other to get the incremental months.
Cell range B5:E7 is using the COUNTIFS formula to count how many emails or chats were there for that month. You'll notice that the range argument is referencing as: '2022 Data'!.
What I want to do here is to create a COUNTIFS formula with a range argument that can refer to the corresponding sheet depending on the year of that column. For example for cell B5, the month is Jan 2023, I want it to refer to the 2023 Data sheet instead of 2022 Data sheet in a dynamic way.
Obviously, I could add another COUNTIFS so that it can take 2023 Data sheet's data into consideration, however, I'd have to change the formula every time it's a new year.
Using the QUERY function as an example, I know that you can refer to a cell within the string argument if you use the double quotation and the ampersand symbol (example: "&H1&") to get out of the string. I tried doing something similar (example: '"&B4&" Data'!) but nothing worked.
[Question]
Is there somehow where I could potentially change the cell referring range dynamically depending on the value of another cell?
you can use INDIRECT:
=COUNTIFS(INDIRECT("202"&RIGHT(B4, 1)&" Data!A:A"),B4)

How to drag copy a data validation and increment the column letter

I'm trying to make a drop-down list in my spreadsheet, my spreadsheet has a couple of sheets but the main two sheets are "Master" and "Sheet3".
I have made the drop-down list using the Data -> Data validation as shown in the below screenshot:
And in the "Sheet3" sheet there is a big table (1000 columns) of data that I want to make a drop-down list from, from each column in it.
Now what I want is to drag copy this cell down so that the criteria will be like this:
=Sheet3!A1:A
=Sheet3!B1:B
=Sheet3!C1:C
=Sheet3!D1:D
...etc
and so forth for 1000 rows, but when I do this, it does not increment the column letter, it's just like the first cell criteria value (=Sheet3!A1:A) for all other rows.
Is there a way to make this work without me having to edit each row in this 1000-row column manually?
Thank you all.
We have Sheet3 with 1000 column and we want to get a drop down of all 1000 column in Master sheet in a single column.
Solution
01 - Transpose Sheet3 to use it as a source of Data validation.
=TRANSPOSE(Sheet3!A1:999)
02 - Go to Data validation and set the range to ='Sheet3 Transposed'!1:1 as soon as you click save it automatically changes to ='Sheet3 Transposed'!$1:$1 with a abslout refrence just change it back to ='Sheet3 Transposed'!1:1 with removing $ dollar signs.
03 - Copy the the drop down cell D2 and paste it in the range D2:D1001, we get Error: Invalid Input must fall within specified range, in the whole range D2:D1001, to solve it copy the range 'Sheet3 Transposed'!A1:A1000 and in D2 right click: paste special > Values only
enjoy :D

Google Sheets: Absolute Reference in Formula when Adding Column

I have a spreadsheet that I want to calculate the average of the first three values in a row...
For example:
Column A Column B Column C Column D
Row 1 7/1/2017 6/1/2017 5/1/2017
Row 2 $934 $392 $214
So my formula is
=average($A$2:$C$2)
This works fine, until I add a new column to the left of Column A to add the newest month's data which now looks like this:
Column A Column B Column C Column D
Row 1 7/1/2017 6/1/2017 5/1/2017
Row 2 $934 $392 $214
The issue is that spreadsheet automatically changes the formula to
=average($B$2:$D$2)
when what I really want is to retain the original formula so it will continue to give the the average of the most recent three months of data.
Here is a link to a spreadsheet so you can see what is happening, sheet one is before added column, and sheet two is after adding column.
https://docs.google.com/spreadsheets/d/1XE2zyFGCHUfSf44vNHwXij59I68LJEL_L7cNSf0-uag/edit?usp=sharing
How can I do this? Thanks!
I suggest a sensible place to put such a formula is in ColumnA (having made room for it!) hence:
=average(OFFSET(A2,,1,1,3))

Sum values based on row value and column header

We have a Google Sheet doc which contains a date column with each date as a row value, and also multiple columns (some of which have the same name) and we want to sum the values in the cells where the row is a specific date, and the column has a specific header. For example where the date is 01/03/2017 we want to sum all the values which have the column header "X" on that date. Can this be done?
Yes it can be done
=SUMIF($C$3:$J$3,"X",OFFSET(C3:J3,MATCH(B1,B4:B15,0)+3,0))
Broken down
=sumif($C$3:$J$3 [<-header row with X],"X" [<-what we're looking for],C3:J3 [<-row to sum])
the formula above will sum the header row if there is an "X" (not very useful)
I used offset(C3:J3,[row number],0) with the header row range to push it down to the row matching 01/03/2017
To get the row number of 01/03/2017 I used Match() and put 01/02/2017 in cell B1
MATCH(B1,B4:B15 [range of dates] ,0)
I add 3 becuase my range starts at 4
You can hard code the date into the formula by replacing B1 with
DATEVALUE("01/03/2017")
I've not tried this in Google Sheets as I don't have access at the moment but it works in Excel and I'll try it in Sheets later.
Here's the formula that you can paste into A2 on your sheet "Sum of Data"
=SUMIF(Data!$B$1:$J$1,B$1,OFFSET(Data!$B$1:$J$1,MATCH($A2,Data!$A$2:$A,0),0))
It's all about changing the original formula to match your data and also locking the ranges correctly with the $ so that it will autofill down and across without breaking.
Use INDEX to peel off the appropriate column for SUMIF.
=SUMIF(A:A, G4, INDEX(A:E, 0, MATCH(H4, 1:1, 0)))
Considering a sheet where:
Cell B1 contains the date of interest (e.g. 01/03/2017)
Cell B2 contains the header of interest (e.g. X)
Cell B3 returns the sum of interest (values of all columns with header "X" on 01/03/2017)
Row 4 contains the headers to be evaluated (e.g. "Date", "A", "B", "X", "C", "X", "D")
Subsequent rows contain the actual data (e.g. dates in column A and data in columns B:G)
Refer to the image on the link below for details:
Example with cells of interest highlighted in yellow
The following formula should return the expected result:
=SUMIF(4:4,B2,INDEX(A:G,MATCH(B1,A:A,0),0))
I used Google Sheets in Portuguese. So, the formula actually tested was:
=SOMASE(4:4;B2;ÍNDICE(A:G;CORRESP(B1;A:A;0);0))
I hope that was usefeul.

Resources