PC Review


Reply
Thread Tools Rate Thread

Copy Excel Sheet From Access

 
 
Ross Lewis
Guest
Posts: n/a
 
      8th Jun 2004
I am trying to create a copy an excel sheet and give it a new name (from a
button in Access).
The main code listed below, I have used before for extracting information
from Excel.
What I would like to know is how to convert the excel macro that I have
inserted into the code to work with Access VBA.

Any help would be greatly appreciated.


Private Sub copy_excel_sheet_Click()
Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
Set xlsApp = CreateObject("Excel.Application")
Set xlsWB = xlsApp.Workbooks.Open("C:\excel_file.xls", , True)
'
' Excel Macro
'
Sheets("sheet1").Select
Sheets("sheet1").Copy After:=Sheets("sheet1")
Sheets("sheet1 (2)").Select
Sheets("sheet1 (2)").Name = "sheet2"
'
xlsWB.Close False
Set xlsWB = Nothing
xlsApp.Quit
Set xlsApp = Nothing
End Sub


 
Reply With Quote
 
 
 
 
JimB
Guest
Posts: n/a
 
      8th Jun 2004
Are you trying to copy sheet1 to sheet2 in the same
workbook or copy sheet1 into a new workbook?

>-----Original Message-----
>I am trying to create a copy an excel sheet and give it a

new name (from a
>button in Access).
>The main code listed below, I have used before for

extracting information
>from Excel.
>What I would like to know is how to convert the excel

macro that I have
>inserted into the code to work with Access VBA.
>
>Any help would be greatly appreciated.
>
>
>Private Sub copy_excel_sheet_Click()
>Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
>Set xlsApp = CreateObject("Excel.Application")
>Set xlsWB = xlsApp.Workbooks.Open("C:\excel_file.xls", ,

True)
> '
> ' Excel Macro
> '
> Sheets("sheet1").Select
> Sheets("sheet1").Copy After:=Sheets("sheet1")
> Sheets("sheet1 (2)").Select
> Sheets("sheet1 (2)").Name = "sheet2"
> '
>xlsWB.Close False
>Set xlsWB = Nothing
>xlsApp.Quit
>Set xlsApp = Nothing
>End Sub
>
>
>.
>

 
Reply With Quote
 
JimB
Guest
Posts: n/a
 
      9th Jun 2004
Ross, here are 2 sample modules. The first copies data
from a query into multiple sheets. The second opens a
excel file (template) then loads data into new sheets and
saves them into separate workbooks. You need to have the
Excel 9.0 Object Library reference turned on.

Sample 1)
Function Tbl_Qry_To_Excel()
Dim objExcel As Excel.Application
Dim objSheet As Excel.Worksheet
Dim objBook As Excel.Workbook
Dim j As Long, i As Integer, K As Integer
Dim fld1 As String, dblSum As Double
Dim tbl1 As New ADODB.Recordset
Dim cn As New ADODB.Connection

' Create a new instance of Excel workbook and sheet.
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Add
Set objSheet = objBook.Worksheets.Add(, , 9)
Set cn = CurrentProject.Connection

' Set variables to zero.
j = 0
i = 0
K = 0
fld1 = "c:\temp\xltext" & Format(Time(), "hhnnss")
& ".xls"
' Turn off Access warning messages.
DoCmd.SetWarnings False
' Do you want to see Excel on screen?
If MsgBox("Do you want Excel visible?", vbInformation
+ vbYesNo, _
"Application Visible?") = vbYes Then
' Show the instance of Excel on the screen.
objExcel.Visible = True
End If
' Open query an save as recordset.
tbl1.Open "Query1", cn, adOpenKeyset, adLockOptimistic
' How many records in recordset?
j = tbl1.RecordCount
' Open, copy recordset then format worksheet.
For K = 1 To 3
tbl1.MoveFirst
'objBook.Sheets(K).Select.
With objBook.Sheets(K)
' Make this sheet active.
.Activate
.Shapes.AddPicture "c:\temp\5_1.bmp", False, True,
140, 2, 63, 32
.Range("a5").CopyFromRecordset tbl1
.Range("a4").Value = "Creditor Name"
.Range("a4").Interior.ColorIndex = 5
.Range("a4").Font.ColorIndex = 2
.Range("a4").Font.Bold = True
.Range("b4").Value = "Invoice#"
.Range("b4").Interior.ColorIndex = 3
.Range("b4").Font.Bold = True
.Range("c4").Value = "LC Amount"
.Range("c4").Interior.ColorIndex = 4
.Range("c4").Font.Bold = True
dblSum = .Application.WorksheetFunction.Sum(.Range
("c5:c" & j + 4))
.Range("b" & j + 6).Value = "Sum"
.Range("c" & j + 6).Value = dblSum
.Range("b" & j + 6 & ":c" & j + 6).Font.Bold = True
.Range("b" & j + 6 & ":c" & j + 6).Interior.ColorIndex
= 28
.Range("b4").HorizontalAlignment = xlCenter
.Range("c4").HorizontalAlignment = xlCenter
.Range("a4:c" & j + 4).Borders.ColorIndex = 1
.Range("b" & j + 6 & ":c" & j + 6).Borders.ColorIndex
= 1
.Rows("4").RowHeight = 20
.Rows("4").VerticalAlignment = xlCenter
.Columns("a:b").AutoFit
.Columns("c").ColumnWidth = 20
.Name = "Answer" & K
End With
Next K
' Make first sheet active.
objBook.Sheets(1).Activate
tbl1.Close
' How many sheets are there?
i = objBook.Sheets.Count
K = 0
' Turn off Excel alert messages.
objExcel.DisplayAlerts = False
For j = i To 1 Step -1
For K = 1 To i
If objBook.Sheets(j).Name = "Sheet" & K Then
objBook.Sheets(j).Delete
K = i
End If
Next K
Next j

