View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
CAMoore CAMoore is offline
external usenet poster
 
Posts: 11
Default reply

Sorry. I realize I went back and forth with ComboBox and ListBox. What I'm
trying to get working with RowSource right now is the ListBox. Here is the
code you last posted and I edited as per my Sheet and UserForm name, and it
didnt work. :( I'm using Excel 2007. Maybe I could email my spreadsheet to
you???

Private Sub UserForm2_Initialize()
'TextBox2.Value = Date
Dim lr As Long
Dim r As Range

lr = Sheets("Filenames").Cells(Rows.Count, "A").End(xlUp).Row
Set r = Range("A2:A" & lr)
Me.ListBox1.RowSource = "Filenames!" & r.Address
CommandButton1.SetFocus
End Sub
--
Other programming languages I''''ve used a Cobol, C++, Informix Database,
and Unix.


"FSt1" wrote:

hi
now i'm confused. you said you made a mistake about posted list box vs.
combo box so now i'm not sure which one we are working with. no matter. the
two work very similar in code.
you first try should have worked. I didn't test second try. not sure at this
point why it didn't work so i drug out my play form, put a list box(?) on it,
put my code in it and it loaded right up at initialization. here is my code
from my play form.
Private Sub UserForm_Initialize()
TextBox2.Value = Date
Dim lr As Long
Dim r As Range

lr = Sheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set r = Range("A2:A" & lr)
Me.ListBox1.RowSource = "Sheet2!" & r.Address
CommandButton1.SetFocus
End Sub

again, i use generic sheet names but the list box loaded with the list i
have on sheet 2. i doubled the list length and it loaded the new list. i
opened the form from sheet 1.
I did assume that your list was in column A. if not change the code in this
line to the column your list is in....
lr = Sheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row

works in xp.
regards
FSt1

"CAMoore" wrote:

I've tried two different ways in UserForm2_Initialization and neither one
worked :(

***********
** Try #1: **
***********
Public Sub UserForm2_Initialize()
ColCnt = ActiveSheet.UsedRange.Columns.Count
Set rng = ActiveSheet.UsedRange
Dim lr As Long
Dim r As Range

With ListBox1
.ColumnCount = ColCnt
.RowSource = rng.Address

cw = ""

For c = 1 To .ColumnCount
cw = cw & rng.Columns(c).Width & ";"
Next c

.ColumnWidths = cw
.ListIndex = 0
End With

lr = Sheets("FileNames").Cells(Rows.Count, "A").End(xlUp).Row
Set r = Range("A2:A" & lr)
Me.ListBox1.RowSource = "Filenames!" & r.Address

End Sub

***********
** Try #2: **
***********
Public Sub UserForm2_Initialize()
Dim rng As Range
Dim LastRow As Long

ThisWorkbook.Worksheets("Main").Activate

LastRow = Worksheets("Filenames").Range("A65536").End(xlUp). Row

With ThisWorkbook.Sheets("Filenames")
Set rng = .Range("A1:A" & LastRow)
End With

With ListBox1
For Each cell In rng.Cells
AddItem cell.Value
Next cell
End With

End Sub


--
Other programming languages I''''ve used a Cobol, C++, Informix Database,
and Unix.


"FSt1" wrote:

hi
accually that's userform2 initialazation code. listboxs and combobox don't
have an initialation evert. forms do.
try this....
Private Sub UserForm2_Initialize()
ColCnt = ActiveSheet.UsedRange.Columns.Count
Set Rng = ActiveSheet.UsedRange
dim lr as long
dim r as range

With ListBox1
.ColumnCount = ColCnt
.RowSource = Rng.Address
cw = ""

For c = 1 To .ColumnCount
cw = cw & Rng.Columns(c).Width & ";"
Next c

.ColumnWidths = cw
.ListIndex = 0
End With

lr = Sheets("FileNames").Cells(Rows.Count, "A").End(xlUp).Row
Set r = Range("A2:A" & lr)
Me.ComboBox1.rowsource = "Filenames!" & r.Address

End Sub

untested. watch for typos
regards
FSt1