Python Forum
Com Error with macro within for loop due to creating new workbook in the loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Com Error with macro within for loop due to creating new workbook in the loop
#1
So I have three files right now, 1. Template file with the rater, which performs calculations, and two files with data that gets copied to the rater.

I wrote a code that loops through the files assigns them to a work book copies the data from the work book to the rater, runs the macro to perform the calculations, and copies the output column back to the work book/save/close.

It works fine if I run it in pieces but click on the worksheets. however when I try to run it within the loop it gives me a com_error.
This leads me to believe that when I create the new workbook it makes the new workbook the active workbook.
 app=xw.apps.add()
rater=app.books.open(path)
#macros to run
clear_all=rater.macro('clear_batch')
batch_rater=rater.macro('batch_rater')

for file_name in filenames:

    os.chdir(input_path)
    wb=xw.Book(file_name)    

    comp_data=wb.sheets("Data for Rater").range("copy_range").value
    rater.sheets("Batch Rater").range("b23").value=comp_data



    batch_rater()


    comp_premium=rater.sheets("Batch Rater").range("premium_copy_py").value
    #wb.sheets("Batch Rater").range("AQ23").options(transpose=True).value=test
    if file_name=="cien_cova_roofage_rooftype.xlsx":
        wb.sheets("All Company Premium").range("AQ67202").options(transpose=True).value=comp_premium
    elif file_name=='cien_cova_yearbuilt.xlsx':
        wb.sheets("All Company Premium").range("AQ12002").options(transpose=True).value=comp_premium


    clear_all()


    os.chdir(output_path)
    wb.save(str(file_name[:-4])+'_batch_output.xlsx')
    wb.close() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Re Try loop for "net use..." failures tester_V 10 447 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 685 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 428 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 308 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 358 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 374 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 545 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,106 Dec-07-2023, 12:28 PM
Last Post: RockBlok
  Which GUI can have indefinite loop ? jst 20 1,578 Nov-16-2023, 10:23 PM
Last Post: jst
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020