Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50070

Sub that works separately but has infinite loop when run with other subs

$
0
0
To all proficient VBA users I would greatly appreciate your help with the following:
File that works separately but loops indefinitely when combined with the execution of other subs(VBA code is included in attached files.
I have a file called “Testing All Subs Relating To Setting Up Labor Model” that simulates another file that I need to complete every month. The simulated file consists of several spreadsheets. One spreadsheet is called Prior Month Payroll which currently consists of December’s payroll info. The second spreadsheet is called Current Month Payroll which currently house’s January data. The 3rd spreadsheet is called New Sheet which will house February’s data in an organized manner(The columns in February’s data are out of order compared to the Order of the Labor Model Column Index) so that the headings going across row A3 will have the same order as the headings in a column in the 4th spreadsheet called Labor Model Column Index. There are also 2 additional spreadsheets which contain December and January’s data in case they need to be pasted into the Prior and Current Month Payroll sheets to restore the file to it’s state before any of the subs were executed. This needs to be done before the subs can be run again. Also, all data in the “New Sheet” must be deleted before the subs can be run again.

I have written 4 subs that accomplish the following:
The main sub is called “OpenFebruaryPayrollForLaborSimulationAndPrepareLaborModelForCurrentMonth ()”. This sub deletes the data in the Prior Month File(December data), copies the current month data(January) into the Prior Month Payroll sheet. It then deletes the data in the Current Month Payroll(January) and copies the data(February) from sheet1 in the “February Payroll For Labor Model Simulation.xlsx” file after it opens the file, into the spreadsheet in the “Testing All Subs Related To Setting Up Labor Model” file.
It then calls 3 other subs that do the following:
FindCell () – Identifies the address of the first cell in the dynamic range on the spreadsheet and includes
it in a message box.
GetRange () – Which does the same thing for the last cell in the range.
FindAll_and_copy which cuts and pastes the February data to the “New Sheet” spreadsheet data in an organized manner so that the headings of the columns and related data going across will have the same order as the headings descending in a column in the 4th spreadsheet called Labor Model Column Index.
When I ran the main sub excluding(turning the call statement into a comment) for the FindAll_and_copy sub it works perfectly. If I simulate the Current Month Payroll with February’s data and run the FindAll_and_copy sub from the module before running the other subs, it works perfectly as well.
Once I try to run the “OpenFebruaryPayrollForLaborSimulationAndPrepareLaborModelForCurrentMonth ()” sub that calls all 3 subs, the FindAll_and_copy sub doesn’t run properly and goes into an indefinite loop.
I have included 3 files, 2 of which have been mentioned above. The third file is called Book2 – Testing FindAll_and_copy Isolated Testing. This file contains some of the files in the other files and is set up for the sole purpose to test the FindAll_and_copy sub independently of the other subs and prove it works. Again, each time you want to rerun the sub you must restore the sheets in the file (February data must in the Current Month Payroll sheet and the New Sheet must be empty). Run the sub from the Module.
If you run the sub from the “Workbook” then you will not need to load the February data into the Current Month Payroll sheet. This version of the sub will open the “February Payroll For Labor Model Simulation.xlsx” and paste the data in for you.
The “February Payroll For Labor Model Simulation.xlsx” needs to be closed each time after a sub involving it is run.
I would greatly appreciate it if anyone could explain why the FindAll_and_copy sub suddenly won’t work properly and has an infinite loop when working in conjunction with the other subs.
Thanks,
Phil

Viewing all articles
Browse latest Browse all 50070

Trending Articles