Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Copy Sheet Values to new WB

I am having trouble with the code to copy the sheet values to a new
workbook where the copyfrom WB has multiple sheets. The number of
sheets may vary, but always more than one. TIA

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Copy Sheet Values to new WB

Can you define "having trouble?"

If you identify the source worksheet correctly, there should be no problem.
Posting your code would help.

--

Vasant


"GregR" wrote in message
oups.com...
I am having trouble with the code to copy the sheet values to a new
workbook where the copyfrom WB has multiple sheets. The number of
sheets may vary, but always more than one. TIA



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Copy Sheet Values to new WB

Here is some real generic code. Without more details of what you need to do
this is the best I can give you...

Sub CopySheets()
Dim wbkNew As Workbook
Dim wksToCopy As Worksheet
Dim wksPaste As Worksheet

Set wbkNew = Workbooks.Add
Set wksPaste = wbkNew.Worksheets.Add
Set wksToCopy = ThisWorkbook.Sheets("Sheet1")
wksToCopy.Cells.Copy
wksPaste.Cells.PasteSpecial xlPasteValues

Set wksPaste = wbkNew.Worksheets.Add
Set wksToCopy = ThisWorkbook.Sheets("Sheet2")
wksToCopy.Cells.Copy
wksPaste.Cells.PasteSpecial xlPasteValues
End Sub

--
HTH...

Jim Thomlinson


"GregR" wrote:

I am having trouble with the code to copy the sheet values to a new
workbook where the copyfrom WB has multiple sheets. The number of
sheets may vary, but always more than one. TIA


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Copy Sheet Values to new WB

Vasant, got it to work, but can the code be improved?

Sub CopySheetsValues()

Dim ThisBookSheets As Long
Dim OldNumSheets As Long
Dim i As Long
Dim ThisWorkbookName As String

OldNumSheets = Application.SheetsInNewWorkbook
ThisBookSheets = ThisWorkbook.Worksheets.Count
ThisWorkbookName = ThisWorkbook.Name

' Add new workbook with as many sheets as are in the current workbook
Application.SheetsInNewWorkbook = ThisBookSheets
Workbooks.Add


For i = 1 To ThisBookSheets
Workbooks(ThisWorkbookName).Sheets(i).Cells.Copy
With Sheets(i).Cells
.PasteSpecial Paste:=xlValues
.PasteSpecial Paste:=xlFormats
End With
Next i

Application.SheetsInNewWorkbook = OldNumSheets

End Sub

Greg

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 692
Default Copy Sheet Values to new WB

Greg,

Since you got the code from Vasant it should already be the best...

If I had to change anything it would be the variable names.
My preference is to keep them as short as possible, but it is only a
preference...

You might want to start the code with the below:

This one stops screen flashing and in some cases helps the code to run
faster:

Application.ScreenUpdating = False
' the code here
Applications.ScreenUpdating = True

You can add similar structures to turn event code off (if there is any),
And some turn calculation off and on if there are a lot of formulas.

Other wise - Vasant definitely knows how to write great code...
--
steveB

Remove "AYN" from email to respond
"GregR" wrote in message
oups.com...
Vasant, got it to work, but can the code be improved?

Sub CopySheetsValues()

Dim ThisBookSheets As Long
Dim OldNumSheets As Long
Dim i As Long
Dim ThisWorkbookName As String

OldNumSheets = Application.SheetsInNewWorkbook
ThisBookSheets = ThisWorkbook.Worksheets.Count
ThisWorkbookName = ThisWorkbook.Name

' Add new workbook with as many sheets as are in the current workbook
Application.SheetsInNewWorkbook = ThisBookSheets
Workbooks.Add


For i = 1 To ThisBookSheets
Workbooks(ThisWorkbookName).Sheets(i).Cells.Copy
With Sheets(i).Cells
.PasteSpecial Paste:=xlValues
.PasteSpecial Paste:=xlFormats
End With
Next i

Application.SheetsInNewWorkbook = OldNumSheets

End Sub

Greg





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Copy Sheet Values to new WB

Jim, what I would like, is to have generic code in my personal.xls that
does the above, but also pastes the formatting. As I understand your
code, if this code is not in a specific workbook module, it copies the
sheets from my personal.xls, as it is "ThisWorkbook". Also, I don't
want it set to a specific number of worksheets, but however many there
are in the active workbook. TIA

