Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default listbox and textbox problem

I don't understand why this is happening. I have a form with a listbox and
four textboxex. when an value is selected in the listbox, the 4 textboxes
need to show data (vehicle information) from the same row as the selected
value.
it works.... sort of. if I have the vehicle information sheet selected,
everything works fine, but if any other sheet is selected, the values for the
text boxes come from there. here is the code i have in the userform:


Private Sub CommandButton1_Click()
TextBox1 = Clear
TextBox2 = Clear
TextBox3 = Clear
TextBox4 = Clear
AddTicket.Hide
End Sub

Private Sub CommandButton2_Click()
Call Add_Ticket
End Sub

Private Sub ListBox1_Change()
With Sheets("Sheet2").Range("A:A,E:E")
Dim lastrow As Long, a As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For a = lastrow To 2 Step -1
If Cells(a, 1).Text = ListBox1.Text Then
If Cells(a, 2).Text = "" Then
TextBox1.Text = "No Info Availiable"
Else: TextBox1.Text = Cells(a, 2).Text
End If
If Cells(a, 3).Text = "" Then
TextBox2.Text = "No Info Availiable"
Else: TextBox2.Text = Cells(a, 3).Text
End If
If Cells(a, 4).Text = "" Then
TextBox3.Text = "No Info Availiable"
Else: TextBox3.Text = Cells(a, 4).Text
End If
If Cells(a, 5).Text = "" Then
TextBox4.Text = "No Info Availiable"
Else: TextBox4.Text = Cells(a, 5).Text
End If
End If
Next
End With
End Sub

Private Sub UserForm_Initialize()
Dim ListCellreasons As Range
Dim listcellofficers As Range
Dim listcellowner As Range
With Sheets("sheet2").Range("A:A,C:C")
For Each listcellowner In Range("Owner")
If listcellowner.Value < "" Then
ListBox1.AddItem listcellowner.Value
End If
Next
For Each listcellofficers In Range("officers")
If listcellofficers.Value < "" Then
ListBox2.AddItem listcellofficers.Value
End If
Next
For Each ListCellreasons In Range("reasons")
If ListCellreasons.Value < "" Then
ListBox3.AddItem ListCellreasons.Value
End If
Next
End With
End Sub

TIA
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default listbox and textbox problem

You with structure doesn't do anything unless you precede object to be
qualified with a period

Worksheets("Sheet2").Activate
With Worksheets("Sheet1")
msgbox cells(1,1).Value
End with

will display A1 from Sheet2 (since sheet2 is the active sheet) - your WITH
statement is doing nothing

Worksheets("sheet2").Activate
With Worksheets("Sheet1")
msgbox .Cells(1,1).Value
End With

now displays the value of A1 in Sheet1.

fix your code to utilize your WITH statement or otherwise qualify your cell
references with a specific sheet reference.

--
Regards,
Tom Ogilvy

"jeramie" wrote:

I don't understand why this is happening. I have a form with a listbox and
four textboxex. when an value is selected in the listbox, the 4 textboxes
need to show data (vehicle information) from the same row as the selected
value.
it works.... sort of. if I have the vehicle information sheet selected,
everything works fine, but if any other sheet is selected, the values for the
text boxes come from there. here is the code i have in the userform:


Private Sub CommandButton1_Click()
TextBox1 = Clear
TextBox2 = Clear
TextBox3 = Clear
TextBox4 = Clear
AddTicket.Hide
End Sub

Private Sub CommandButton2_Click()
Call Add_Ticket
End Sub

Private Sub ListBox1_Change()
With Sheets("Sheet2").Range("A:A,E:E")
Dim lastrow As Long, a As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For a = lastrow To 2 Step -1
If Cells(a, 1).Text = ListBox1.Text Then
If Cells(a, 2).Text = "" Then
TextBox1.Text = "No Info Availiable"
Else: TextBox1.Text = Cells(a, 2).Text
End If
If Cells(a, 3).Text = "" Then
TextBox2.Text = "No Info Availiable"
Else: TextBox2.Text = Cells(a, 3).Text
End If
If Cells(a, 4).Text = "" Then
TextBox3.Text = "No Info Availiable"
Else: TextBox3.Text = Cells(a, 4).Text
End If
If Cells(a, 5).Text = "" Then
TextBox4.Text = "No Info Availiable"
Else: TextBox4.Text = Cells(a, 5).Text
End If
End If
Next
End With
End Sub

