Wikiposts
Search

Notices
Computer/Internet Issues & Troubleshooting Anyone with questions about the terribly complex world of computers or the internet should try here. NOT FOR REPORTING ISSUES WITH PPRuNe FORUMS! Please use the subforum "PPRuNe Problems or Queries."

Excel VBA Query

Thread Tools
 
Search this Thread
 
Old 16th June 2011 | 10:58
  #1 (permalink)  
Thread Starter
Per Ardua ad Astraeus
 
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Excel VBA Query

Any VBA gurus please -

I have a macro which works with data from 3 worksheets in a book. I wish to save only worksheets 1 and 2 to a new workbook.

At the moment I save (all 3) with

ActiveWorkbook.SaveAs TITLE

I have had some but not all success with

ActiveWorkbook.ActiveSheet("Sheet1").SaveAs TITLE
BOAC is offline  
Reply
Old 16th June 2011 | 12:10
  #2 (permalink)  
 
Joined: May 2011
Posts: 56
Likes: 0
From: London
At last I can contribute something of value:

There are two ways of talking to a worksheet in a workbook either by its index:

ActiveWorkbook.Worksheets(1).SaveAs “Title”

Or by its name

ActiveWorkbook.Worksheets(“WorksheetName”).SaveAs “Title”

The worksheets are indexed 1 to n, the 1st worksheet is the leftmost in the viewing pane, if you move Sheet2 in front of Sheet1 it will become worksheets(1)

Equivalently Workbooks("WorkbookName") is preferred to ActiveWorkbook, as it will error rather than do something irreversible to the activeworkbook
PhineasC is offline  
Reply
Old 16th June 2011 | 13:06
  #3 (permalink)  
 
Joined: May 2011
Posts: 56
Likes: 0
From: London
This sub will create a new workbook and copy Sheet1, Sheet2 and Sheet3 to the new workbook. As Sheet1 etc. exist in the new workbook then copied sheets will be named Sheet1(1) etc. If this is an issue we could either delete Sheet1 etc. from the new workbook before the copy or rename the sheets in the new workbook

Sub CreateAndCopy()
''' Define this wokbook
Dim wbkSource As Workbook
Set wbkSource = ActiveWorkbook

''' Create a new workbook

Dim wbkTarget As Workbook
Workbooks.Add
Set wbkTarget = ActiveWorkbook

''' Copy the worksheets accross backwards

wbkSource.Worksheets("Sheet3").Copy
AfterwbkTarget.Sheets(3)
wbkSource.Worksheets("Sheet2").Copy
AfterwbkTarget.Sheets(3)
wbkSource.Worksheets("Sheet1").Copy
AfterwbkTarget.Sheets(3)

''' Save and close the new workbook

wbkTarget.SaveAs "Target"
wbkTarget.Close

End Sub



To rename a sheet the syntax
Sheets("Sheet1").Name = "NewName"

The : = syntax id reproduced as

Happy to private if you need further advice
PhineasC is offline  
Reply
Old 16th June 2011 | 16:19
  #4 (permalink)  
Thread Starter
Per Ardua ad Astraeus
 
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Thanks indeed, Phineas - a great help - no PMs necessary!
BOAC is offline  
Reply
Old 16th July 2011 | 14:37
  #5 (permalink)  
Thread Starter
Per Ardua ad Astraeus
 
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
I need to beg some more VBA help - my thanks indeed to those who helped before.

I am 'firing on all four' now but am stuck on FTP issues. I am able to FTP files up and down, creating the necessary TEXTFILE to "get" or "put" inthe code and using "FTPcommand = "ftp -n -s -v:" & Chr(34) & TEXTFILE & Chr(34)" to execute the up/down request.

What I would like to do is use the same procedure to list the files on my server and add them to a listbox.but I cannot work out how to use "dir" to achieve this. If anyone can help I can PM the current lines of code. Google seem to be devoid of simple subs or functions.
BOAC is offline  
Reply
Old 17th July 2011 | 05:31
  #6 (permalink)  
15 Anniversary
 
Joined: Jun 2009
Posts: 1,344
Likes: 80
From: Bedford, UK
VBA for Dummies: Amazon.co.uk: John Paul Mueller: Books VBA for Dummies: Amazon.co.uk: John Paul Mueller: Books
Mr Optimistic is offline  
Reply
Old 17th July 2011 | 07:07
  #7 (permalink)  
Thread Starter
Per Ardua ad Astraeus
 
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
R O - are you saying this is definitely covered in that book? I am on my 4th VBA library book and none have so far.
BOAC is offline  
Reply
Old 17th July 2011 | 11:05
  #8 (permalink)  
15 Anniversary
 
Joined: Jun 2009
Posts: 1,344
Likes: 80
From: Bedford, UK
BOAC, '4th VBA library book' ! What with the re-use of old PATA drives I am getting the impression of penury at your end.

Re book, claims to on CD. May just be a little beginners for you but I liked it (although the faux matey style of these things is nauseating). There is on on VBA for excel (but which version of excel I don't know).

Hope your sitting down, the first of these retails at £22

Excel VBA Programming for Dummies: Amazon.co.uk: John Walkenbach: Books Excel VBA Programming for Dummies: Amazon.co.uk: John Walkenbach: Books

VBA for Dummies: Amazon.co.uk: John Paul Mueller: Books VBA for Dummies: Amazon.co.uk: John Paul Mueller: Books
Mr Optimistic is offline  
Reply
Old 17th July 2011 | 18:13
  #9 (permalink)  
Thread Starter
Per Ardua ad Astraeus
 
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Mr O - not 'extreme poverty - just a little cautious. Your suggestion is noted, thanks. I am receiving excellent help also from PhineasC, but I will be prepared to order that book from my local library........................
BOAC is offline  
Reply
Old 17th July 2011 | 20:41
  #10 (permalink)  
15 Anniversary
 
Joined: Jun 2009
Posts: 1,344
Likes: 80
From: Bedford, UK
Well I guess there is never any harm in being careful.
Mr Optimistic is offline  
Reply

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 Off
Pingbacks are Off
Refbacks are Off



Contact Us - Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service

Copyright © 2026 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.