How to fill the empty cell with value above cell using xlwings? - xlwings

How to fill the empty cell with value above cell using xlwings?
Can you let me know the code?

You can do this by looping through the range of cells required, and if the value of the cell is None (the value where the cell is empty for cell.value) then set the value to that of the cell 1 row above using an offset of -1 rows, 0 columns:
for cell in ws.range("A2:D3"):
if cell.value is None: # and cell.row != 1 if going to include row 1 in the range.
cell.value = cell.offset(-1, 0).value
Note the comment in the if statement that an additional condition is required if the range is going to include the first row, because an error occurs when offsetting to row 0 which obviously doesn't exist.
Also, where the cell above is also empty, the value of the cell will not change. But as the loop is through all cells in the row left to right, then to the next, any vertical series of consecutive empty cells will be filled with the same value from the cell above the first empty cell.

Related

How to collect data and headers for non blank cells in a row in Sheets

I cannot find a solution to my problem:
I have a sheet with ~290 rows and ~80 columns. The first row and column are fixed/header.
I would like to collect non-blank values and their header into column B.
I've tried to search for solutions, but I'm not as good at excel, so I cannot wrap my head around most of the advice that I've found.
In Google Sheets you could use an Array formula. I got this:
The formula I've used:
=ArrayFormula(CONCATENATE(IF(--(C2:G2<>"")*COLUMN($C$1:$G$1)<>0;$C$1:$G$1&" "&C2:G2;"")))
This is how it works:
(--(C2:G2<>"") will return an array of 0 and 1 if the cell is blank or not
COLUMN($C$1:$G$1) will return an array of column numbers of each cell
(C2:G2<>"")*COLUMN($C$1:$G$1) we multiply both arrays, so we will get an array of column numbers of non blank cells and 0 of blank cells
<>0;$C$1:$G$1&" "&C2:G2;"") We check if each number in the array obtained in step 3 is 0 or not. If it's 0, it returns a null value, if not, it returns the value of cell
CONCATENATE will concatenate all values from previous array (step 4) so we concatenate null values with real values of non blank cells.
Not sure if this will make the sheet load slower if you have too many records.
Hope this helps
Excel is not the same Google Sheets
=ARRAYFORMULA(TRIM(REGEXREPLACE(
TRANSPOSE(
QUERY(TRANSPOSE(IF(C2:F13<>"",C1:F1 & ", ","")),,99^99)
),
"((\s+)|(,\s*$))",
" "
)))
My sample
use:
=ARRAYFORMULA(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
IF(C2:G<>"", C1:G1&" "&C2:G&",", )),,99^99))), ",$", ))

Adding a range of cells to one cell based on a condition

I have a sheet in Ms Excel with Loan details, where the loan status is either 'Paid' or 'Unpaid'. For every row, I want to add the amount value to cell C18 if the corresponding cell in status column reads "Unpaid".
eg. for row 10,
=IF(J10 ="Unpaid", $C$18+G10, $C$18+0) **{This does not work}**
How can I achieve this so as to use the formula for a couple of rows?
You might try ...
=C$18+(J10="Unpaid")*G10
This formula will add C18 to G10, but only if J10 says "Unpaid", otherwise, it will just add C18 to 0.
It can be "dragged down" for the other rows, and the '$' on C$18 will keep that cell fixed while the references for J and G will correspond to the row that the formula is in.
My guess from what you're saying is that you want to add column G only when the corresponding value in column J = "Unpaid".
If so, the formula you put in C18 is =SUMIFS(G:G,J:J,"Unpaid") or =SUMIFS(G10:G,J10:J,"Unpaid") if you want to start the sum at row 10.
You can't add to $c$10, only to a new column, lets say called 'balance'. There (column k) would be the formula =$k9+if($j10="Unpaid",$g10,0)
in Google Sheets:
=ARRAYFORMULA(IF(J10:J50="Unpaid"; C18+G10:G50; C18))

Index range of data and compare to column then return first available option from same column

I have a range of data on Sheet1 in B2:K18 and a list of values on Sheet2 in A2:A.
In a cell, say Sheet1!B20, I would like to automatically return the first value from the list in Sheet2!A2:A that is not found in the range Sheet1!B2:K18.
Note that Sheet1!B2:K18 will be updated so the returned value will hopefully change if the original returned value is entered into Sheet1!B2:K18.
I am assuming it has to include INDEX and MATCH but I have been unsuccessful.
=IFERROR(INDEX(FILTER(sheet2!A2:A,
NOT(REGEXMATCH(sheet2!A2:A, "^"&TEXTJOIN("$|^", 1, B2:K18)&"$"))), 1, 1))

I need a cell to subtract one number from another based on a code word in a third cell

I want a function to subtract one number in a cell from another number on a different page and cell based on what word is in the cell next to the original number.
In the picture is a lista of expenses, so far only groceries is labeled, but the rest will be labeled with car loan, gasoline, clothes, and miscellaneous.
I need a function that will take my budgeted number (like 155 on the right), and compare it to all the words in column H. If a word like “gasoline” is found I want the function to subtract the value to the left of the key word.
New picture with updated progress
=ARRAYFORMULA(IF(H2:H="Groceries", J2:J-G2:G, ))
This code is making all of I return what subtracting any number with “Groceries” next to it by the value in its respective J cell. I only need I2 to change when a new “Groceries” key word is entered, and the value in its respective H cell should be subtracted from the J2 cell.
(https://i.stack.imgur.com/5ar6d.png)
try like this in I2 if it fits
=IF(H2="Groceries"; G2-J2; )
then you could enter the next level with using an array formula in I2
=ARRAYFORMULA(IF(H2:H="Groceries"; G2:G-J2:J; ))
for more words use regex like:
=ARRAYFORMULA(IF(REGEXMATCH(LOWER(H2:H);
"groceries|car loan|gasoline|clothes|miscellaneous"); G2:G-J2:J; ))

Google Sheets; H = C OR if empty D OR if empty E OR "empty"

=if((len(C2:C);C2:C;"")(len(D2:D);D2:D;"")(len(E2:E);E2:E;"")*(len(F2:F);F2:F;"");"")
I was going for this one, but it didn't get me far. Whats wrong; any ideas?
Please try:
=ArrayFormula(if(len(C2:C),C2:C,if(len(D2:D),D2:D,if(len(E2:E),E2:E,if(len(F2:F),F2:F,"")))))
Looks, row by row, first at the ColumnC cell – if populated returns that value, otherwise passes on to the ColumnD cell and if that is populated, returns that value … and so on. If it finds four blanks in a row then for that row it returns a blank.