A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

macro help



 
 
Thread Tools Display Modes
  #1  
Old November 11th, 2009, 04:31 AM posted to microsoft.public.excel.misc
Donna[_5_]
external usenet poster
 
Posts: 69
Default macro help

I have a macro for a report and I want to add a worksheet to the macro. My
question is I already have it typed in an excel worksheet. Is there a way to
copy it in the macro without having to re type it to add it to my existing
macro?
Donna
  #2  
Old November 11th, 2009, 02:39 PM posted to microsoft.public.excel.misc
Dave Peterson
external usenet poster
 
Posts: 19,791
Default macro help

You want to add a worksheet from an existing workbook?

If that existing workbook contains a single sheet:

Dim Sh as object
set sh = sheets.add(type:="c:\pathtothatfile.xls")

Actually, this will add all the sheets in that workbook.

If you wanted to pick out a single sheet, you could use something like:

dim wkbk as workbook
Dim wks as worksheet
dim wkbkName as string
dim wksName as string
dim ActWkbk as workbook

set actwkbk = activeworkbook 'the recipient workbook

wkbkname = "C:\pathtothatfile.xls"
wksname = "somesheetnamehere"

set wkbk = workbooks.open(filename:=wkbkname, readonly:=true)
set wks = wkbk.worksheets(wksname)

wks.copy _
after:=actwkbk.sheets(actwkbk.sheets.count)

wkbk.close savechanges:=false

==========

Untested, uncompiled. Watch for typos.

Donna wrote:

I have a macro for a report and I want to add a worksheet to the macro. My
question is I already have it typed in an excel worksheet. Is there a way to
copy it in the macro without having to re type it to add it to my existing
macro?
Donna


--

Dave Peterson
 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:53 AM.


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