Solved

EXCEL VBA

Posted on 2014-02-22
2
393 Views
Last Modified: 2014-02-22
Hello,
can you please help,
I'm trying to use below code to get  each sheet saved.
I get the sheets saved, but each sheet has the information from previous sheet as well.

meaning,
sheet 1 is ok,
sheet 2, it is saving sheet 1 range + sheet 2 range
sheet 3 is saving sheet 1 range + sheet 2 range + sheet 3 range
and so on.

I need each sheet with only the range in the sheet.

Sub TEST()
Dim wshH As Worksheet
    Dim c As Range, r As Range
    Dim output As String
    Dim LastRowW As Long
   
For Each wshH In Worksheets
If wshH.Name <> "58" And wshH.Name <> "1" Then
wshH.Select
    LastRowW = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A2:A" & LastRowW).Select

For Each r In Range("A2:A" & LastRowW).Rows

        For Each c In r.Cells
            output = output & "'" & c.Value & "',"
        Next c
        output = output '& vbNewLine
Next r
    Open "C:\Users\Wassim\Desktop\" & wshH.Name & ".txt" For Output As #1
    Print #1, output
    Close
End With

End If
Next wshH
End Sub

thank you
0
Comment
Question by:W.E.B
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 total points
ID: 39879245
You are not resetting the 'output' string for reach sheet. Try this:
Sub TEST()
    Dim wshH As Worksheet
    Dim c As Range, r As Range
    Dim output As String
    Dim LastRowW As Long
   
    For Each wshH In Worksheets
        output = "" '<---- set string to empty
        If wshH.Name <> "58" And wshH.Name <> "1" Then
            wshH.Select
            LastRowW = Range("A" & Rows.Count).End(xlUp).Row
            With Range("A2:A" & LastRowW).Select
                For Each r In Range("A2:A" & LastRowW).Rows
                    For Each c In r.Cells
                        output = output & "'" & c.Value & "',"
                    Next c
                    output = output '& vbNewLine
                Next r
                Open "C:\Users\Wassim\Desktop\" & wshH.Name & ".txt" For Output As #1
                Print #1, output
                Close
            End With
        End If
    Next wshH
End Sub

Open in new window

0
 

Author Closing Comment

by:W.E.B
ID: 39879247
Great
thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question