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  dBforums > PC based Database Applications > Microsoft Excel > Yet another excel macro

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-02-04, 09:29
doctor doctor is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
(RESOLVED)Yet another excel macro

Hey I need some quick guidance for a excel macro..

I need a macro that does this:

1) Selects sheet2
2) Copies all of sheet 2 to sheet 1
3) Deletes sheet2
3) Selects sheet 3
3) Copies all fo sheet 2 into next available space on sheet 1
4) Deletes sheet 3
......

Here is the problem though... how can we automate this process because all the sheet names have no numbers and there are 99 sheets. is there a NextSheet command? Im Very new to VBA and dont have any books to reference so thats why im here Any help will be much appreicated.

Doc

Last edited by doctor; 07-02-04 at 10:42.
Reply With Quote
  #2 (permalink)  
Old 07-02-04, 10:18
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
Hi Doc

Try something like this

Code:
Sub Test()
    Dim I As Integer
    
    For I = 2 To Worksheets.Count
        'copy from worksheet
        Worksheets(I).Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) _
                .EntireRow.Copy
        'paste to new worksheet
        Worksheets(1).Range("A" & Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row) _
                .PasteSpecial
    Next I
    
    For I = Worksheets.Count To 2 Step -1
        Application.DisplayAlerts = False
        'delete all worksheets bar the first
        Worksheets(I).Delete
        Application.DisplayAlerts = True
    Next I
    
        
End Sub
Hope this helps
David
Reply With Quote
  #3 (permalink)  
Old 07-02-04, 10:42
doctor doctor is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
CHAAAA CHIING .. you the man!
Reply With Quote
  #4 (permalink)  
Old 07-02-04, 10:45
ioclio ioclio is offline
Registered User
 
Join Date: Jun 2004
Location: Italy
Posts: 15
Hi Doctor,

to go through your sheets you can use:

Do
On error GoTo EndOfSheets
Worksheets(1).Activate
ActiveSheet.Next.Activate
'You can also use ActiveSheet.Previous.Activate
...
Loop


EndOfSheets:
MsgBox "End of sheets"
Reply With Quote
  #5 (permalink)  
Old 07-02-04, 10:47
ioclio ioclio is offline
Registered User
 
Join Date: Jun 2004
Location: Italy
Posts: 15
Come on David,
you answered while I was writing...that's not fair :-)
By the way, that's a good solution
Reply With Quote
  #6 (permalink)  
Old 07-02-04, 10:58
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
Quote:
Originally Posted by ioclio
Come on David,
you answered while I was writing...that's not fair :-)
By the way, that's a good solution
it comes with practice and iritation at how slow things are running

it's best not to activate sheets select ranges etc slows thing way down
trying to create macros without this is always a good idea



David
Reply With Quote
  #7 (permalink)  
Old 07-02-04, 11:57
ioclio ioclio is offline
Registered User
 
Join Date: Jun 2004
Location: Italy
Posts: 15
Thanks for the advise.
FatherXmas
Reply With Quote
  #8 (permalink)  
Old 07-15-04, 14:41
doctor doctor is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
Hmm using

Code:
Sub Test()
    Dim I As Integer
    
    For I = 2 To Worksheets.Count
        'copy from worksheet
        Worksheets(I).Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) _
                .EntireRow.Copy
        'paste to new worksheet
        Worksheets(1).Range("A" & Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row) _
                .PasteSpecial
    Next I
    
    For I = Worksheets.Count To 2 Step -1
        Application.DisplayAlerts = False
        'delete all worksheets bar the first
        Worksheets(I).Delete
        Application.DisplayAlerts = True
    Next I
    
        
End Sub
In Excel works just fine. But now i need to call this from access, and to be honest im not good at calling excel macros from access, ive done it before but im having particular trouble with this one... can anyone help me out?? I know we need to append objects to their respective ".methods" but with the "Worksheets" its acting funny.

Any help would be much appreciated.

Thanks!
Doc
Reply With Quote
  #9 (permalink)  
Old 07-15-04, 14:42
doctor doctor is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
oops I think this should be in the Access VBA forum sorry ill repost there with a link to this one, but if any of you know how to solve my problem feel free to help me out

Last edited by doctor; 07-15-04 at 15:24.
Reply With Quote
  #10 (permalink)  
Old 07-16-04, 04:29
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
Try this

Add a reference to Excel

Code:
Option Compare Database
Option Explicit

Private Sub cmdRun_Click()
    Dim xlApp As Excel.Application
    Dim xlbook As Workbook
    
    Set xlApp = New Excel.Application
    Set xlbook = xlApp.Workbooks.Open("C:\yourfilepath\yourfilename.xls")
    
    xlApp.Application****n ("yourfilename.xls'!yourmacroname")
    
    xlApp.Visible = True
    
    Set xlbook = Nothing
    Set xlApp = Nothing
    
End Sub
ive atached this to a command button
HTH
David
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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