I'm trying to use a button in one workbook to open another Excel file, copy data from that file into an array, and paste some of the array data back into the original file (with the button) but on a different sheet than the sheet where the button is located. In the past, I've had no problems with this. I was just "updated" to Excel 2010 though from 2003 and things don't seem to work as they always have. Anyway, here are the basic steps:
1. Click button in Workbook A on Sheet 1
No problems with button activating macro in this step
2. VBA opens Workbook B (only one sheet) and reads all the data into an array
No problems with this step
3. VBA switches back to Wrkbk A and selects Sheet 2
I was using: Windows(<i>Wrkbk A</i>).Activate
Sheets(<i>Sheet 2</i>)).Select
I had problems in the next step though, so I switched this to:
Windows(<i>Wrkbk A</i>).Activate
ActiveWorkbook.Sheets(<i>Sheet 2</i>)).Activate
This seems to be working alright.
4. Clear old data from Sheet 2
I was using a Range().ClearContents command but it was clearing the data in the specified range on Sheet <b>_1_</b>, not Sheet 2.
So I switched to: Sheets(<i>Sheet 2</i>).Range(<data range>).ClearContents
This seems to be working alright.
5. VBA goes through the array and all records meeting a certain criterion have their data written to Wrkbk A, Sheet 2.
At first, as I cycled through the array, I was trying to write to Sheet 2 using a series of Cells(<i>Row, Column</i>).Value = DataArray(I).<i>parameter</i> statements. Even though I'd selected Sheet 2 though, it was still writing to Sheet 1.
So, I put in another 'ActiveWorkbook.Sheets(<i>Sheet 2</i>)).Activate' statement.
This didn't help though. So I changed all the write statements to have the form:
ActiveSheet.Cells(<i>Row, Column</i>).Value = DataArray(I).<i>parameter</i>
This now seems to be working (writing to Sheet 2), but it is INCREDIBLY slow. Each value takes about two seconds to write out. Since we're talking about somewhere in the neighborhood of 700,000 values to be written out, this is not acceptable if I want to finish before I retire (I'm 37).
So, I have two questions:
(1) Why does it seem that Excel isn't keeping track of the active sheet like I remember it doing in Excel 2003? Did something change? My memory is pretty good and I never had to continually specify the active sheet if I'd already done it once. I looked at a script I wrote a couple weeks ago to do a similar process and didn't have to fuss at all with specifying active sheets anywhere.
(2) Why is the write process from the array to the worksheet so slow?
Any advice would be fantastic. Thanks everyone.
-Sean
1. Click button in Workbook A on Sheet 1
No problems with button activating macro in this step
2. VBA opens Workbook B (only one sheet) and reads all the data into an array
No problems with this step
3. VBA switches back to Wrkbk A and selects Sheet 2
I was using: Windows(<i>Wrkbk A</i>).Activate
Sheets(<i>Sheet 2</i>)).Select
I had problems in the next step though, so I switched this to:
Windows(<i>Wrkbk A</i>).Activate
ActiveWorkbook.Sheets(<i>Sheet 2</i>)).Activate
This seems to be working alright.
4. Clear old data from Sheet 2
I was using a Range().ClearContents command but it was clearing the data in the specified range on Sheet <b>_1_</b>, not Sheet 2.
So I switched to: Sheets(<i>Sheet 2</i>).Range(<data range>).ClearContents
This seems to be working alright.
5. VBA goes through the array and all records meeting a certain criterion have their data written to Wrkbk A, Sheet 2.
At first, as I cycled through the array, I was trying to write to Sheet 2 using a series of Cells(<i>Row, Column</i>).Value = DataArray(I).<i>parameter</i> statements. Even though I'd selected Sheet 2 though, it was still writing to Sheet 1.
So, I put in another 'ActiveWorkbook.Sheets(<i>Sheet 2</i>)).Activate' statement.
This didn't help though. So I changed all the write statements to have the form:
ActiveSheet.Cells(<i>Row, Column</i>).Value = DataArray(I).<i>parameter</i>
This now seems to be working (writing to Sheet 2), but it is INCREDIBLY slow. Each value takes about two seconds to write out. Since we're talking about somewhere in the neighborhood of 700,000 values to be written out, this is not acceptable if I want to finish before I retire (I'm 37).
So, I have two questions:
(1) Why does it seem that Excel isn't keeping track of the active sheet like I remember it doing in Excel 2003? Did something change? My memory is pretty good and I never had to continually specify the active sheet if I'd already done it once. I looked at a script I wrote a couple weeks ago to do a similar process and didn't have to fuss at all with specifying active sheets anywhere.
(2) Why is the write process from the array to the worksheet so slow?
Any advice would be fantastic. Thanks everyone.
-Sean
Last edited: