ComboBox list ends with 2 blank rows of data ???

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
622
Office Version
  1. 2011
Platform
  1. MacOS
Hi all,

I'm sure I'm missing something small, but does anyone know why I have to use "LastRow-2" in the code below to keep from ending up with 2 empty rows of data at the bottom of my ComboBox list?

I guess I would get it if I had to use LastRow-1 since the array might be putting in an extra line, because the array starts at 0. I'm just confused as to why I have to use -2.

Thanks in advance,
Mike

Code:
Sub UserForm_Initialize()

  Dim ws As Worksheet
  Dim i As Long
  Dim LastRow As Long
  
  Set ws = Sheets("DataSheet")

  LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
  
  Dim MyArray()
  ReDim MyArray(LastRow-2, 2) As Variant
  
  With Me.ComboBox1
    .SetFocus
    .Clear
    .ColumnHeads = False
    .ColumnCount = 3
    .ColumnWidths = "240;240;240"
    .Font.Size = 14
    For i = 0 To LastRow-2
      MyArray(i, 0) = ws.Cells(i + 2, 1)
      MyArray(i, 1) = ws.Cells(i + 2, 2)
      MyArray(i, 2) = ws.Cells(i + 2, 27)
    Next i
    .List = MyArray
  End With
  
End Sub
 
You're quite right, it should be
Code:
      Me.ComboBox1.List = Application.Index(ws.Range("[COLOR=#ff0000]A1[/COLOR]:AA" & LastRow).Value, Evaluate("row(2:" & LastRow & ")"), Array(1, 2, 27))
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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