Home>

I would like to extract the required 1 row data (14 cells) from the Excel data, duplicate 12 rows on a new sheet and paste it up to the last row of the original data. I was able to paste it into multiple lines, but the process of repeating it until the last line is not executed. I would like advice on how to do it.

Error message
Corresponding source code
def act (a): # Function to get the column part for one row

    for i in range (1,14): # Extract specified 14 cells
        copy = ws.cell (row = a, column = i) .value
        copy_prg = ws.cell (row = a, column = 33) .value
        copy_biko = ws.cell (row = a, column = 34) .value
        for j in range (1,12): # Duplicate the extracted part in 12 lines
            ws2.cell (row = j, column = i, value = copy) #Paste all column format parts
            ws2.cell (row = j, column = 1, value = ws ['K1']. value) # Put k1 in the first column
            ws2.cell (row = j, column = 14,value = copy_prg) #AG Insert column
            ws2.cell (row = j, column = 15, value = copy_biko) #AH Insert column (no line break)
#The process below does not work
max = int (ws.max_row) #Calculation of the number of last rows (221 rows)
for y in range (6, max, 4): # Execute while skipping 4 lines from the 6th line to the last line (because lines 1-3 are blank)
    act (6)
What I tried Supplementary information (FW/tool version, etc.)
  • Answer # 1

    As pointed out by peterman, act (6) of this process should be act (y).

    for y in range (6, max, 4): # Execute while skipping 4 lines from the 6th line to the last line (because lines 1-3 are blank)
        act (6)

    The following process is a paste process, but the line number is fixed.

          for j in range (1,12): # Duplicate the extracted part in 12 lines
                ws2.cell (row = j, column = i, value = copy) #Paste all column format parts

    This will always write the values ​​from the 1st line to the 11th line (not 12).
    If the original first line is 1-12, the original second line must be written in 13-24, so the argument of range () must be changed according to the value of the original line. ..
    The original number of lines is in the variable a. The number of destination lines can be calculated using it.

  • Answer # 2

    I'm not sure about the contents of the act function, but since act (6) is executed every time in the for statement, isn't it incremented? Since the argument does not include y, I think the same process will be repeated.