I can't populate data on userform if the sheet is not active

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
I search for way if I run the userform from any sheet should populate data on userform
now I run userform from sheet2 and should brings data from sheet1 . so far the sheet1 should be activate to poulate data otherwise doesn't show anything
here is the code
VBA Code:
Sub Due_Date()

Dim i, lastrow As Long
Application.ScreenUpdating = False
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow
    If Cells(i, 1) - Date >= 2 And Cells(i, 4) - Date <= 20 Then
        With UserForm1.ListBox1
            .AddItem
            .List(.ListCount - 1, 1) = Sheet1.Cells(i, 2).Text
            .List(.ListCount - 1, 2) = Sheet1.Cells(i, 3).Text
            .List(.ListCount - 1, 3) = Sheet1.Cells(i, 4).Text
        End With
        
    End If
Next
Application.ScreenUpdating = True
UserForm1.Show
End Sub
any idea guys ?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You need to qualify the cells on this line with the sheet name as you have done with the rest of the code.
VBA Code:
If Cells(i, 1) - Date >= 2 And Cells(i, 4) - Date <= 20 Then
 
Upvote 0
Hi,
if you mean like this
VBA Code:
If Sheet1.Cells(i, 1) - Date >= 2 And Sheet1.Cells(i, 4) - Date <= 20 Then
also doesn't work .
 
Upvote 0
In what way doesn't it work?
 
Upvote 0
when I run the userform from sheet2 is active and the data are existed in sheet1 then should populate data when run the userform

in one case populate the data if I have select sheet1 and run the userform from it( sheet1 must be activated). otherwise doesn't show data when run userform.
 
Upvote 0
Is Sheet1 the actual name of the sheet (as seen on the tab) or is it the codename?
 
Upvote 0
Solution
that's right ,not the same sheet name ,seem to contains a little space, just question but why doesn't show subscript out of range if is not matched the sheet name?
 
Upvote 0
You are using the Sheet Codename, so it must exist, but maybe not the sheet you think it is.
 
Upvote 0
this problem was confused & strange !
thanks for your solution .;)
 
Upvote 0
My pleasure & glad it's sorted.
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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