How to paste only certain columns from a listbox into a named range

G

GH

I am struggling with how to paste only the last two columns of a three
column listbox (Listbox1) into a named range ("DataRange") on a
worksheet ("Sheet1"). The number of rows and columns are not dynamic.
Listbox1 always has 3 columns with 18 rows and DataRange always has 2
columns and 18 rows. I want to paste the contents of columns 2 and 3
from ListBox1 into DataRange.
A simple task but I cannot get the syntax correct.

GH
 
D

Dick Kusleika

GH

Try something like this

Private Sub CommandButton1_Click()

Dim rData As Range
Dim i As Long, j As Long

Set rData = Sheet1.Range("DataRange")

For i = 0 To Me.ListBox1.ListCount - 1
For j = 1 To 2 'columns start at zero, this is cols 2 and 3
rData.Cells(i + 1, j).Value = Me.ListBox1.List(i, j)
Next j
Next i

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top