I am applying Max formula of excel using OpenPyXL but it is deleting formula on opening file - openpyxl

Please note that I am applying MAX formula in a column through OpenPyXL.
The source data is also coming from Formula applied through OpenPyXL.
Problem is that it is deleting formula.
Some code given below:
#cumm profit
if(wsprofitrowno==2):
wsprofit.cell(row=wsprofitrowno, column = 3).value = '=SUM(B2)'
else:
wsprofit.cell(row=wsprofitrowno, column = 3).value = '=B%d+C%d' % (wsprofitrowno, wsprofitrowno-1)
#Peak
if(wsprofitrowno>2):
wsprofit.cell(row=wsprofitrowno, column = 4).value = '=MAX($C$2:C%d' % (wsprofitrowno)
wb.save("output.xlsx")
wsprofitrowno = wsprofitrowno + 1

You are trying to add this as a formula in the Excel cell '=MAX($C$2:C%d' % (wsprofitrowno) so Excel would see that as an error and the formula would be rejected. The Excel MAX function needs a closing bracket, since you are trying to use the row 'wsprofitrowno' as the row value for the second coordinate of the MAX formula you can do either
Add the close bracket; '=MAX($C$2:C%d)' % wsprofitrowno
just do '=MAX($C$2:C' + str(wsprofitrowno) + ')'

Related

Python Openpyxl is unable to check from Column 6 onwards

I want to use Openpyxl to check if cells.value='OK' and the interior color of its corresponding row in another sheet is !='FF9BCCFF'. I used the code appended at the end. It works fine for Column 1 to 5 in
R_sht.cell(row = i, column=7).value=='OK'
But when I change to column 6 onwards the code doesn't work. It goes to else block although I have rows that meet the criteria of if block.
I even tested by copying and pasting column 5 to column 6. So both columns have SAME contents/format/etc. And the code works on column 5 but fails in column 6.
I am not sure why there is this issue. I tried on 2 different Excel workbooks. Same issue. Code fails for column 6 onwards.
There is no upload function here for me to show a sample workbook.
But hope someone can still help. Thank you
for i in range (first_used_row,last_used_row+1):
if R_sht.cell(row = i, column=7).value=='OK' and IN_sht.cell(row = i, column=1).fill.start_color.index!='FF9BCCFF':
print(str(i) + "Not Blank")
else:
cell_color=IN_sht.cell(row = i, column=1).fill.start_color.index
print(str(i) + "Blank" + str(cell_color))

Issue using Cells().formula

I'm attempting to input a formula into a column in a table. The formula uses the table headers to solve for a result. the location used in the Cells() command also uses a variable as seen below:
Sheet6.Cells(17, k + 7).Formula = "=[#[Net Weight]]/([Length] * $J$10 * $J$10)"
Range(Cells(17, k + 7).Address()).Select
Your code works for me, if I make sure that the cell is in the table or next to it. If I put it in a random cell it results in the same error as you get.

Auto-Numbering depending upon Row or Column being hidden