Private Sub UserForm_Initialize()
Dim ListCellreasons As Range
Dim listcellofficers As Range
Dim listcellowner As Range
With Sheets("sheet2").Range("A:A,C:C")
For Each listcellowner In Range("Owner")
If listcellowner.Value < "" Then
ListBox1.AddItem listcellowner.Value
End If
Next
For Each listcellofficers In Range("officers")
If listcellofficers.Value < "" Then
ListBox2.AddItem listcellofficers.Value
End If
Next
For Each ListCellreasons In Range("reasons")
If ListCellreasons.Value < "" Then
ListBox3.AddItem ListCellreasons.Value
End If
Next
End With
End Sub

TIA

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default listbox and textbox problem

All of your Cells references are unqualified - that is, you don't specify
which sheet Cells belongs to and it assumes the active sheet. You have a
With block, but it doesn't look you're using anything related to
Range("A:A,E:E"). Try putting a period before each Cells(...) and see if
that does it for you.


--
Dick Kusleika
MS MVP - Excel
www.dailydoseofexcel.com

jeramie wrote:
I don't understand why this is happening. I have a form with a
listbox and four textboxex. when an value is selected in the listbox,
the 4 textboxes need to show data (vehicle information) from the same
row as the selected value.
it works.... sort of. if I have the vehicle information sheet
selected, everything works fine, but if any other sheet is selected,
the values for the text boxes come from there. here is the code i
have in the userform:


Private Sub CommandButton1_Click()
TextBox1 = Clear
TextBox2 = Clear
TextBox3 = Clear
TextBox4 = Clear
AddTicket.Hide
End Sub

Private Sub CommandButton2_Click()
Call Add_Ticket
End Sub

Private Sub ListBox1_Change()
With Sheets("Sheet2").Range("A:A,E:E")
Dim lastrow As Long, a As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For a = lastrow To 2 Step -1
If Cells(a, 1).Text = ListBox1.Text Then
If Cells(a, 2).Text = "" Then
TextBox1.Text = "No Info Availiable"
Else: TextBox1.Text = Cells(a, 2).Text
End If
If Cells(a, 3).Text = "" Then
TextBox2.Text = "No Info Availiable"
Else: TextBox2.Text = Cells(a, 3).Text
End If
If Cells(a, 4).Text = "" Then
TextBox3.Text = "No Info Availiable"
Else: TextBox3.Text = Cells(a, 4).Text
End If
If Cells(a, 5).Text = "" Then
TextBox4.Text = "No Info Availiable"
Else: TextBox4.Text = Cells(a, 5).Text
End If
End If
Next
End With
End Sub

Private Sub UserForm_Initialize()
Dim ListCellreasons As Range
Dim listcellofficers As Range
Dim listcellowner As Range
With Sheets("sheet2").Range("A:A,C:C")
For Each listcellowner In Range("Owner")
If listcellowner.Value < "" Then
ListBox1.AddItem listcellowner.Value
End If
Next
For Each listcellofficers In Range("officers")
If listcellofficers.Value < "" Then
ListBox2.AddItem listcellofficers.Value
End If
Next
For Each ListCellreasons In Range("reasons")
If ListCellreasons.Value < "" Then
ListBox3.AddItem ListCellreasons.Value
End If
Next
End With
End Sub

TIA



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retaining focus on a textbox / listbox item pair after validation = false. chatterbox Excel Programming 2 April 4th 06 11:42 AM
Copying data from a listbox to a textbox Kevin Vaughn Excel Programming 2 March 3rd 06 11:26 PM
Listbox problem browie Excel Discussion (Misc queries) 1 August 22nd 05 11:44 AM
Listbox problem browie Excel Programming 1 August 22nd 05 11:44 AM
Populating TextBox Value--using ListBox jpendegraft[_15_] Excel Programming 1 May 2nd 04 03:53 AM


All times are GMT +1. The time now is 10:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"