Bottom Page

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() 
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  simple key value loop help siten0308 4 153 Jun-25-2019, 02:53 PM
Last Post: siten0308
  Simple while loop only works on first attempt jsb83 2 83 Jun-20-2019, 08:57 PM
Last Post: jsb83
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 99 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  Arrange list of tuple using loop batchenr 7 213 Jun-16-2019, 03:24 PM
Last Post: Abdullah
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 162 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Loop through array items dynamically in a certain format bhojendra 3 130 Jun-11-2019, 03:37 AM
Last Post: micseydel
  change to loop amirliker 3 148 Jun-06-2019, 01:08 PM
Last Post: heiner55
  while loop ef26 5 194 May-27-2019, 01:25 PM
Last Post: DeaD_EyE
  my function is stuck on loop - even when it not supposed to be korenron 2 221 May-26-2019, 12:31 PM
Last Post: korenron
  Working on my FOR loop wilsonrivas 1 309 May-24-2019, 04:05 PM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)