Thanks Dave I really appreciate your help.
I wanted to give the user a little bit more control so this is how it
ended thanks to your suggestions and the ones from BrianB from
Luton,England from MrExcel Message Boards.
http://www.mrexcel.com/board2/viewto...=848773#848773
Private Sub CommandButton1_Click()
On Error Resume Next
Application.DisplayAlerts = False
Sheets("filtered").Delete
Application.DisplayAlerts = True
Dim wb As Workbook ' this
Dim ws As Worksheet ' this
Dim MyRange As Range
Dim LastRow As Long
Dim FileName As String ' for text file
Dim MyRow As Long
Dim MyCol As Integer
Dim ColumnCount As Integer ' number of columns
'---------------------------------------------------------------
'- initialize variables
Set wb = ThisWorkbook
Set ws = ActiveSheet
LastRow = ws.Range("A65536").End(xlUp).Row
ColumnCount = 2
'----------------------------------------------------------------
'- Copy visible cells
Set MyRange = ws.Range(Cells(1, "A"), _
Cells(LastRow,
ColumnCount)).SpecialCells(xlCellTypeVisible)
MyRange.Copy
'================================================= ==================
'- paste to workbook (containing macro).Could use 'ActiveWorkBook'
?
Sheets.Add Type:=xlWorksheet
ActiveSheet.Name = "filtered"
wb.Worksheets("filtered").Paste _
Destination:=wb.Worksheets("filtered").Range("A1")
'================================================= ==================
'- to a comma delimited text file
FileName = Application.GetSaveAsFilename("C:\", "(*.csv), *.csv", ,
"")
If FileName < False Then
Open FileName For Output As #1
For MyRow = 1 To LastRow ' - 1
If ws.Rows(MyRow).EntireRow.Hidden = False Then
For MyCol = 1 To ColumnCount
If ((MyRange.Cells(MyRow, MyCol).Value = "") And
(MyRange.Cells(MyRow, MyCol + 1).Value = "")) Then
MyCol = ColumnCount
Else
Print #1, MyRange.Cells(MyRow, MyCol).Value;
If MyCol < ColumnCount Then Print #1, ",";
End If
Next
Print #1, ' end of line
End If
Next
'-------------------------------------------------------------------
Close #1
'================================================= ==================
MsgBox ("Done")
End If
End Sub