save filtered range to text file

rkhong

New Member
Joined
Aug 28, 2018
Messages
2
There are 6 columns and I'm using autofilter to filter from 2 columns, however I want to copy and paste another column (after filtering) to a text file.





' Getting the day of the current date
var = Day(Date)

'Autofilter
ActiveSheet.Range("A:F").AutoFilter Field:=3, Criteria1:="=Daily", _
Operator:=xlOr, Criteria2:="=var"


'Get the number of rows in Report column
LastRow = ActiveSheet.UsedRange.Rows.Count

'Loop through the column and paste the report names in the txt file
For iCntr = 2 To LastRow
Write #1 , Range("F" & iCntr)

Next iCntr
Close #1


The loop pastes all the values in COLUMN F. But I want to paste only filtered values.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the forum

Try this
(untested)

Code:
Dim[COLOR=#ff0000] rng [/COLOR]As Range,[COLOR=#ff0000] cel [/COLOR]As Range

Var = Day(Date)

[I]'Autofilter[/I]
ActiveSheet.Range("A:F").AutoFilter Field:=3, Criteria1:="=Daily", _
Operator:=xlOr, Criteria2:="=var"

[I]'Get the number of rows in Report column[/I]
lastrow = ActiveSheet.UsedRange.Rows.Count

[COLOR=#ff0000][I]'consider visible rows only[/I]
[/COLOR]Set [COLOR=#ff0000]rng[/COLOR] = ActiveSheet.Range("F2:F" & lastrow)[COLOR=#ff0000].SpecialCells(xlCellTypeVisible)[/COLOR]

[I]'Loop through the column and paste the report names in the txt file[/I]
For Each [COLOR=#ff0000]cel [/COLOR]In [COLOR=#ff0000]rng
   [/COLOR] Write [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] [COLOR=#ff0000], cel.Value
[/COLOR]Next[COLOR=#ff0000] cel[/COLOR]
Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,599
Members
449,460
Latest member
jgharbawi

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