Run time error 1004 advice please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,940
Office Version
  1. 2007
Platform
  1. Windows
Hi.
On my useform i select a name from a list & then press the transfer button.
Now on my worksheet todays date has been entered in the cell which refers to the name i had selected on the userform.

Normally the list on the userform has many names so this is a first for me because now there are two names.
So whats happens is,
I select one of the names & press the transfer button.
On my worksheet the date is placed next to that name so far all is ok.

So now there is only one name left in the list
I select this name & press the transfer button & i see the updated message but then i get the run time error message because the list doesnt have any names to show.

Can we now at this stage show maybe a msgbox saying No Names In List etc as opposed to the run time error beeing shown.

I think the issue is related to the red text shown but not sure how to proceed with the fix.

Code:
Private Sub UserForm_Initialize()Dim cl As Range
Dim rng As Range
Dim lstrw As Long
Dim lastrow As Long
Dim Lastrowa As Long
Dim cntr As Integer
Load PostageTransferSheet
'==============================================================================================
Application.ScreenUpdating = False
lastrow = Sheets("POSTAGE").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("POSTAGE").Cells(8, 2).Resize(lastrow - 7).Copy Sheets("POSTAGE").Cells(1, 12)
Lastrowa = Sheets("POSTAGE").Cells(Rows.Count, "L").End(xlUp).Row
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Sort key1:=Cells(1, 12).Resize(Lastrowa), order1:=xlAscending, Header:=xlNo
CustomerSearchBox.List = Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Value
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Clear
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cntr = 1
With Sheets("POSTAGE")
    lstrw = .Range("B65536").End(xlUp).Row
    Set rng = .Range("B8:B" & lstrw)
        For Each cl In rng
            If cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = cl.Value: cntr = cntr + 1
        Next
     .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
[COLOR=#ff0000]      NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value[/COLOR]
     .Range("L1:L" & cntr - 1).Clear
     TextBox2.SetFocus
End With
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Application.ScreenUpdating = True


TextBox1.Value = Format(CDbl(Date), "dd/mm/yyyy")
TextBox7.Value = Format(CDbl(Date), "dd/mm/yyyy")
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
If cntr > 1 Then
     .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
      NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value
     .Range("L1:L" & cntr - 1).Clear
Else
   MsgBox " No names left"
End If
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,940
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I assume i did this correct.

Replace this
Code:
cntr = 1
With Sheets("POSTAGE")
    lstrw = .Range("B65536").End(xlUp).Row
    Set rng = .Range("B8:B" & lstrw)
        For Each cl In rng
            If cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = cl.Value: cntr = cntr + 1
        Next
     .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
      NameForDateEntryBox.List = .Range("L1:L" & cntr - 0).Value
     .Range("L1:L" & cntr - 1).Clear
     TextBox2.SetFocus
End With

For this

Code:
If cntr > 1 Then     .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
      NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value
     .Range("L1:L" & cntr - 1).Clear
Else
   MsgBox " No names left"
End If

When the form tries to open i see a error message,
Compile erroe, Invalid or unqualified reference.

When i debug i see this in yellow

Code:
.Range("L1:L" & cntr - 1).Sort key1:=[COLOR=#ff0000].Range[/COLOR]("L1"), order1:=xlAscending, Header:=xlNo
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
Nope, it should be like
Code:
With Sheets("POSTAGE")
    lstrw = .Range("B65536").End(xlUp).Row
    Set Rng = .Range("B8:B" & lstrw)
        For Each Cl In Rng
            If Cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = Cl.Value: cntr = cntr + 1
        Next
         If cntr > 1 Then
              .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
               NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value
              .Range("L1:L" & cntr - 1).Clear
         Else
            MsgBox " No names left"
         End If
     TextBox2.SetFocus
End With
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,940
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

Hmm stil an issue for me.

I put some extra of the code down to show what i have and where.

Run time error 381
Could not set the list property,Invalid property array index.
I debug and see the below in yellow

Code:
Application.ScreenUpdating = Falselastrow = Sheets("POSTAGE").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("POSTAGE").Cells(8, 2).Resize(lastrow - 7).Copy Sheets("POSTAGE").Cells(1, 12)
Lastrowa = Sheets("POSTAGE").Cells(Rows.Count, "L").End(xlUp).Row
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Sort key1:=Cells(1, 12).Resize(Lastrowa), order1:=xlAscending, Header:=xlNo
CustomerSearchBox.List = Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Value
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Clear
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cntr = 1
With Sheets("POSTAGE")
    lstrw = .Range("B65536").End(xlUp).Row
    Set rng = .Range("B8:B" & lstrw)
        For Each cl In rng
            If cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = cl.Value: cntr = cntr + 1
        Next
         If cntr > 1 Then
              .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
               [COLOR=#ff0000]NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value[/COLOR]
              .Range("L1:L" & cntr - 1).Clear
         Else
            MsgBox " NO NAMES LEFT IN POSTAGE LIST"
         End If
     TextBox2.SetFocus
End With
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Application.ScreenUpdating = True


TextBox1.Value = Format(CDbl(Date), "dd/mm/yyyy")
TextBox7.Value = Format(CDbl(Date), "dd/mm/yyyy")
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
   cntr = 1
   With Sheets("POSTAGE")
      lstrw = .Range("B65536").End(xlUp).Row
      Set Rng = .Range("B8:B" & lstrw)
      For Each Cl In Rng
         If Cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = Cl.Value: cntr = cntr + 1
      Next
      Select Case cntr
         Case 0
            MsgBox " NO NAMES LEFT IN POSTAGE LIST"
         Case 1
            NameForDateEntryBox.AddItem .Range("L1").Value
            .Range("L1").Clear
         Case Else
            .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
            NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value
            .Range("L1:L" & cntr - 1).Clear
      End Select
      TextBox2.SetFocus
   End With
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,940
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

Hi,
Exactly the same line is in yellow
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
Oops, it should be

Rich (BB code):
   cntr = 1
   With Sheets("POSTAGE")
      lstrw = .Range("B65536").End(xlUp).Row
      Set Rng = .Range("B8:B" & lstrw)
      For Each Cl In Rng
         If Cl.Offset(0, 5).Value = "" Then Sheets("POSTAGE").Range("L" & cntr).Value = Cl.Value: cntr = cntr + 1
      Next
      Select Case cntr
         Case 1
            MsgBox " NO NAMES LEFT IN POSTAGE LIST"
         Case 2
            NameForDateEntryBox.AddItem .Range("L1").Value
            .Range("L1").Clear
         Case Else
            .Range("L1:L" & cntr - 1).Sort key1:=.Range("L1"), order1:=xlAscending, Header:=xlNo
            NameForDateEntryBox.List = .Range("L1:L" & cntr - 1).Value
            .Range("L1:L" & cntr - 1).Clear
      End Select
      TextBox2.SetFocus
   End With
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,940
Office Version
  1. 2007
Platform
  1. Windows
Nope.

What that now does is as below

Two names in list

Tom Jones
Eric Hall

I select Eric Hall & see the updated message.

I then go back to hopefully do the same with Tom Jones but what i now see is,
Tom Jones
Tom Jones
Eric Hall
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
Is that in the listbox, or on the sheet, or both?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,126
Messages
5,640,274
Members
417,133
Latest member
caaronh85

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
Top