Consulting

Results 1 to 2 of 2

Thread: Running a macro for any workbook/worksheet

  1. #1
    VBAX Newbie
    Joined
    May 2007
    Posts
    1
    Location

    Running a macro for any workbook/worksheet

    Hi! I was able to create a macro that formats the worksheet that I've downloaded from a system. However, I would like to run the same macro to different worksheets. I tried saving the macro in the personal.xls file, but it formats the sheet in the personal.xls file. Below is the code that I would like to use for different worksheets:

    Dim RowCount As Integer
    Sub Format()
    Columns("C:C").Select
    Selection.Insert Shift:=x1ToRight
    Columns("I:I").Select
    Selection.Insert Shift:=x1ToRight
    Columns("K:L").Select
    Selection.Cut
    Columns("R:R").Select
    ActiveSheet.Paste
    Columns("K:L").Select
    Selection.Delete Shift:=x1ToLeft
    Columns("L:L").Select
    Selection.Insert Shift:=x1ToRight
    Call HeadingFormat
    Sheet1.Activate
    Rows("1:1").Select
    ActiveSheet.Paste
    Call Autofit
    Windows("Lookup tables.xls").Activate
        Range("C2").Select
        Selection.Copy
    Sheet1.Activate
    Range("C2").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    Range("C2").Select
    Selection.Copy
    RowCount = 3
    Do Until IsEmpty(Cells(RowCount, 2))
    If Cells(RowCount, 2).Value <> EmptyCell Then
    ActiveCell.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    RowCount = RowCount + 1
    End If
    Loop
    
    Windows("Lookup tables.xls").Activate
        Range("I2").Select
        Selection.Copy
    Sheet1.Activate
    Range("I2").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    Range("I2").Select
    Selection.Copy
    RowCount = 3
    Do Until IsEmpty(Cells(RowCount, 8))
    If Cells(RowCount, 8).Value <> EmptyCell Then
    ActiveCell.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    RowCount = RowCount + 1
    End If
    Loop
    End Sub
    Public Sub HeadingFormat()
    Windows("Lookup tables.xls").Activate
        Rows("1:1").Select
        Selection.Copy
    End Sub
    Public Sub Autofit()
    Cells.Select
    Selection.Columns.Autofit
    End Sub
    Please help, it would greatly improve my process and not have to format everytime I download an excel file.

    Thank you so much!

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •