I need to find in Activities[1:30000] the 1st cell that matches Report[1, 0] and remember that cell address.
Then copy Report[1:30000, 0:37]
and paste, values only, starting in the matched cell. A friend gave me this code, but it doesn’t work. Column A in both workbooks are named the same and sorted ascending. Activities.xlsx is a table, extending from [38:52]. The ‘Paste values only’ needs to be contained in [0:37] as not to erase formulas. Any assistance would be greatly appreciated.
Here is the code I’m working from:
import pandas as pd
# Load the Report.csv file into a DataFrame
report_df = pd.read_csv('Report.csv')
# Load the Activities.xlsx file into a DataFrame
activities_df = pd.read_excel('Activities.xlsx')
# Get the value in Report[1, 0]
value_to_match = report_df.iloc[1, 0]
# Find the first occurrence of value_to_match in Activities[1:30000]
match_index = activities_df.iloc[1:30000].eq(value_to_match).stack().idxmax()
# Get the row and column index of the matched cell
match_row, match_col = match_index
# Copy the specified range from Report
copied_data = report_df.iloc[1:30000, 0:37]
# Check if there is enough space to paste the copied_data into Activities starting from the matched cell
rows_to_copy, cols_to_copy = copied_data.shape
rows_available = activities_df.shape[0] - match_row
cols_available = activities_df.shape[1] - match_col
if rows_available >= rows_to_copy and cols_available >= cols_to_copy:
# Paste the copied_data into Activities starting from the matched cell
activities_df.iloc[match_row:match_row+rows_to_copy, match_col:match_col+cols_to_copy] = copied_data.values
else:
print("Error: Not enough space in Activities to paste the copied data.")
# Save the modified Activities DataFrame to a new Excel file
activities_df.to_excel('modified_Activities.xlsx', index=False)
But it doesn’t work.
1