' Turn on Excel alert messages.
objExcel.DisplayAlerts = True
' Save the Workbook.
objBook.SaveAs fld1
' Close the Workbook.
objBook.Close
' Close Excel to free up memory.
objExcel.Quit
' Set the variable to Nothing to free up the name
' space in Access.
Set tbl1 = Nothing
Set objExcel = Nothing
Set objSheet = Nothing
Set objBook = Nothing
' Turn on Access warning messages.
DoCmd.SetWarnings True

MsgBox "Done! File saved as:" & vbCrLf & vbCrLf & fld1

End Function

Sample 2)
Function Tbl_Qry_To_Excel_2()
Dim objExcel As Excel.Application
Dim objSheet As Excel.Worksheet
Dim objBook As Excel.Workbook
Dim j As Long, i As Integer, K As Integer, JJ As
Integer
Dim fld1 As String, dblSum As Double
Dim tbl1 As New ADODB.Recordset
Dim cn As New ADODB.Connection

' Create a new instance of Excel workbook and sheet.
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open
("c:\temp\xltexttemp.xlt")
Set objSheet = objBook.Worksheets(1)
Set cn = CurrentProject.Connection

' Set variables to zero.
j = 0
i = 0
K = 0
JJ = 0
fld1 = "c:\temp\xltext" & Format(Time(), "hhnnss")
& ".xls"
' Turn off Access warning messages.
DoCmd.SetWarnings False
' Do you want to see Excel on screen?
If MsgBox("Do you want Excel visible?", vbInformation
+ vbYesNo, _
"Application Visible?") = vbYes Then
' Show the instance of Excel on the screen.
objExcel.Visible = True
End If
' Open query an save as recordset.
tbl1.Open "Query1", cn, adOpenKeyset, adLockOptimistic
' How many records in recordset?
j = tbl1.RecordCount
' Copy template into three (3) new workbooks.
For K = 27 To 29
objBook.SaveCopyAs "c:\temp\xltext" & K & ".xls"
Next K
' Close template.
objBook.Close
' Delay counter = 1 second
For K = 27 To 29
For i = 1 To 1000
Next i
' Open "saved" Workbook.
Set objBook = objExcel.Workbooks.Open("c:\temp\xltext"
& K & ".xls")
Set objSheet = objBook.Worksheets(1)
tbl1.MoveFirst
' Open and copy recordset into Worksheet.
With objSheet
.Range("a5").CopyFromRecordset tbl1
dblSum = .Application.WorksheetFunction.Sum(.Range
("c5:c" & j + 4))
.Range("b" & j + 6).Value = "Sum"
.Range("c" & j + 6).Value = dblSum
.Range("b" & j + 6 & ":c" & j + 6).Font.Bold = True
.Range("b" & j + 6 & ":c" & j + 6).Interior.ColorIndex
= 28
.Range("a5:c" & j + 4).Borders.ColorIndex = 1
.Range("b" & j + 6 & ":c" & j + 6).Borders.ColorIndex
= 1
.Name = "Today at " & Format(Time(), "hhnnss")
End With
' How many sheets are there?
i = objBook.Sheets.Count
' Turn off Excel alert messages.
objExcel.DisplayAlerts = False
For JJ = i To 1 Step -1
If objBook.Sheets(JJ).Name = "Sheet" & JJ Then
objBook.Sheets(JJ).Delete
End If
Next JJ
' Turn on Excel alert messages.
objExcel.DisplayAlerts = True
' Save the Workbook.
objBook.Save
' Close the Workbook.
objBook.Close
Next K
tbl1.Close
' Close Excel to free up memory.
objExcel.Quit
' Set the variable to Nothing to free up the name
' space in Access.
Set tbl1 = Nothing
Set objExcel = Nothing
Set objSheet = Nothing
Set objBook = Nothing
' Turn on Access warning messages.
DoCmd.SetWarnings True

