Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need Help to copy data from one worksheet to another

Hi,

I have a sheet with about 48 rows and columns with data from Row A to
Row CD. When The macro runs I want to create a new sheet and copy the
first 5 rows to the new sheet and give the user a way to choose the
other row to copy to the new sheet.


I am able to create the new sheet and to let the user select the rows
to copy, buth the new sheet is blank. Could Somebody please help.


Here is the code I have written.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional


Dim wSht As Worksheet
Dim ResultWSht As Worksheet


Dim shtName As String
Dim Last As Long


Set Rng = Application.InputBox(prompt:="Select Column to Work on",
Type:=8)


Rng.Select


shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht


Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName
Set wSht = ActiveSheet
wSht.Range("A1").Copy
ResultWSht.Range ("A1:CD5")
MsgBox "I am at the END"
End Sub


Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Need Help to copy data from one worksheet to another

Your line 'Set wSht = ActiveSheet' is misplaced. Once you have added the new
sheet, it is now the ActiveSheet. What you did was set wSht = to ResultWSht
so you were copying the new worksheet to itself. Set wSht = ActiveSheet
needs to be after the name check loop but before adding the new sheet.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional
Dim wSht As Worksheet
Dim ResultWSht As Worksheet
Dim shtName As String
Dim Last As Long
Dim rng As Range

Set rng = Application.InputBox(prompt:="Select Column to Work on", Type:=8)
rng.Select

shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht

Set wSht = ActiveSheet
Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName

wSht.Range("A1").Copy ResultWSht.Range("A1:CD5")
MsgBox "I am at the END"
End Sub


Mike F
wrote in message
ups.com...
Hi,

I have a sheet with about 48 rows and columns with data from Row A to
Row CD. When The macro runs I want to create a new sheet and copy the
first 5 rows to the new sheet and give the user a way to choose the
other row to copy to the new sheet.


I am able to create the new sheet and to let the user select the rows
to copy, buth the new sheet is blank. Could Somebody please help.


Here is the code I have written.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional


Dim wSht As Worksheet
Dim ResultWSht As Worksheet


Dim shtName As String
Dim Last As Long


Set Rng = Application.InputBox(prompt:="Select Column to Work on",
Type:=8)


Rng.Select


shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht


Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName
Set wSht = ActiveSheet
wSht.Range("A1").Copy
ResultWSht.Range ("A1:CD5")
MsgBox "I am at the END"
End Sub


Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need Help to copy data from one worksheet to another

Mike,

Thanks for your help. Now I am able to copy the first 5 rows from the
old sheet to the new sheet.

When I try to copy the user selected row I am getting the following
runtime error.
Runtime Error 1004:
Method 'Range' of object '_Worksheet failed.

This is what I added at the end after the 5 rows are copied
Set wSht = ActiveSheet
wSht.Range(rng).Copy ResultWSht.Range("A7")

Could you pls help me with this error.

Thanks Again

Mike Fogleman wrote:
Your line 'Set wSht = ActiveSheet' is misplaced. Once you have added the new
sheet, it is now the ActiveSheet. What you did was set wSht = to ResultWSht
so you were copying the new worksheet to itself. Set wSht = ActiveSheet
needs to be after the name check loop but before adding the new sheet.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional
Dim wSht As Worksheet
Dim ResultWSht As Worksheet
Dim shtName As String
Dim Last As Long
Dim rng As Range

Set rng = Application.InputBox(prompt:="Select Column to Work on", Type:=8)
rng.Select

shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht

Set wSht = ActiveSheet
Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName

wSht.Range("A1").Copy ResultWSht.Range("A1:CD5")
MsgBox "I am at the END"
End Sub


Mike F
wrote in message
ups.com...
Hi,

I have a sheet with about 48 rows and columns with data from Row A to
Row CD. When The macro runs I want to create a new sheet and copy the
first 5 rows to the new sheet and give the user a way to choose the
other row to copy to the new sheet.


