How to Offset Column in this Multi Select Listbox?

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
I got this code from OnlinePCLearning. How do I offset every other column?

Private Sub cmdAdd_Click()
Dim addme As Range, cNum As Integer
Dim x As Integer, y As Integer, Ck As Integer
'set variables
Set addme = Sheet1.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0)
cNum = 7
Ck = 0
'run the for loop
For x = 0 To Me.lstMulti.ListCount - 1
'add condition statement
If Me.lstMulti.Selected(x) Then
Ck = 1
'second loop
For y = 0 To cNum
addme.Offset(0, y) = Me.lstMulti.List(x, y)
Next y
Set addme = addme.Offset(2, 0)
End If
'clear the selected values
lstMulti.Selected(x) = False
Next x
'send a message if nothing is selected
If Ck = 0 Then
MsgBox "There is nothing selected"
End If
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this
Code:
'run the for loop
For x = 0 To Me.lstMulti.ListCount - 1
    'add condition statement
    If Me.lstMulti.Selected(x) Then
        Ck = 1
        'second loop
        For y = 0 To cNum
            addme.Offset(0, [COLOR="#FF0000"]2 * y[/COLOR]) = Me.lstMulti.List(x, y)
        Next y
        Set addme = addme.Offset(2, 0)
    End If
    'clear the selected values
    lstMulti.Selected(x) = False
Next x
 
Upvote 0
Another option:

Code:
Private Sub cmdAdd_Click()
  [COLOR=#0000ff]Dim lr As Long[/COLOR], cNum As Integer, x As Integer, y As Integer, Ck As Integer
  'set variables
[COLOR=#0000ff]  lr = Sheet1.Cells(Rows.Count, 4).End(xlUp)(2).Row[/COLOR]
  cNum = 7
  Ck = 0
  'run the for loop
  For x = 0 To Me.lstMulti.ListCount - 1
    'add condition statement
    If Me.lstMulti.Selected(x) Then
      Ck = 1
      'second loop
      For y = 0 To cNum
        Cells(lr, 4 + y) = Me.lstMulti.List(x, y)
      Next y
[COLOR=#0000ff]      lr = lr + 1[/COLOR]
    End If
    'clear the selected values
    lstMulti.Selected(x) = False
  Next x
  'send a message if nothing is selected
  If Ck = 0 Then
    MsgBox "There is nothing selected"
  End If
End Sub
 
Upvote 0
The OP offsets 2 rows for each found item, should that be lr = lr +2 ?

If so, then it is correct, it must be lr = lr + 2, but I have the doubt because at the beginning it only offset a row:

Code:
Set addme = Sheet1.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0)
 
Upvote 0
Try this
Code:
'run the for loop
For x = 0 To Me.lstMulti.ListCount - 1
    'add condition statement
    If Me.lstMulti.Selected(x) Then
        Ck = 1
        'second loop
        For y = 0 To cNum
            addme.Offset(0, [COLOR=#FF0000]2 * y[/COLOR]) = Me.lstMulti.List(x, y)
        Next y
        Set addme = addme.Offset(2, 0)
    End If
    'clear the selected values
    lstMulti.Selected(x) = False
Next x

Thank you Very Much!!
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
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