Question Locked

Programatically copy worksheet via VBA in Excel

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
I need to programatically copy a worksheet to a new workbook via VBA in Excel97 without any of the links back to the original workbook yet retain all data and formuli. I can already create the new workbook & copy the worksheets. I just need a way to Not have the Links in the new workbook. This is for a client who only wants to press a button. So, copy/paste or anyother non-coded method won't work.

UPDATE: I just attempted to copy/paste special... well, that didn't work either; something to do with invalid links - I don't care!!! How stupid IS that - Just remove the frikkin links!!!

Thanx!

Dave

5 replies

by myronjams

Hi,

I spent the last 2days scouring the Internet looking for ways to
accomplish this. I used some of what I found, but then modified it to
work as I needed it to. You should be able to use this without much
hassle. Just rename to your Workbook.Worksheets and make sure your
changing only those values you need to. The way I did this doesn't
remove any Formulas on the worksheet; Only Links.

The Code:

Dim oBook As Workbook

Excel.Application.DisplayAlerts = False
Application.ScreenUpdating = False

Me.btnExportWorkSheet.TakeFocusOnClick = False

' Delete the Old File
Kill "c:\FileName.xls"

' Create a new blank workbook:
Set oBook = Application.Workbooks.Add
Application.SheetsInNewWorkbook = 1

' Add a defined name to the workbook
' that RefersTo a range:
oBook.Names.Add Name:="tempRange", RefersTo:="=Sheet1!$A$1"

' Save the workbook:
oBook.SaveAs "c:\FileName.xls"

' Select the Workbook where the Worksheet to be copied is located
Workbooks("Old File.xls").Activate
Workbooks("Old File.xls").Worksheets("Work Sheet A").Activate

' Copy the Worksheet to the Workbook
Worksheets("Work Sheet A").Copy Before:=Workbooks("New Work
Book.xls").Sheets(1)

Dim xRange As Range, adr As String

Workbooks("New Work Book.xls").Worksheets("Work Sheet A").Activate
Workbooks("New Work Book.xls").Worksheets("Work Sheet
A").Range("a1:z100").Activate

' Remove Links and Replace with Cell Values
With Workbooks("New Work Book.xls").Worksheets("Work Sheet A")

Dim cCell As Range
Dim strValue As String

Set xRange = .Range("a1:z100")
For Each cCell In xRange

' Save the value to use as a means to identify what cells
to change
strValue = CStr(cCell.Value)

If InStr(strValue, "/") > 0 Then

cCell = CStr(cCell.Value)

End If
Next

End With

' Select the next Workbook where the Worksheet to be copied is
located
Workbooks("Old File.xls").Activate
Workbooks("Old File.xls").Worksheets("Work Sheet B").Activate

' Copy the Worksheet to the Workbook
Worksheets("Work Sheet B").Copy
Before:=Workbooks("CashFlowInput&DollarChart.xls").Sheets(1) ',
UpdateLinks:=0

Workbooks("New Work Book.xls").Worksheets("Work Sheet B").Activate
Workbooks("New Work Book.xls").Worksheets("Work Sheet
B").Range("a1:z100").Activate

' Remove Links and Replace with Cell Values
With Workbooks("New Work Book.xls").Worksheets("Dollars Chart")

Set xRange = .Range("a1:z100")
For Each cCell In xRange

' Save the value to use as a means to identify what cells
to change
' cCell.Formula will show the link back to the Original
Workbook
strValue = CStr(cCell.Formula)

If InStr(strValue, "!") > 0 Then

cCell = CStr(cCell.Value)
' If cCell = .Range("b51") Or cCell = .Range("m4") Then
' Stop
' End If

End If
Next

End With

MsgBox ("Your Worksheets have been copied to C:\New Work
Book.xls")

End Sub

by vdangwal

This is logic:

1. Make a object of excel using add reference to microsoft excel 8 object library.

2. use that object to initiate new excel application.

3. u can use add(new worksheet) or open (existing worksheet) in it where you want to paste your data.

4. then first activate first workbook. copy data using [sheet].Range[some range].copy

5. activate other workbook and select [sheet].range[somerange].paste

now programatically save the 2nd excel application and then close the first excel application.

by siva.perumal

Hi,

i have created 2 reports in 2 separate excel file.Now i want that in same excel workbook in each sheets.

Sheet 1 - report 1
sheet 2 - report 2. The number of reports is dynamic.

I really appreciate your help.

Thanks,
Siva.

by myronjams

You should be able to use th code I provided in my "I Figured it out" post with a little modification to fit your needs.

Basicly:
Your creating an empty workbook
copying the 1st worksheet &
then copying the next worksheet into the same workbook.

You can also open the workbook and then copy the worksheet in as well.

I hope this helps

Myron

This discussion is closed to new replies.