How to use cel l(#,#) format when worksheet isn't active

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hello everyone.

I have a macro that fills a list box box based on the data which is in another combo box. The code is as follows:

Code:
Dim MyCell  Range
Dim wUD As Worksheet
Set wUD = sheets("User Data")
For Each MyCell In wUD.Range("z2:z" & Range("A65536").End(xlUp).Row)
         If Not MyCell = "" Then
         With iPeerGroup
            .AddItem (MyCell.Offset(0, -25).Value)
            .List(.ListCount - 1, 1) = MyCell.Offset(0, -24).Value
            .List(.ListCount - 1, 2) = Format(MyCell, "#,#")
                 End With
         End If
Next
This works fine, but only if the User Data sheet is active. I would like to to work regardless. Any ideas?
-Ilya
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Looks like you have an unqualified range. Not tested but try:

Code:
Dim MyCell  Range
Dim wUD As Worksheet
Set wUD = sheets("User Data")
For Each MyCell In wUD.Range("z2:z" & wUD.Range("A65536").End(xlUp).Row)
         If Not MyCell = "" Then
         With iPeerGroup
            .AddItem (MyCell.Offset(0, -25).Value)
            .List(.ListCount - 1, 1) = MyCell.Offset(0, -24).Value
            .List(.ListCount - 1, 2) = Format(MyCell, "#,#")
                 End With
         End If
Next
 
Upvote 0
Code:
Dim MyCell  Range
Dim wUD As Worksheet
Set wUD = sheets("User Data")
For Each MyCell In wUD.Range("z2:z" & [B][U][I][COLOR="Red"]wUD.[/COLOR][/I][/U][/B]Range("A65536").End(xlUp).Row)
         If Not MyCell = "" Then
         With iPeerGroup
            .AddItem (MyCell.Offset(0, -25).Value)
            .List(.ListCount - 1, 1) = MyCell.Offset(0, -24).Value
            .List(.ListCount - 1, 2) = Format(MyCell, "#,#")
                 End With
         End If
Next
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top