Greg

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Copy Sheet Values to new WB

Thanks for the compliment, Steve, but I did not provide Greg with the code!
<g

However, the following might be more concise:

Sub Test()
Dim ws As Worksheet, c As Range
Application.ScreenUpdating = False
Workbooks("source.xls").Worksheets.Copy
For Each ws In Worksheets
For Each c In ws.UsedRange.Cells
c = c
Next
Next
Application.ScreenUpdating = True
End Sub

Regards,

Vasant


"STEVE BELL" wrote in message
news:VbQKe.11561$0d.10364@trnddc02...
Greg,

Since you got the code from Vasant it should already be the best...

If I had to change anything it would be the variable names.
My preference is to keep them as short as possible, but it is only a
preference...

You might want to start the code with the below:

This one stops screen flashing and in some cases helps the code to run
faster:

Application.ScreenUpdating = False
' the code here
Applications.ScreenUpdating = True

You can add similar structures to turn event code off (if there is any),
And some turn calculation off and on if there are a lot of formulas.

Other wise - Vasant definitely knows how to write great code...
--
steveB

Remove "AYN" from email to respond
"GregR" wrote in message
oups.com...
Vasant, got it to work, but can the code be improved?

Sub CopySheetsValues()

Dim ThisBookSheets As Long
Dim OldNumSheets As Long
Dim i As Long
Dim ThisWorkbookName As String

OldNumSheets = Application.SheetsInNewWorkbook
ThisBookSheets = ThisWorkbook.Worksheets.Count
ThisWorkbookName = ThisWorkbook.Name

' Add new workbook with as many sheets as are in the current workbook
Application.SheetsInNewWorkbook = ThisBookSheets
Workbooks.Add


For i = 1 To ThisBookSheets
Workbooks(ThisWorkbookName).Sheets(i).Cells.Copy
With Sheets(i).Cells
.PasteSpecial Paste:=xlValues
.PasteSpecial Paste:=xlFormats
End With
Next i

Application.SheetsInNewWorkbook = OldNumSheets

End Sub

Greg





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Copy Sheet Values to new WB

Here is some code... This will copy over everything including pictures,
shapes...

Sub CopySheets()
Dim wbkCurrent As Workbook
Dim wbkNew As Workbook
Dim wksToCopy As Worksheet
Dim wksPaste As Worksheet

Set wbkCurrent = ActiveWorkbook
For Each wksToCopy In wbkCurrent.Worksheets
If wbkNew Is Nothing Then
wksToCopy.Copy
Set wbkNew = ActiveWorkbook
Else
wksToCopy.Copy wbkNew.Sheets(wbkNew.Sheets.Count)
End If
Set wksPaste = wbkNew.ActiveSheet
wksToCopy.Cells.Copy
wksPaste.Cells.PasteSpecial xlValues
wbkCurrent.Activate
Next wksToCopy

End Sub
--
HTH...

Jim Thomlinson


"GregR" wrote:

Jim, what I would like, is to have generic code in my personal.xls that
does the above, but also pastes the formatting. As I understand your
code, if this code is not in a specific workbook module, it copies the
sheets from my personal.xls, as it is "ThisWorkbook". Also, I don't
want it set to a specific number of worksheets, but however many there
are in the active workbook. TIA

Greg


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Copy Sheet Values to new WB

Vasant, Steve, and Jim thank you very much.

Greg

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 values generated by conditional formula in one sheet to the other work sheet as values ramana Excel Worksheet Functions 1 October 5th 05 01:04 PM
copy values generated by conditional formula in one sheet to the other work sheet as values bobby Excel Discussion (Misc queries) 1 October 5th 05 12:18 PM
how to find and copy values on sheet 2, based on a list on sheet 1 evanmacnz Excel Programming 4 February 7th 05 08:33 PM
how to copy values to another sheet C. Thies Excel Programming 1 July 2nd 04 02:08 AM
How do i compare values from two sheet and copy & paste if values match? rozb Excel Programming 0 March 5th 04 12:06 AM


All times are GMT +1. The time now is 07:03 PM.

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"