MsgBox "Done! Files saved is C:\Temp!"

End Function

>-----Original Message-----
>Are you trying to copy sheet1 to sheet2 in the same
>workbook or copy sheet1 into a new workbook?
>
>>-----Original Message-----
>>I am trying to create a copy an excel sheet and give it

a
>new name (from a
>>button in Access).
>>The main code listed below, I have used before for

>extracting information
>>from Excel.
>>What I would like to know is how to convert the excel

>macro that I have
>>inserted into the code to work with Access VBA.
>>
>>Any help would be greatly appreciated.
>>
>>
>>Private Sub copy_excel_sheet_Click()
>>Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
>>Set xlsApp = CreateObject("Excel.Application")
>>Set xlsWB = xlsApp.Workbooks.Open("C:\excel_file.xls", ,

>True)
>> '
>> ' Excel Macro
>> '
>> Sheets("sheet1").Select
>> Sheets("sheet1").Copy After:=Sheets("sheet1")
>> Sheets("sheet1 (2)").Select
>> Sheets("sheet1 (2)").Name = "sheet2"
>> '
>>xlsWB.Close False
>>Set xlsWB = Nothing
>>xlsApp.Quit
>>Set xlsApp = Nothing
>>End Sub
>>
>>
>>.
>>

>.
>

 
Reply With Quote
 
Ross Lewis
Guest
Posts: n/a
 
      9th Jun 2004
I am trying to copy sheet1 to sheet2 in the same workbook.

"JimB" <(E-Mail Removed)> wrote in message
news:1a07801c44d9a$9f9fd150$(E-Mail Removed)...
> Are you trying to copy sheet1 to sheet2 in the same
> workbook or copy sheet1 into a new workbook?
>
> >-----Original Message-----
> >I am trying to create a copy an excel sheet and give it a

> new name (from a
> >button in Access).
> >The main code listed below, I have used before for

> extracting information
> >from Excel.
> >What I would like to know is how to convert the excel

> macro that I have
> >inserted into the code to work with Access VBA.
> >
> >Any help would be greatly appreciated.
> >
> >
> >Private Sub copy_excel_sheet_Click()
> >Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
> >Set xlsApp = CreateObject("Excel.Application")
> >Set xlsWB = xlsApp.Workbooks.Open("C:\excel_file.xls", ,

> True)
> > '
> > ' Excel Macro
> > '
> > Sheets("sheet1").Select
> > Sheets("sheet1").Copy After:=Sheets("sheet1")
> > Sheets("sheet1 (2)").Select
> > Sheets("sheet1 (2)").Name = "sheet2"
> > '
> >xlsWB.Close False
> >Set xlsWB = Nothing
> >xlsApp.Quit
> >Set xlsApp = Nothing
> >End Sub
> >
> >
> >.
> >



 
Reply With Quote
 
JimB
Guest
Posts: n/a
 
      9th Jun 2004
Ross, try this:

objExcel.Worksheets(1).Activate
objExcel.Worksheets(1).Range("a1:xx").Copy
objExcel.ActiveSheet.Paste
Destination:=objExcel.Worksheets(2).Range("a1:xx")
' Save the Workbook
objBook.SaveAs WhereToReport & ".xls"
' Close the Workbook
objBook.Close

Substitute xx with the last data field cell range. Od
give a max cell range if your data varies.

>-----Original Message-----
>I am trying to copy sheet1 to sheet2 in the same workbook.
>
>"JimB" <(E-Mail Removed)> wrote in

message
>news:1a07801c44d9a$9f9fd150$(E-Mail Removed)...
>> Are you trying to copy sheet1 to sheet2 in the same
>> workbook or copy sheet1 into a new workbook?
>>
>> >-----Original Message-----
>> >I am trying to create a copy an excel sheet and give

it a
>> new name (from a
>> >button in Access).
>> >The main code listed below, I have used before for

>> extracting information
>> >from Excel.
>> >What I would like to know is how to convert the excel

>> macro that I have
>> >inserted into the code to work with Access VBA.
>> >
>> >Any help would be greatly appreciated.
>> >
>> >
>> >Private Sub copy_excel_sheet_Click()
>> >Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
>> >Set xlsApp = CreateObject("Excel.Application")
>> >Set xlsWB = xlsApp.Workbooks.Open

("C:\excel_file.xls", ,
>> True)
>> > '
>> > ' Excel Macro
>> > '
>> > Sheets("sheet1").Select
>> > Sheets("sheet1").Copy After:=Sheets("sheet1")
>> > Sheets("sheet1 (2)").Select
>> > Sheets("sheet1 (2)").Name = "sheet2"
>> > '
>> >xlsWB.Close False
>> >Set xlsWB = Nothing
>> >xlsApp.Quit
>> >Set xlsApp = Nothing
>> >End Sub
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
Ross Lewis
Guest
Posts: n/a
 
      9th Jun 2004
