Bring back value from specific cell in Excel after each combination was entered

25 Views Asked by At
from itertools import product
import openpyxl

# Load your Excel file with data_only=True
workbook = openpyxl.load_workbook(r'D:\1 - DOWNLOADS\PICK 4.xlsx', data_only=True)

# Get the sheet by name
sheet_name = 'PICK4'  # Replace with the actual sheet name
sheet = workbook[sheet_name]

# Get the initial values in BN2:BQ2
values = [sheet[cell].value for cell in ['BN2', 'BO2', 'BP2', 'BQ2']]

# Generate all combinations of numbers from 1 to 10 for each cell
combinations = product(range(1, 11), repeat=len(values))

# Iterate through all combinations
for combo in combinations:
    # Enter the current combination into cells BN2:BQ2
    for i, value in enumerate(combo):
        sheet.cell(row=2, column=62+i).value = value

    # Calculate the value of cell BG1 for the current combination
    bg1_value = sum(combo)  # Replace this with your actual calculation

    # Update the value of cell BG1 with the calculated value
    sheet['BG1'].value = bg1_value

    # Print the value of cell BG1 for the current combination
    print(f"Combination: {combo}, Value in BG1: {bg1_value}")

The above code currently prints sequential numbers from cell BG1 when each combination should have different values in that cell. Here's what it currently looks like when it prints:

Combination: (10, 10, 9, 3), Value in BG1: 32 Combination: (10, 10, 9, 4), Value in BG1: 33 Combination: (10, 10, 9, 5), Value in BG1: 34 Combination: (10, 10, 9, 6), Value in BG1: 35 Combination: (10, 10, 9, 7), Value in BG1: 36 Combination: (10, 10, 9, 8), Value in BG1: 37 Combination: (10, 10, 9, 9), Value in BG1: 38 Combination: (10, 10, 9, 10), Value in BG1: 39 Combination: (10, 10, 10, 1), Value in BG1: 31 Combination: (10, 10, 10, 2), Value in BG1: 32 Combination: (10, 10, 10, 3), Value in BG1: 33 Combination: (10, 10, 10, 4), Value in BG1: 34

That's just an example but it's roughly the same with each combinations. Everything the code does is perfect, I just need it to give me the correct value from cell BG1 when it prints it. Please help me fix this.

Thank you!

0

There are 0 best solutions below