Only process filtered items

Sebas123

New Member
Joined
Aug 12, 2011
Messages
4
Hello again,

How can you let vba process "only" the filtered data (on my filtered excel sheet). The code executes and it stil executes it on all the data.


Here is an example of my code. In the sheet of this document are filtered columns. Even with filter-on, vb still processes the whole document.

Code:
Sub ExportTXT()
 
Dim rownum, colnum As Integer
Dim filename, Filelocation As String
Dim ws As Worksheet
 
Set ws = Worksheets("Sheet")
Filelocation = "C:\"
 
 With ws
        rownum = 3
        While .Cells(rownum, 1) <> ""
            filename = .Cells(rownum, 1).Value
            Open (Filelocation & filename & ".txt") For Output As #1
 
            colnum = 2
            While .Cells(rownum, colnum) <> 0
                 Print #1, ws.Cells(rownum, colnum) & vbNewLine;
                colnum = colnum + 1
            Wend
 
            Close #1
            rownum = rownum + 1
        Wend
 
    End With
 
End Sub

Can someone help me?

Thanks,
Sebas123
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Sebas123,

Give the following a try:
Code:
Sub ExportTXT()
    
    Const fPath As String = "C:\"
    Static rng As Range: Set rng = Intersect(ActiveSheet.UsedRange, Range("A3", Cells(Rows.Count, "A")))
    
    Dim rCell As Range
    Dim colnum As Long
    
    On Error GoTo NoneVisible
    For Each rCell In rng.SpecialCells(xlCellTypeVisible)
        Open fPath & rCell.Value & ".txt" For Output As #1
        colnum = 2
        While Cells(rCell.Row, colnum) <> 0
            Print #1, Cells(rCell.Row, colnum) & vbNewLine;
            colnum = colnum + 1
        Wend
        Close #1
    Next rCell
    Exit Sub
    
NoneVisible:
    MsgBox "No visible cells found"
    Exit Sub
    
End Sub



Hope that helps,
~tigeravatar
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top