Thanks Jim,

I found the solution with the following code.

If Me!copy_sheet_1 > "" Then
If Me!copy_sheet_2 > "" Then
'
ExcelFile = DLookup("excel_file", "excel_sheets", "excel_sheets_id = " &
Me!copy_sheet_1)
ExcelSheet = DLookup("excel_sheet", "excel_sheets", "excel_sheets_id = "
& Me!copy_sheet_1)
OldExcelFile = "C:\directory\" & ExcelFile & "\Work\" & ExcelFile &
".xls"
NewExcelFile = "C:\directory\" & ExcelFile & "\Work\" & ExcelFile &
"b.xls"
'MsgBox ExcelDirectory & " - " & NewExcelFile
'
Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
Set xlsApp = CreateObject("Excel.Application")
Set xlsWB = xlsApp.Workbooks.Open(OldExcelFile, , True)
xlsWB.WorkSheets(ExcelSheet).Select
xlsWB.WorkSheets(ExcelSheet).Copy
After:=xlsWB.WorkSheets(ExcelSheet)
xlsWB.WorkSheets(ExcelSheet & " (2)").Select
xlsWB.WorkSheets(ExcelSheet & " (2)").Name = Me!copy_sheet_2
xlsWB.SaveAs Filename:=NewExcelFile
xlsWB.Close False
Set xlsWB = Nothing
xlsApp.Quit
Set xlsApp = Nothing
Kill OldExcelFile
FileCopy NewExcelFile, OldExcelFile
Kill NewExcelFile
MsgBox "Done"
'
End If
End If

"JimB" <(E-Mail Removed)> wrote in message
news:1a4ed01c44e34$f799bc50$(E-Mail Removed)...
> Ross, try this:
>
> objExcel.Worksheets(1).Activate
> objExcel.Worksheets(1).Range("a1:xx").Copy
> objExcel.ActiveSheet.Paste
> Destination:=objExcel.Worksheets(2).Range("a1:xx")
> ' Save the Workbook
> objBook.SaveAs WhereToReport & ".xls"
> ' Close the Workbook
> objBook.Close
>
> Substitute xx with the last data field cell range. Od
> give a max cell range if your data varies.
>
> >-----Original Message-----
> >I am trying to copy sheet1 to sheet2 in the same workbook.
> >
> >"JimB" <(E-Mail Removed)> wrote in

> message
> >news:1a07801c44d9a$9f9fd150$(E-Mail Removed)...
> >> Are you trying to copy sheet1 to sheet2 in the same
> >> workbook or copy sheet1 into a new workbook?
> >>
> >> >-----Original Message-----
> >> >I am trying to create a copy an excel sheet and give

> it a
> >> new name (from a
> >> >button in Access).
> >> >The main code listed below, I have used before for
> >> extracting information
> >> >from Excel.
> >> >What I would like to know is how to convert the excel
> >> macro that I have
> >> >inserted into the code to work with Access VBA.
> >> >
> >> >Any help would be greatly appreciated.
> >> >
> >> >
> >> >Private Sub copy_excel_sheet_Click()
> >> >Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
> >> >Set xlsApp = CreateObject("Excel.Application")
> >> >Set xlsWB = xlsApp.Workbooks.Open

> ("C:\excel_file.xls", ,
> >> True)
> >> > '
> >> > ' Excel Macro
> >> > '
> >> > Sheets("sheet1").Select
> >> > Sheets("sheet1").Copy After:=Sheets("sheet1")
> >> > Sheets("sheet1 (2)").Select
> >> > Sheets("sheet1 (2)").Name = "sheet2"
> >> > '
> >> >xlsWB.Close False
> >> >Set xlsWB = Nothing
> >> >xlsApp.Quit
> >> >Set xlsApp = Nothing
> >> >End Sub
> >> >
> >> >
> >> >.
> >> >

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy and rename MS Excel sheet from MS Access VBA (MS Office 2000) Alek Luchnikov Microsoft Access 1 31st May 2007 11:44 AM
How to copy and rename MS Excel sheet from MS Access VBA (MS Office 2000) Alek Luchnikov Microsoft Access Getting Started 2 31st May 2007 09:12 AM
Copy Excel Sheet to another sheet and preserve formatting? =?Utf-8?B?UmljaA==?= Microsoft Excel Programming 4 4th May 2007 06:12 PM
Copy an Excel sheet to another workbood with access vba Nick Microsoft Access VBA Modules 2 18th Aug 2006 03:22 PM
How do I copy a print format from sheet to sheet in excel ? =?Utf-8?B?a2VybmF0?= Microsoft Excel Misc 1 22nd Jul 2005 04:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:21 AM.