I am able to create the new sheet and to let the user select the rows
to copy, buth the new sheet is blank. Could Somebody please help.


Here is the code I have written.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional


Dim wSht As Worksheet
Dim ResultWSht As Worksheet


Dim shtName As String
Dim Last As Long


Set Rng = Application.InputBox(prompt:="Select Column to Work on",
Type:=8)


Rng.Select


shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht


Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName
Set wSht = ActiveSheet
wSht.Range("A1").Copy
ResultWSht.Range ("A1:CD5")
MsgBox "I am at the END"
End Sub


Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Need Help to copy data from one worksheet to another

Mike,

Thanks I got it working now.

This is the code I am using to copy the user defined range

'Set wSht = ActiveSheet
MsgBox "Range Selected is " & rng.Address

wSht.Range(rng.Address).Copy ResultWSht.Range("A7")

Thanks for your help.
dpatwary

wrote:
Mike,

Thanks for your help. Now I am able to copy the first 5 rows from the
old sheet to the new sheet.

When I try to copy the user selected row I am getting the following
runtime error.
Runtime Error 1004:
Method 'Range' of object '_Worksheet failed.

This is what I added at the end after the 5 rows are copied
Set wSht = ActiveSheet
wSht.Range(rng).Copy ResultWSht.Range("A7")

Could you pls help me with this error.

Thanks Again

Mike Fogleman wrote:
Your line 'Set wSht = ActiveSheet' is misplaced. Once you have added the new
sheet, it is now the ActiveSheet. What you did was set wSht = to ResultWSht
so you were copying the new worksheet to itself. Set wSht = ActiveSheet
needs to be after the name check loop but before adding the new sheet.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional
Dim wSht As Worksheet
Dim ResultWSht As Worksheet
Dim shtName As String
Dim Last As Long
Dim rng As Range

Set rng = Application.InputBox(prompt:="Select Column to Work on", Type:=8)
rng.Select

shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht

Set wSht = ActiveSheet
Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName

wSht.Range("A1").Copy ResultWSht.Range("A1:CD5")
MsgBox "I am at the END"
End Sub


Mike F
wrote in message
ups.com...
Hi,

I have a sheet with about 48 rows and columns with data from Row A to
Row CD. When The macro runs I want to create a new sheet and copy the
first 5 rows to the new sheet and give the user a way to choose the
other row to copy to the new sheet.


I am able to create the new sheet and to let the user select the rows
to copy, buth the new sheet is blank. Could Somebody please help.


Here is the code I have written.

Sub SpreadWorkAssgmnt()
Dim RowToBeCopied As Range 'This line of code is optional


Dim wSht As Worksheet
Dim ResultWSht As Worksheet


Dim shtName As String
Dim Last As Long


Set Rng = Application.InputBox(prompt:="Select Column to Work on",
Type:=8)


Rng.Select


shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht


Set ResultWSht = ThisWorkbook.Worksheets.Add
ResultWSht.Name = shtName
Set wSht = ActiveSheet
wSht.Range("A1").Copy
ResultWSht.Range ("A1:CD5")
MsgBox "I am at the END"
End Sub


Thanks


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
copy from B worksheet to A worksheet with NO repeated data tikchye_oldLearner57 Excel Discussion (Misc queries) 1 September 29th 06 06:56 PM
copy data in a cell from worksheet A to worksheet B rajesh Excel Discussion (Misc queries) 1 February 21st 06 07:40 AM
Copy Data from one worksheet to another Sdbenn90 Excel Discussion (Misc queries) 0 December 30th 05 07:31 PM
Copy Modified Worksheet 1 Data to Worksheet 2 clarkelrc Excel Programming 0 April 15th 04 01:36 PM
copy all worksheet data into a new worksheet eric Excel Programming 2 October 2nd 03 08:33 PM


All times are GMT +1. The time now is 06:28 PM.

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

About Us

"It's about Microsoft Excel"