PC Review


Reply
Thread Tools Rate Thread

Basic If Statement

 
 
=?Utf-8?B?YmJraXh4?=
Guest
Posts: n/a
 
      23rd Oct 2006
Hello-

I searched around for awhile, but becuase I m not sure exactly what to look
for, I could not figure out a way to do this...

I am using the following macro to create PDFs of sheets in a book.

Dim s As String
s = ActiveCell.Worksheet.Name
ActiveWorkbook.saveAs Filename:=s

Application.Run "EblastMacro2.xls!printAsPDF"

ActiveSheet.Next.Select

s = ActiveCell.Worksheet.Name
ActiveWorkbook.saveAs Filename:=s

Application.Run "EblastMacro2.xls!printAsPDF"

ActiveSheet.Next.Select

How can I write an If statement that will search for the next sheet to run
the macro on, but if there isn't a next sheet, then stop the macro. Right now
it will work fine, but instead of just stopping, it will give me runtime
error message.

Any thoughts?

Thank you,
BBkixx
 
Reply With Quote
 
 
 
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      23rd Oct 2006
dim i as integer

for i = 1 to activeworkbook.sheets.count step 1

' your code

next i


"bbkixx" wrote:

> Hello-
>
> I searched around for awhile, but becuase I m not sure exactly what to look
> for, I could not figure out a way to do this...
>
> I am using the following macro to create PDFs of sheets in a book.
>
> Dim s As String
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> How can I write an If statement that will search for the next sheet to run
> the macro on, but if there isn't a next sheet, then stop the macro. Right now
> it will work fine, but instead of just stopping, it will give me runtime
> error message.
>
> Any thoughts?
>
> Thank you,
> BBkixx

 
Reply With Quote
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      23rd Oct 2006
Sub what_ever()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
''''''''''''''''''''''''''''
'
' your macro here
'
''''''''''''''''''''''''''''
Next
End Sub


in place of the ActiveSheet.Next stuff
--
Gary''s Student


"bbkixx" wrote:

> Hello-
>
> I searched around for awhile, but becuase I m not sure exactly what to look
> for, I could not figure out a way to do this...
>
> I am using the following macro to create PDFs of sheets in a book.
>
> Dim s As String
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> How can I write an If statement that will search for the next sheet to run
> the macro on, but if there isn't a next sheet, then stop the macro. Right now
> it will work fine, but instead of just stopping, it will give me runtime
> error message.
>
> Any thoughts?
>
> Thank you,
> BBkixx

 
Reply With Quote
 
=?Utf-8?B?Q2hhZEY=?=
Guest
Posts: n/a
 
      23rd Oct 2006

Might want to consider the following:

Public Sub WritePagesToPDF()

Dim aWS as Worksheet
Dim aWB as Workbook
Dim SheetName as String

Set aWB = ActiveWorkbook

Foreach aWS in aWB
aWS.Select
SheetName = aWS.Name
aWB.SaveAs SheetName

On Error Resume Next
Application.Run "EblastMacro2.xls!printAsPDF"

Next aWS


End Sub


The 'On Error' statement will avoid the runtime bug you mention ...

Hope this helps,
Chad


"bbkixx" wrote:

> Hello-
>
> I searched around for awhile, but becuase I m not sure exactly what to look
> for, I could not figure out a way to do this...
>
> I am using the following macro to create PDFs of sheets in a book.
>
> Dim s As String
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> s = ActiveCell.Worksheet.Name
> ActiveWorkbook.saveAs Filename:=s
>
> Application.Run "EblastMacro2.xls!printAsPDF"
>
> ActiveSheet.Next.Select
>
> How can I write an If statement that will search for the next sheet to run
> the macro on, but if there isn't a next sheet, then stop the macro. Right now
> it will work fine, but instead of just stopping, it will give me runtime
> error message.
>
> Any thoughts?
>
> Thank you,
> BBkixx

 
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
If then statement in Visual Basic ServiceEnvoy Microsoft Access Form Coding 2 28th Feb 2008 08:57 PM
Basic IF statement? =?Utf-8?B?U2F5V2hhdEF1dG8=?= Microsoft Excel Misc 4 30th Dec 2006 11:37 PM
basic sql sum statement - help i'm going mad Dan Microsoft Access Queries 1 13th Dec 2006 06:41 PM
Basic IF statement question NeonSky via AccessMonster.com Microsoft Access Form Coding 5 30th Nov 2006 03:17 PM
If Statement - Basic Portuga Microsoft Excel Misc 2 23rd Jan 2006 05:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:19 AM.