Solved

EXCEL VBA

Posted on 2014-02-22
2
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

738 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