Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copying ranges from one sheet to another

Hi All,

I have earlier posted this question but I think my earlier message wa
not cler, so I am re-posting this again.

I need some help with a problem regarding copying of a range from on
worksheet to another. Although I already have a code, I am getting som
errors, of which I want to ask advise from anyone, as I am not a VB/VB
programmer.

Here is what I would like to do: I have two worksheets, Sheet1 an
Sheet2, with Sheet1 containing the data that will be copied and sheet
as the target sheet.

My Sheet1 looks something like this:

A B C D E F
1 1 1 2 2 3 3
2 1 1 2 2 3 3
3 1 1 2 2 3 3
4 1 1 2 2 3 3
5 1 1 2 2 3 3

And my Sheet2 will originally look like this:

A B C D E F
1 3
2 5
3 1
4
5

When I execute my script, Sheet2 will look something like this:

A B C D E F G
1 3 2 2 3 3 1 1
2 5 2 2 3 3 1 1
3 1 2 2 3 3 1 1
4 * 2 2 3 3 1 1
5 * 2 2 3 3 1 1

* signifies space for presentation purposes

Here is my code:

Code
-------------------

Public Sub CopyThere()

Dim tmpColCtr, targetCol As Integer
Dim rrow As Range
Dim tempSheet, mainSheet As String

tempSheet = "Sheet2"
mainSheet = "Sheet1"
tmpColCtr = 3

With Worksheets(tempSheet)
For Each rrow In .Range("A:A")
If rrow.Value < "" Then
targetCol = rrow.Value
MsgBox "Column value in row is " & targetCol
Sheets(mainSheet).Range(Cells(1, targetCol), Cells(5, (targetCol + 1))).Copy _
Destination:=Sheets(tempSheet).Range(Cells(1, tmpColCtr))
tmpColCtr = tmpColCtr + 2

Else
Exit For
End If

Next rrow
End With

MsgBox "Copying of data completed!"

End Sub

-------------------


When I execute this code, using the worksheet data I have above, I a
getting the error "Run-time error '1004': Application-defined o
object-defined error".

Am I missing something on my declarations, or using th
methods/properties incorrectly? Please help! :(

Thanks in advance for your help!
Sinobat

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 131
Default Copying ranges from one sheet to another

I think the problem comes from this line:
For each rrow in .Range("A:A")

rrow is a Range object. You are treating it as a Cell.
It is true that a single cell is a range, but not vice-
versa. So you are not, as you seem to think, stepping
through each cell in the column but each Range in the
column. To clarify: all of the below are ranges within
column A:
A1; A1:A2; A1:A3; A2:A3; A2:A65536

So when you say something like "rrow.Value" what does it
mean?

As a side point, even if it worked (i.e. rrow gave you the
value of each cell in column A) the code would be very
inefficient and slow, since to copy your few rows of data
it would have to loop through each of the 65536 cells in
the column!

Here's an alternative way to find and step through
the "filled in" rows in your table:

Dim UsedRows as Long, RowStep as Long
....

With Worksheets(tempSheet).Range("A1")
UsedRows = .CurrentRegion.Rows.Count
For RowStep = 1 to UsedRows
targetCol = .Offset(RowStep-1, 0).Value
...
Next RowStep
....
End With

-----Original Message-----
Hi All,

I have earlier posted this question but I think my

earlier message was
not cler, so I am re-posting this again.

I need some help with a problem regarding copying of a

range from one
worksheet to another. Although I already have a code, I

am getting some
errors, of which I want to ask advise from anyone, as I

am not a VB/VBA
programmer.

Here is what I would like to do: I have two worksheets,

Sheet1 and
Sheet2, with Sheet1 containing the data that will be

copied and sheet2
as the target sheet.

My Sheet1 looks something like this:

A B C D E F
1 1 1 2 2 3 3
2 1 1 2 2 3 3
3 1 1 2 2 3 3
4 1 1 2 2 3 3
5 1 1 2 2 3 3

And my Sheet2 will originally look like this:

A B C D E F
1 3
2 5
3 1
4
5

When I execute my script, Sheet2 will look something like

this:

A B C D E F G
1 3 2 2 3 3 1 1
2 5 2 2 3 3 1 1
3 1 2 2 3 3 1 1
4 * 2 2 3 3 1 1
5 * 2 2 3 3 1 1

* signifies space for presentation purposes

Here is my code:

Code:
--------------------

Public Sub CopyThere()

Dim tmpColCtr, targetCol As Integer
Dim rrow As Range
Dim tempSheet, mainSheet As String

tempSheet = "Sheet2"
mainSheet = "Sheet1"
tmpColCtr = 3

With Worksheets(tempSheet)
For Each rrow In .Range("A:A")
If rrow.Value < "" Then
targetCol = rrow.Value
MsgBox "Column value in row is " & targetCol
Sheets(mainSheet).Range(Cells(1, targetCol), Cells(5,

(targetCol + 1))).Copy _
Destination:=Sheets(tempSheet).Range(Cells(1,

tmpColCtr))
tmpColCtr = tmpColCtr + 2

Else
Exit For
End If

Next rrow
End With

MsgBox "Copying of data completed!"

End Sub

--------------------


When I execute this code, using the worksheet data I have

above, I am
getting the error "Run-time error '1004': Application-

defined or
object-defined error".

Am I missing something on my declarations, or using the
methods/properties incorrectly? Please help! :

(

Thanks in advance for your help!
Sinobato


---
Message posted from http://www.ExcelForum.com/

.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with copying multiple ranges and paste it on a work sheet based on conditions prakash Excel Discussion (Misc queries) 0 November 30th 06 10:18 AM
Copying ranges from one sheet to another Sinobato[_9_] Excel Programming 0 July 29th 04 03:14 PM
selectively copying ranges from one sheet to second sheet JPTIII Excel Programming 1 December 5th 03 08:02 PM
copying ranges etc chick-racer[_43_] Excel Programming 0 November 27th 03 02:33 PM
Copying ranges brym Excel Programming 8 July 19th 03 04:27 PM


All times are GMT +1. The time now is 09:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"