Solved

EXCEL VBA

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now