Adding a second sheet to an automated Excel Workbook

Brittany

New Member
Joined
Apr 14, 2003
Messages
7
I have an automated Excel Workbook that takes the data from my MS Access database, sends it to Excel, created a PDF and then closed Excel. Currently, I am only sending data to sheet1. I want to send other data to sheet 2 - does anyone know how to do this?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Howdy Brit, it kind of depends what you're doing. This will push a picture from Form1 to the second sheet:

Code:
Option Explicit

Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" _
    (ByVal lpFileName As String) As Long

'\Lets Use a Constant
Public Const MyLogo = "C:\temp\MyLogo.Emf"

Sub test()
Dim fNum As Integer ' Hold next File#
' Byte arrays to hold the PictureData prop
' Example grabs Image1 from Form: MyLogo
' Original Code and Credit to Stephen Lebans
' Tweaked by Nate Oliver
Dim bArray() As Byte, cArray() As Byte
Dim lngRet As Long
' Excel & pic late bind vars
Dim myPic As Object, myXl As Object
DoCmd.Echo False, "Hold Up"
DoCmd.OpenForm "MyLogo", acNormal, , , acFormEdit
' Resize to hold entire PictureData prop
ReDim bArray(LenB(Forms!MyLogo.Image1.PictureData) - 1)
' Resize to hold the EMF wrapped in the PictureData prop
ReDim cArray(LenB(Forms!MyLogo.Image1.PictureData) - (1 + 8))
' Copy to our array
bArray = Forms!MyLogo.Image1.PictureData
DoCmd.Close acForm, "MyLogo", acSaveNo
DoCmd.Echo True, False
' Copy the embedded EMF - SKIP first 8 bytes
For lngRet = 8 To UBound(cArray) ' - (1) '+ 8)
    cArray(lngRet - 8) = bArray(lngRet)
Next
' Get next avail file handle
fNum = FreeFile
' Let's Create/Open our new EMF File.
Open MyLogo For Binary As fNum
' Write out the EMF FileHeader
Put fNum, , cArray
' Close the File
Close fNum
Set myXl = GetObject(, "Excel.Application")
Set myPic = myXl.sheets(2).Pictures.Insert(MyLogo)
With myPic
    .Top = myXl.sheets(2).[g4].Top
    .ShapeRange.Height = myXl.sheets(2).[g4].RowHeight * 6.2 '6.2 rows tall
    .Left = myXl.sheets(2).[g4].Left - .Width 'Right align at G
End With
Call DeleteFile(MyLogo)
End Sub

You can push data with something like:

Code:
Sub test()
Dim sql As String, Rs As Recordset, myXl As Object, myCnt As Integer
Set myXl = GetObject(, "Excel.Application")
sql = "My Jet SQL String"
Set Rs = CurrentDb.OpenRecordset(sql)
  If Rs.RecordCount > 0 Then
    Rs.MoveLast:    Rs.MoveFirst
    myCnt = Rs.RecordCount
    With myXl.activeworkbook.sheets(2)
        .Range(.Cells(1, 1), .Cells(myCnt, "NumberofColsinRS")).CopyFromRecordset Rs
    End With
End If
Rs.Close
End Sub

You can pull data by creating a second sheet, slamming in a MS query table and refresh it programmatically.

A few ways to pluck this duck. Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,222,697
Messages
6,167,702
Members
452,132
Latest member
Steve T

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top