Input text to selected listbox row

learningthings

New Member
Joined
Oct 29, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I populated my listbox with this code:

VBA Code:
 Dim rng     As Range
    Dim MyArray
    Set ws = Sheets("Sell")

'~~> Set you relevant range here
Set rng = ws.Range("A1:O" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
With .lstpickuporder
    .Clear
    .ColumnHeads = False
    .ColumnCount = rng.Columns.Count

    '~~> create a one based 2-dim datafield array
     MyArray = rng

    '~~> fill listbox with array values
 .List = MyArray

    '~~> Set the widths of the column here. Ex: For 5 Columns
    '~~> Change as Applicable
    .ColumnWidths = "90;60;70;60;50;70;80;70;70;70;70;70;70;70;70"
    .TopIndex = 0
    .ListIndex = .ListCount - 1
End With

Now when I click the save button I want the selected row in the listbox to have the words "picked up" printed
Code:
Dim rw As Long
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("sell")
Dim lRw As Long

rw = Me.lstpickuporder.ListIndex + 2
With sh

.Cells(rw, 14) = "Picked Up"
.Cells(rw, 15) = Now()
End With
But this code does nothing. What should I do?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Your button code writes values to the worksheet, but the listbox cannot be aware of these changes, so you have to populate your listbox again. Try something like this:

VBA Code:
Private Sub CommandButton1_Click()
    DoSave
End Sub

Private Sub UserForm_Initialize()
    RefreshListBox
End Sub

Private Sub DoSave()

    Dim rw As Long
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("sell")

    rw = Me.lstpickuporder.ListIndex + 1
    With sh
        .Cells(rw, 14) = "Picked Up"
        .Cells(rw, 15) = Now()
    End With

    RefreshListBox
End Sub


Private Sub RefreshListBox()
    
    Dim rng     As Range, ws As Worksheet
    Dim MyArray
    Set ws = Sheets("Sell")

    '~~> Set you relevant range here
    Set rng = ws.Range("A1:O" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
    With Me.lstpickuporder
        .Clear
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count

        '~~> create a one based 2-dim datafield array
        MyArray = rng

        '~~> fill listbox with array values
        .List = MyArray

        '~~> Set the widths of the column here. Ex: For 5 Columns
            '~~> Change as Applicable
        .ColumnWidths = "90;60;70;60;50;70;80;70;70;70;70;70;70;70;70"
        .TopIndex = 0
        .ListIndex = .ListCount - 1
        
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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