Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is "If Then" Possible With a Macro?

Status
Not open for further replies.

kc27

Technical User
Sep 10, 2008
171
US
I recorded a simple macro in Excel 2003 to format the columns and rows of a comma separated value report. I'm just bolding some text, widening some columns, and formatting cells to wrap text.

Some of the csv files have 8 columns, some have 9 columns.
If it is a 8 column report, I delete the 4th column (D), and continue styling the report. If it is a 9 column report, I delete the 5th column and continue styling the report.

So my first obstacle is, can the one macro be created that can determine the total number of columns, and depending on the number found, delete either column D or E?

The second obstacle is that I have certain widths I want the specific columns to be. However if I'm starting with a 9 column csv file, column A should be a different width than when column A is created from an 8 column csv report.

Is this one of those situations where giving the user two macros would be the way to go? That is, tell them, if you generate a 9 column csv file, use the 9 column macro, if you generate an 8 column csv file, use the 8 column macro. Ideally, Ideally, I'd like to provide them with one macro that can figure this out for them, but I've never touched the VG Editor so number 1, I don't know if this is possible, and number 2, if possible, how much programming will be need to pull it off. I've attached the code generated from the two macros to this message.

Any advice would be appreciated. Thanks in advance for your time.
 
There are several ways you could design things.

To be sure I understand you - the two reports still have a different number of columns even after you're done with them, right? That is to say, you don't delete a different number of columns from the reports so they wind up looking the same?

Assuming that's right, I'd leave things broken up into two separate macros, but a macro CAN tell you which one to use.

Here's something to get you started:
Code:
Sub AnnotationMaster
    intColcount = Sheets("Sheet1").UsedRange.Columns.Count

    If intColcount = 8 Then Call Format_Annotation_Report
    If intColcount = 9 Then Call Format_Annotation_Report_All

    'Proceed with formatting that is the same for both reports
End Sub

You can include any formatting that is the same regardless of the number of columns - like formatting the header row - in this 'master' macro after (or before) you call the macro specific to either report.

Also, avoid using Select or Activate wherever possible. That will make the code run faster. Unfortunately, the Macro Recorder loves doing it that way.

But you can replace things like this:
Code:
    Columns("F:F").Select
    With Selection
with this:
Code:
    With Columns("F:F")

[tt][blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top