ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   help pasting data from an array (https://www.excelbanter.com/excel-programming/416170-help-pasting-data-array.html)

MJKelly

help pasting data from an array
 

Hi, I have the following code which takes data from an array and
dumps it into a worksheet.
Startdata is a column number and enddata is a column number. the
columns of data between these are pasted into the worksheet from the
array. However, I always want the data pasted into column 5 onwards,
but the code is pasting the values into the column numbers of the
array. How do i change this? I've tried adding a count and setting
the start value as 5 (fifth column), but I can't get this to work?

For i = 1 To UBound(varArray)
For j = StartData To EndData
wksDump.Cells(i, j) = varArray(i, j)
Next j
Next i

any help greatly appreciated.

Matt

Nigel[_2_]

help pasting data from an array
 
The destination of the data currently is not independent of the array as you
indexing both using the same variable

Try using and extra destination column variable to control the output

For i = 1 To UBound(varArray)
dCol = 5
For j = StartData To EndData
wksDump.Cells(i, dCol) = varArray(i, j)
dCol = sCol + 1
Next j
Next i

--

Regards,
Nigel




"MJKelly" wrote in message
...

Hi, I have the following code which takes data from an array and
dumps it into a worksheet.
Startdata is a column number and enddata is a column number. the
columns of data between these are pasted into the worksheet from the
array. However, I always want the data pasted into column 5 onwards,
but the code is pasting the values into the column numbers of the
array. How do i change this? I've tried adding a count and setting
the start value as 5 (fifth column), but I can't get this to work?

For i = 1 To UBound(varArray)
For j = StartData To EndData
wksDump.Cells(i, j) = varArray(i, j)
Next j
Next i

any help greatly appreciated.

Matt



Dave Peterson

help pasting data from an array
 
So you're keeping all the rows, but only some of the columns:

For i = 1 To UBound(varArray, 1)

In fact, I'd use:

Option Explicit
Sub testme01()
Dim i As Long
Dim j As Long
Dim myCol As Long
Dim varArray As Variant
Dim StartData As Long
Dim EndData As Long
Dim wksDump As Worksheet

varArray = ActiveSheet.Range("a1:g12")

Set wksDump = Worksheets.Add

StartData = 3
EndData = 6

For i = LBound(varArray, 1) To UBound(varArray, 1)
myCol = 5
For j = StartData To EndData
wksDump.Cells(i, myCol) = varArray(i, j)
myCol = myCol + 1
Next j
Next i
End Sub




MJKelly wrote:

Hi, I have the following code which takes data from an array and
dumps it into a worksheet.
Startdata is a column number and enddata is a column number. the
columns of data between these are pasted into the worksheet from the
array. However, I always want the data pasted into column 5 onwards,
but the code is pasting the values into the column numbers of the
array. How do i change this? I've tried adding a count and setting
the start value as 5 (fifth column), but I can't get this to work?

For i = 1 To UBound(varArray)
For j = StartData To EndData
wksDump.Cells(i, j) = varArray(i, j)
Next j
Next i

any help greatly appreciated.

Matt


--

Dave Peterson

MJKelly

help pasting data from an array
 
Excellent, just what I was looking for.

Many thanks,
Matt


All times are GMT +1. The time now is 05:19 PM.

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