I have a spreadsheet that processes data returned via a third-party query add-in to Excel. The numbers of records returned will vary depending on selection parameters set by the user. The sheets containing the query data are consolidated onto an interim results page. Then, to present the analysis results in the format needed by the user, the interim results sheet contents are copied to another sheet that is subtotalled and formatted. The contents of this sheet then get pasted to a front sheet that the user can either view or print.
I need to determine the the numbers of rows of data returned by the queries so I can copy and paste various ranges of data between sheets. To complicate matters, I can't reference the query data directly as the refresh process deletes rows in the sheets that it returns data into, and this causes #REF! error in any other sheets that made direct reference the returned data. Hence all references to the query data have to be indirect using the INDIRECT function.
I've used several diferent mehods of determining the number of rows of data returned.
As the queries return the data into named ranges for each column of data, I have used the ListNames method to return the details of the ranges to a control sheet once the queries have completed. From this I can generate text strings corresponding to the ranges I need to manipulate.
I have used the COUNTA function to count the number of non-blank cells in a range (the size of which I know will always exceed the number of rows of data retutrned).
I get my macro to select the last row in the sheet containing data, and use a statement of the form
rownum - activecell.row
to get the row number of the last row containing data.
I've even constructed a loop to step down a column of data until a blank cell is reached and counting how many non-blank cells it finds before the blank one.
Each method works fine if I single-shot the macro using Debug and the F8 key. However, if I run the macro from a button on the spreadsheet, or by using F5 from the VB editor, the macro fails as it picks up incorrect ranges, usually the ranges from the previous refresh of data. When I check back on the control sheet that calculates all the ranges, though, they are correct!! It is though Excel is multi-threading and the thread updating the spreadsheet lags behind the execution thread of the macro. By the time I get to check the spreadsheet contents, they're all ok, except for the results.
Has anyone else seen this type of problem, and if so, have they discovered any solutions.
I need to determine the the numbers of rows of data returned by the queries so I can copy and paste various ranges of data between sheets. To complicate matters, I can't reference the query data directly as the refresh process deletes rows in the sheets that it returns data into, and this causes #REF! error in any other sheets that made direct reference the returned data. Hence all references to the query data have to be indirect using the INDIRECT function.
I've used several diferent mehods of determining the number of rows of data returned.
As the queries return the data into named ranges for each column of data, I have used the ListNames method to return the details of the ranges to a control sheet once the queries have completed. From this I can generate text strings corresponding to the ranges I need to manipulate.
I have used the COUNTA function to count the number of non-blank cells in a range (the size of which I know will always exceed the number of rows of data retutrned).
I get my macro to select the last row in the sheet containing data, and use a statement of the form
rownum - activecell.row
to get the row number of the last row containing data.
I've even constructed a loop to step down a column of data until a blank cell is reached and counting how many non-blank cells it finds before the blank one.
Each method works fine if I single-shot the macro using Debug and the F8 key. However, if I run the macro from a button on the spreadsheet, or by using F5 from the VB editor, the macro fails as it picks up incorrect ranges, usually the ranges from the previous refresh of data. When I check back on the control sheet that calculates all the ranges, though, they are correct!! It is though Excel is multi-threading and the thread updating the spreadsheet lags behind the execution thread of the macro. By the time I get to check the spreadsheet contents, they're all ok, except for the results.
Has anyone else seen this type of problem, and if so, have they discovered any solutions.