Opening a userform on a different sheet than the reference data used in the initialize process

Kmanbozzy

New Member
Joined
Apr 18, 2016
Messages
18
Here is my issue. When i open my userform on any sheet other than the sheet "Data Entry" or sheet3 in my case, my initialization fails because i reference a dynamic range on sheet3. I found away around it by selecting sheet3 at the beginning and jumping back to the activesheet at the end. but i want to eliminate using select and actual names of the sheet, hence me using sheet3.

it has to be the way im referring to the range when im away from sheet3

this will not work when away from sheet3 error is at the m= line
Code:
Private Sub UserForm_Initialize()Dim sh As Worksheet
Set sh = Sheet3


h = 16  'this is my method of lastrow i use to exclude any empty cells in the range it returns the last row
Do Until m = 1048576
m = sh.Range(Cells(h, 34), Cells(h, 34)).End(xlDown).Row
    If m = 1048576 Then
        Exit Do
    End If
h = sh.Range(Cells(h, 34), Cells(h, 34)).End(xlDown).Row
Loop


rng = sh.Range(Cells(16, 34), Cells(h, 34)).Address


With sh
         ListBox1.RowSource = "'" & .name & "'!" & rng
End With
End Sub

this code works, but has the select in use

Code:
Private Sub UserForm_Initialize()Dim sh As Worksheet
Dim ws As Worksheet
Dim rng As Range
Application.ScreenUpdating = False


Set sh = Sheet3
Set ws = ActiveSheet
sh.Select


h = 16   
Do Until m = 1048576
m = sh.Range(Cells(h, 34), Cells(h, 34)).End(xlDown).Row
    If m = 1048576 Then
        Exit Do
    End If
h = sh.Range(Cells(h, 34), Cells(h, 34)).End(xlDown).Row
Loop


rng = sh.Range(Cells(16, 34), Cells(h, 34)).Address


With sh
         ListBox1.RowSource = "'" & .name & "'!" & rng
End With
ws.Select
Application.ScreenUpdating = True
End Sub

help please?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Please excuse the Dim sh as worksheet right after the sub, its correct in my actual code, that is not the problem
 
Upvote 0
Try
Code:
Private Sub UserForm_Initialize()
   Dim sh As Worksheet
   Set sh = Sheet3
   
   With sh
      h = 16  'this is my method of lastrow i use to exclude any empty cells in the range it returns the last row
      Do Until m = 1048576
         m = .Range(.Cells(h, 34), .Cells(h, 34)).End(xlDown).Row
         If m = 1048576 Then Exit Do
         h = .Range(.Cells(h, 34), .Cells(h, 34)).End(xlDown).Row
      Loop
      
      
      Rng = .Range(.Cells(16, 34), .Cells(h, 34)).Address
      
      
      ListBox1.RowSource = "'" & .Name & "'!" & Rng
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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