Critical Problem - Error Populating Listbox With Named Range

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am encountering a critical hurdle in my project.

During my userform initialization code, I receive an error "Method 'Range' of object '_Global' failed" with the line in red red below. It is happening as I try to populate the contents of a listbox with the values of a named range "data_file_list".

Rich (BB code):
Private Sub UserForm_Initialize()
   
    Dim temp_ws As Worksheet
    Set temp_ws = wb_sched.Worksheets("temp_ws")
    
    mbEvents = False
    Application.ScreenUpdating = False
    
    With Me
        .Label1.BackColor = RGB(0, 126, 167)
        
        .uf1_tb1 = Format(dDate, "ddd mm/dd/yyyy h:mm AM/PM")
        .uf1_tb2 = Round(((lSize) * 0.001), 0)
        .uf1_tb3 = llastrow
        .uf1_tb4 = v1
        .uf1_tb1.ForeColor = RGB(0, 52, 89)
        .uf1_tb2.ForeColor = RGB(0, 52, 89)
        .uf1_tb3.ForeColor = RGB(0, 52, 89)
        .uf1_tb4.ForeColor = RGB(0, 52, 89)
        .uf1_tb1.Locked = True
        .uf1_tb2.Locked = True
        .uf1_tb3.Locked = True
        .uf1_tb4.Locked = True
        .cbtn_process.Enabled = False
        With .uf1_label10
            .Caption = "Clean data!"
            .ForeColor = RGB(255, 99, 71)
        End With
        
        With .uf1_listbox1
            .ForeColor = RGB(0, 52, 89)
            .Clear
            .ColumnCount = 2
            .ColumnWidths = "75;25"
            .List = Range("data_file_list").Value
            .ListStyle = fmListStylePlain
            .MultiSelect = fmMultiSelectMulti
            .Locked = True 'do not unlock until missing rentals are 0
        End With
        ....


The named range is created in the module below ...

Rich (BB code):
Sub GetUniques(ByVal temp_ws As Object)

    Dim sheetToFind As String
    'analyses data in schedule.csv determining range of dates within database and the number of records unique to those dates
    
    Dim d As Object             'scripting dictionary
    Dim c As Variant, i As Long
    Dim data_file_list As Name  'named range containing unique dates & record #s for use as listbox rowsource
    Dim cntr As Integer, pctCompl As Single, g As Long
    Dim lastdate As Long
    
    Application.ScreenUpdating = False
    'MsgBox wb_sched.Name
    
    With ws_sched
        Set d = CreateObject("Scripting.Dictionary")
        c = .Range("M1:M" & llastrow)

        msg1 = vbLf & "Contents: " & llastrow & " records." & vbLf & vbLf & "Analyzing raw data date range."
        uf1_create_wo1.F6E_msg13 = msg1
        For cntr = 1 To 100                 'progress bar incrementer
            For i = 1 To UBound(c, 1)       'loop through rows in source data Ubound = c = llastrow
                d(c(i, 1)) = 1
            Next i
            pctCompl = cntr                 'percentage complete = counter
            progress pctCompl
        Next cntr                           'progress bar incrementer

    End With

    sheetToFind = "temp_ws"                     'function variable
    If Not sheetExists(sheetToFind) Then        'does temp_ws already exist in schedule.csv? (function: sheetExists)
        Set temp_ws = wb_sched.Worksheets.Add   'add it if it doesn't
        temp_ws.Name = "temp_ws"                'and name it temp_ws
        Debug.Print "temp_ws created in schedule.csv        "
    End If

    Set temp_ws = Workbooks("schedule.csv").Worksheets("temp_ws")

    With temp_ws
    
        'create headers
        .Range("A1") = "CLASS DATE"
        .Range("B1") = "DATE SELECTION"
        .Range("C1") = "RECORDS"
        .Range("D1") = "FILE DATE"
        .Range("E1") = "SERIAL DATE"
        .Range("A2").Resize(d.count) = Application.Transpose(d.keys) 'paste the unique dates found in 'getuniques' function [GetUniques1]
        
        'populate columns
        v1 = WorksheetFunction.count(.Range("A:A")) 'count number of NUMERIC (date) values in column A of temp_ws
        For cntr = 1 To 100             'progress bar incrementer
            For i = 1 To v1             'loops from rows to v1
                .Range("B" & i + 1) = Format(.Range("A" & i + 1), "ddd dd-mmm") 'date format 1
                .Range("C" & i + 1) = WorksheetFunction.CountIf(ws_sched.Range("M:M"), .Range("A" & i + 1)) 'number of raw records for that date
                .Range("E" & i + 1) = .Range("A" & i + 1) 'serial date format
                .Range("D" & i + 1) = Format(.Range("E" & i + 1), "dd-mmm") 'date format 2
            Next i
            pctCompl = cntr             'percentage complete = counter
            progress pctCompl
        Next cntr                       'progress bar incrementer
        uf1_create_wo1.F6E_pframe.Visible = False
        uf1_create_wo1.F6E_per.Visible = False


        lastdate = .Range("A" & temp_ws.Range("A" & Rows.count).End(xlUp).row)
        ' create message for F6E
        msga = vbLf & "Contents: " & llastrow & " records." & vbLf & "Date range: " & Format(temp_ws.Range("A2"), "ddd mmm-dd") & " - " & Format(lastdate, "ddd mmm-dd")
        msgb = "Enhancing raw database."
        msgc = "                 Please wait ..."
        msg1 = msga & vbLf & msgb & msgc
        uf1_create_wo1.F6E_msg13 = msg1
        
        'create named range
        Workbooks("Sports17.xlsm").Names.Add Name:="data_file_list", RefersTo:="=OFFSET('[" & wb_sched.Name & "]" & temp_ws.Name & "'!$B$1,1,0,count('[" & wb_sched.Name & "]" & temp_ws.Name & "'!$A:$A),2)"
    End With
    Application.ScreenUpdating = True
    
End Sub

I am not sure what this error means. Perhaps it means the range is empty. It shouldn't be as the source is populated. I have stepped through the code from the command button that prompts the userform initialization which includes the module for defining the range.

I will be super grateful for anyone able to help me overcome this error.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What happens with

.
Code:
List = temp_ws.Range("data_file_list").Value

or

Code:
.List = temp_ws.Range("data_file_list")
 
Upvote 0
Where exactly is the named range located?
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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