Solved

EXCEL VBA

Posted on 2014-02-22
2
392 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

825 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