Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to point to different listboxes 1

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Why is this not working? Im working in Excel 2003.

I thought I could set it to a different listbox depending on the value of my page variable?

Do I need to somehow set my dynamic variable as a listbox control?

Code:
Dim logfilepath As String
Dim dynamicbox As String

Select Case page

Case Is = 1

dynamicbox = ListBox1

Case Is = 2

dynamicbox = ListBox2

Case Is = 3

dynamicbox = ListBox3

Case Is = 4

dynamicbox = ListBox4

Case Is = 5

dynamicbox = ListBox5

End Select

logfilepath = "k:\handläggningsstöd\Ärenden\logg-för-" & logfilename & ".ini"

If FileOrDirExists(logfilepath) Then

MsgBox ("Det finns en LOGGFIL!")

UserForm1.dynamicbox.Clear

Dim lFile As Long
Dim varjerad As String

lFile = FreeFile()
     
    Open logfilepath For Input As lFile
     
    While Not EOF(lFile)
         
         
        Line Input #lFile, varjerad
         
        UserForm1.dynamicbox.AddItem varjerad
         
         
    Wend
     
     
    Close lFile

Else

MsgBox ("DET FINNS ABSOLUT INGEN LOGG FIL")

End If
 


hi,

Are you trying to assign an Object to a string? That will not work!

Also, exactly where is this code?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
kalle82,

I think you are trying to address the listboxes using the line:
Code:
UserForm1.dynamicbox.AddItem varjerad

Try this instead:
Code:
UserForm1.controls(dynamicbox).AddItem varjerad

You will also need to put quotes around the names. In other words, change the line:
Code:
dynamicbox = ListBox1

to:
Code:
dynamicbox = "ListBox1"

etc...

Tony
 




if that's the case then the entire select case statement can be replaced by one statement...
Code:
dynamicbox = "ListBox" & page

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,

True. He could also address the LBs directly by dimming dynamicbox as a control and handling the issue that way, but I thought I'd keep it simple and just address the main issue.

kalle82,
If you follow Skip's advice, you need to make sure that page is dimmed as a string, or use:

Code:
dynamicbox = "ListBox" & trim(str(page))

Tony
 


If the variable, page, is an integer data type (which the OP's Select Case statement implies), then no trimming is required, and the vb complier does the string convertion

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hey guys! Wow thanks I love shorten my code, and doing it your way will most certainly be more effective!

Thanks again, for going the extra mile on this post!
 
Skip,

I've just done a quick test, and it looks like you are right about not needing the Trim(). But this is a bit odd, becasue here is a quote from the help on the Str function:
Str Function Example
This example uses the Str function to return a string representation of a number. When a number is converted to a string, a leading space is always reserved for its sign.

Dim MyString
MyString = Str(459) ' Returns " 459".

I used the Trim() to avoid the leading space I was expecting to be there.

As far as the explicit conversion is concerned, you are right, the compiler would do it, but I just prefer to be explicit - it feels safer somehow.

Tony
 


Try using the CStr function, rather than the Str function.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top