Run-time error '1004' - At least two rows of source data

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
173
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm using the below code which creates a unique list of names from a sheet which holds various names. It works fine, except when the data only contains the one name, which can sometimes happen. I get the following error message: "Run-time error '1004': This command requires at least two rows of source data."

Can some one tell me if there is any way to ge around this error?

When I hit 'Debug' the below code in red is hi-lighted:

Code:
Option Explicit
Private Sub Worksheet_SelectionChange _
(ByVal Target As Range)
'This macro creates the dynamic drop-down list of Clients/FM in the user form
Dim r As Long
Dim wsL As Worksheet
Dim lLastRow As Long
 
  'Ignore selection of multiple cells
  If Target.Count > 1 Then Exit Sub
 
  'Ensure that we need to register the change
  If Target.Column = 1 And Target.Row > 2 Then
    Set wsL = Sheets("NameLists")
 
    'Clear the old list
    wsL.Range("A1").CurrentRegion.ClearContents
 
    'Get the last row of clients
    lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
 
    'Create a unique list of clients in the "List" sheet
  [COLOR=red]  Range(Cells(2, 1), Cells(lLastRow, 1)) _[/COLOR]
[COLOR=red]   .AdvancedFilter Action:=xlFilterCopy, _[/COLOR]
[COLOR=red]   CopyToRange:=wsL.Range("A1"), Unique:=True[/COLOR]
    
    'Sort clients alphabetically
    r = wsL.Cells(Rows.Count, 1).End(xlUp).Row
 
    wsL.Range(wsL.Cells(1, 1), wsL.Cells(r, 1)).Sort _
    Key1:=wsL.Range("A1"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
 
    'Dynamically create a named range to capture the client list
    ActiveWorkbook.Names.Add Name:="NameDataValList", _
    RefersToR1C1:="=Lists!R1C1:R" & r & "C1"
 
    'Create a drop down, populated with the unie client list
    With Target.Validation
      .Delete
      .Add Type:=xlValidateList, _
      AlertStyle:=xlValidAlertStop, _
      Operator:=xlBetween, _
      Formula1:="=NameDataValList"
      .IgnoreBlank = True
      .InCellDropdown = True
      .ShowError = False
    End With
 
    End If
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Perhaps like this

Code:
If lLastRow <> 2 Then
    Range(Cells(2, 1), Cells(lLastRow, 1)) _
       .AdvancedFilter Action:=xlFilterCopy, _
       CopyToRange:=wsL.Range("A1"), Unique:=True
Else
    wsL.Range("A1").Value = Cells(2, 1).Value
End If
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,287
Members
452,902
Latest member
Knuddeluff

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