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
  For loop prints strane values? colt 2 40 1 hour ago
Last Post: ichabod801
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 60 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  Beginner problem, replace function with for loop Motley_Cow 9 201 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  nested for loop dilemma YoungGrassHopper 9 205 Sep-13-2019, 03:56 AM
Last Post: jsira2003
  nested for loop dilemma 2 YoungGrassHopper 12 228 Sep-12-2019, 02:06 PM
Last Post: YoungGrassHopper
  for loop stumbling block YoungGrassHopper 8 158 Sep-11-2019, 03:34 PM
Last Post: YoungGrassHopper
  while loop stumbling block YoungGrassHopper 5 96 Sep-09-2019, 08:36 PM
Last Post: YoungGrassHopper
  loop printing twice anna 1 108 Sep-04-2019, 09:13 AM
Last Post: anna
  loop through python pandas data frame Johnse 4 114 Sep-02-2019, 01:45 AM
Last Post: Johnse
  problem with for loop using integers python_germ 5 174 Aug-31-2019, 11:42 AM
Last Post: jefsummers

Forum Jump:


Users browsing this thread: 1 Guest(s)