Hi Excel Experts,
I am struggling with how to reference a Worksheet in VBA and hope someone can help me.
I have a macro enable book where the code opens a different Workbook and copies the first worksheet to the macro book.
Once the code has done its thing I need to sort the results.
My sort code works so long as the worksheet containing the data to sort is active. Here's the old code which worked fine although I don't know if the worksheet with the data was active or not. The old code is messy and difficult for me to completely trace.
To sort the data I created 2 Ranges that use a variable (count).
I can get the code to function as it should so long as I activate the sheet 'IIP' at the top of my code. But my goal is to do away with using 'active' in the code.
Any ideas?
Thanks in advance to anyone who answers.
Matt
I am struggling with how to reference a Worksheet in VBA and hope someone can help me.
I have a macro enable book where the code opens a different Workbook and copies the first worksheet to the macro book.
Once the code has done its thing I need to sort the results.
I had a 'sort' code working in an earlier version of the macro workbook but it was inefficient code and I'm trying to eliminate all instances of 'select', 'copy', paste' and 'activate'.
My sort code works so long as the worksheet containing the data to sort is active. Here's the old code which worked fine although I don't know if the worksheet with the data was active or not. The old code is messy and difficult for me to completely trace.
To sort the data I created 2 Ranges that use a variable (count).
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
'rgSort is the 7 columns containing the data
'rgCol is the column that gets sorted from smallest to largest
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][LEFT][COLOR=#222222][FONT=Verdana] Dim rgSort As Range, rgCol As Range [/FONT][/COLOR][/LEFT][/FONT]
[/FONT][FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Set rgSort = Worksheets("IIP").Range(Cells(2, 2), Cells(count, 7))
Set rgCol = Worksheets("IIP").Range(Cells(2, 7), Cells(count, 7))
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] rgSort.Sort Key1:=rgCol, order1:=xlAscending
[/FONT] 'When I run this piece of code and the sheet 'IIP' is not active I get a "Runtime error '1004' Application-defined or object defined error."
'I've tried the following code where I set 'wb' as the macro enabled workbook and shIIP is the code name for the worksheet 'IIP'.
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Set rgSort = wb.shIIP.Range(shIIP.Cells(2, 2), shIIP.Cells(count, 7))
Set rgCol = wb.shIIP.Range(shIIP.Cells(2, 7), shIIP.Cells(count, 7))
'And I get a Run-time error '438' Object doesn't support this property or method.
I've also tried not referencing the workbook (wb) to no avail.
[/FONT][FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Set rgSort = shIIP.Range(shIIP.Cells(2, 2), shIIP.Cells(count, 7))
Set rgCol = shIIP.Range(shIIP.Cells(2, 7), shIIP.Cells(count, 7))[/FONT]
[/FONT]
I can get the code to function as it should so long as I activate the sheet 'IIP' at the top of my code. But my goal is to do away with using 'active' in the code.
Any ideas?
Thanks in advance to anyone who answers.
Matt