Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50307

[SOLVED] Macro wont run on all sheets in active workbook

$
0
0
I have searched these forums for help on this and tried many of the suggestions given, none of witch has helped. The macro runs on the active sheet but not on all of them. Here is the code Im using.

Code:

Option Explicit
Sub Macro1()
    '
   
    Dim MYFILE As String
    Dim Last_Column As Integer
    Dim Last_Row As Long
    Dim FileNum As Integer
    Dim My_Range As Range
    Dim My_Cell As Variant
    Dim ws As Worksheet
   
    For Each ws In ActiveWindow.SelectedSheets
             
        MYFILE = ActiveSheet.Name
        FileNum = FreeFile
   
        With ActiveSheet.Cells
            Last_Column = .Find("*", [A1], , , xlByColumns, xlPrevious).Column
            Last_Row = .Find("*", [A1], , , xlByRows, xlPrevious).Row
        End With
   
        Set My_Range = ActiveSheet.Range("A2:A" & Last_Row)
       
        Open MYFILE & "_PG00.txt" For Output As #FileNum
        For Each My_Cell In My_Range
            If My_Cell.Row Mod 1000 = 0 Then
                Close #FileNum
                Open MYFILE & "_PG0" & (My_Cell.Row \ 1000) & ".txt" For Output As #FileNum
            End If
            Print #FileNum, My_Cell.Value
        Next
        Close #FileNum
       
    Next ws
   
End Sub


Viewing all articles
Browse latest Browse all 50307

Latest Images

Trending Articles



Latest Images