ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to paste only certain columns from a listbox into a named range (https://www.excelbanter.com/excel-programming/320050-how-paste-only-certain-columns-listbox-into-named-range.html)

GH[_2_]

How to paste only certain columns from a listbox into a named range
 
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


Dick Kusleika[_4_]

How to paste only certain columns from a listbox into a named range
 
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

--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com

GH wrote:
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




GH[_2_]

How to paste only certain columns from a listbox into a named range
 
Thank you. I feel silly.



All times are GMT +1. The time now is 12:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com