I want the cell to number itself in an incremental order depending upon the filters. I found the easiest way is to check for the above Row if it is hidden or not then number itself from 1 if hidden and previous cell value+1 if not hidden.
I've tried to achieve this using the Formula
=IF(COUNTA(ADDR)>SUBTOTAL(103, ADDR), 1, ADDR+1)
Where ADDR is defined as follows:
=ADDRESS(ROW()-1,COLUMN(), 4, TRUE)
SUBTOTAL function returns #VALUE as it cannot contain 3-D References.
Tried replacing SUBTOTAL() function with AGGREGATE(), same issue.
Tried to use VALUE() function to convert the ADDR string to value.
I tried to use VBA
Public Function IsHid(i As Integer)
Dim re As Range, x As Integer
Set re = Range("A" & i)
If re.EntireRow.Hidden Then
Set re = Range("A" & i + 1)
re = 1
Else
x = re.Value + 1
Set re = Range("A" & i + 1)
re = x
End If
End Function
The above function returns #VALUE.
The below function also returns #VALUE.
Public Function IsHid(i As Integer)
If Excel.ThisWorkbook.ActiveSheet.Rows(i).Hidden Then
Cells(i + 1, 1).Value = 1
Else
Cells(i + 1, 1).Value = Cells(i, 1).Value + 1
End If
End Function
Very much appreciated if this functionality can be obtained by means of FORMULAS rather than the VBA
Use Subtotal combined with Count(A):
=SUBTOTAL(3,B$2:B2) and paste down.
This can be in column A and will number only visible rows when you filter on B, C, etc.
You might want to take a look here as well, for additional explanation.
Edit:
Let's say you have Sheet1 and you fill up Range A:G. In column A you want the numbering described in the question. Then Range A1 will hold a header (e.g. FilteredID) and Range B:G will hold your other values.
In range A2 all the way down, you put the formula =Subtotal(3, B$2:B2), in Range A3 this will be =Subtotal(3, B$2:B3), in A4 =Subtotal(3, B$2:B4), etc.
Now, when you filter on column B, C, D etc. so you'll have invisible rows, the numbering in column A will show the visible Row number.
For example, assuming you want to start numbering in row 2 and in column A and you have Excel 2010 or later:
=AGGREGATE(4,5,A$1:A1)+1
Just adjust the start cell as required.

Return values from other workbook

Have a question about formula which will resolve my issue.
In my main workbook I need to compare data from two sources.
One of the columns must retrieve data(amounts) from other workbook.
I want formula which will search for all amounts in column G and will skip all blank cells. Tried to use VLOOKUP, INDEX and SMALL functions but no effect.
Each day amounts are different and I need to match them in main file and find exeptions.
Any ideas?
How about an array formula such as the following?
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-1)-ROW($G$2:$G$20)+1)
The formula would have to be placed into cell I2 as an array formula (which must be entered pressing Strg + Shift + Enter). Then you can drag down the formula to get all the other values.
It doesn't have to be in column I but it has to be in row 2 because this formula get's the n-th Number from the list which is not = 0. The n-th place is (in this formula) row()-1. So for row 2 it will be 2-1=1 and thus the 1st number. By dragging down the formula you get the 2nd, 3rd, etc. number. If you start with the formula in cell I5 instead then it would have to be adjusted to be as follows:
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-4)-ROW($G$2:$G$20)+1)
You could loop through the column and store each value >0 in an array and then compare or you loop through the column and compare directly...
something like:
Dim i as Integer = 0
Foreach value in Maintable
Do
If otherworkbook.cells(i,7) = value Then '7 for G
do your stuff
End If
i = i + 1
While i < otherworkbook.rows.count
Next
I think that could be the right approach

Absolute reference with R1C1 using a variable to dictat row number (VBA)

I have a code that creates a number of separate lists under each other. For each of these lists I'm running a for loop to assign an equation to each row at the end column. This formula should multiply the neighbour cell (a relative reference) with cell at the top of that particular list (an absolute referance). The problem is that the lists are of arbitrary length and generated earlier in the code, so I can't assign the absolute referance beforhand.
I was thinking on saving the row number (row 1 = 1, row 2 = 2 etc) in a variable and then use the variable in the R1C1 notation (= "R(variable)C5*RC[-1]), but I cant seem to get this to work... The variable will be the same throughout the for loop given in the example below, but will change next time the same for loop is entered.
Is this even possible?
(I know the parantheses in the R1C1 are not the proper notation, but this is to show where I want my variable)
...
variable = 3
For i = 1 to count
last = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Cells(siste, "E").FormulaR1C1 = "=R(variable)C5*RC[-1]"
Next
Just one small change:
Cells(siste, "E").FormulaR1C1 = "=R" & variable & "C5*RC[-1]"