Copy does not get last visible row

James Snyder

Well-known Member
Joined
Jan 11, 2013
Messages
618
I am losing the last row of data when using '.SpecialCells(xlCellTypeVisible).Copy'. Since this is not a range I set, I am wondering if others have found this and have a workaround. Below are links to screenshots showing the problem:
Losing NULLs 1 - Bermex's library
Losing NULLs 2 - Bermex's library
Losing NULLs 3 - Bermex's library

Code:
            With ftpWkSht
                ' If we have a valid workbook and a valid worksheet, find the bottom row
                ftpMax = .UsedRange.Rows.Count
                Set ftpRange = .Range(.Cells(1, 1), .Cells(ftpMax, ftpMaxCol).End(xlUp))

                With ftpRange
                    .AutoFilter
                    .AutoFilter field:=meterRdCol, Criteria1:=vbNullString ' or "="

                    ' Make a new sheet and copy filtered data to it to be able to address each row
                    .SpecialCells(xlCellTypeVisible).Copy
                    Set filtSheet = Sheets.Add(After:=Sheets(1))
                    filtSheet.name = fltShtName
                    filtSheet.Paste
                    Application.CutCopyMode = False         ' Clear clipboard of copied data
Since the .AutoFilter catches everything as it should, it seems to be a failure of the .SpecialCells function or the labeling of the last row as 'visible'. Any help?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I don't see where ftpmaxcol is initialized.

Code:
    Set ftpRange = .Range("A1", .Cells(ftpMax, ftpMaxCol).End(xlUp))

    With ftpRange
        .Select    
        Stop    ' is it correct?
        .AutoFilter
 
Upvote 0
No problem. It just used UsedRange to get to the bottom row:
Code:
Private Function HandleNullReads(ByRef ftpWkBk As Workbook, _
    ByRef exceptArray() As String, _
    ByRef exceptFile As String, _
    ByRef sendDate As String) As String


    Dim ftpWkSht As Worksheet
    Dim filtSheet As Worksheet
    Dim ftpRange As Range
    Dim filtRange As Range
    Dim filtMax As Long
    Dim ftpMax As Long
    [COLOR=#006400][B]Dim ftpMaxCol As Long[/B][/COLOR]
    Dim meterRdCol As Long
    Dim i As Long
    Dim fltShtName As String
    Dim failReturn As String
    Dim errString As String

    fltShtName = "Filtered"
    meterRdCol = 6
    [B][COLOR=#006400]ftpMaxCol = 10[/COLOR][/B]

    ' Up front prep of FTP workbook and sheet
    If Not ftpWkBk Is Nothing Then
        ftpWkBk.Activate
        Set ftpWkSht = ftpWkBk.Sheets(1)
        If Not ftpWkSht Is Nothing Then
            HandleNullReads = "Success"

            With ftpWkSht
                ' If we have a valid workbook and a valid worksheet, find the bottom row
                ftpMax = .UsedRange.Rows.Count
                Set ftpRange = .Range(.Cells(1, 1), .Cells(ftpMax, ftpMaxCol).End(xlUp))

                With ftpRange
                    .AutoFilter
                    .AutoFilter field:=meterRdCol, Criteria1:=vbNullString ' or "="

                    ' Make a new sheet and copy filtered data to it to be able to address each row
                    .SpecialCells(xlCellTypeVisible).Copy
                    Set filtSheet = Sheets.Add(After:=Sheets(1))
                    filtSheet.name = fltShtName
                    filtSheet.Paste
                    Application.CutCopyMode = False         ' Clear clipboard of copied data

                    With filtSheet
                        Set filtRange = filtSheet.Range("A2:J" & filtSheet.Range("A65535").End(xlUp).Row)
                        filtMax = filtRange.Range("A65535").End(xlUp).Row

                        If filtMax > 1 Then   ' Only header row, don't process
                            With filtRange
                                For i = 1 To filtMax
                                    If .Range("C" & i) = "Inspection Completed" Then
                                        'Add to exceptions report and remove - no Inspection Completed with a _
                                            NULL meter reading is valid
                                        failReturn = WriteException(.Range("A" & i).Value, exceptArray)
                                        errString = .Range("A" & i) & "       'Inspection Completed' without a meter reading"
                                        failReturn = ProblemReport(errString, exceptFile, sendDate)
                                    Else
                                        If .Range("J" & i) Is Nothing Then  ' Check for valid 'Obstructed Code'
                                            ' Add to exceptions report
'                                            failReturn = WriteException(.Range("A" & i).Value, exceptArray)
                                            errString = .Range("A" & i) & "       'Obstructed Meter' without a meter reading"
                                            errString = errString & " or 'Obstructed Code'"
                                            failReturn = ProblemReport(errString, exceptFile, sendDate)
                                        End If
                                    End If
                                Next i
                            End With    ' filtRange
                        Else
                            Application.DisplayAlerts = False       ' Suppress "SaveAs" dialog box
                            Application.EnableEvents = False        ' Suppress BeforeSave event
                            filtSheet.Delete
                            Application.EnableEvents = True
                            Application.DisplayAlerts = True
                            ftpRange.AutoFilter
                            Set filtSheet = Nothing
                            Set ftpWkSht = Nothing
                            Set ftpRange = Nothing
                            
                            Workbooks("DailyFTP.xlsm").Sheets("DailyFTP").Activate
                            Exit Function
                        End If  ' filtMax > 1
                    End With    ' filtSheet
                End With    ' ftpRange
            End With    ' ftpWkSht

            ActiveSheet.AutoFilterMode = False
            Application.DisplayAlerts = False       ' Suppress "SaveAs" dialog box
            Application.EnableEvents = False        ' Suppress BeforeSave event
            ftpRange.AutoFilter
            filtSheet.Delete
            Application.EnableEvents = True
            Application.DisplayAlerts = True
            Set filtSheet = Nothing
            Set ftpWkSht = Nothing
            Set ftpRange = Nothing
            Set filtRange = Nothing
        Else
            errString = "NULL meter read worksheet object is not set"
            failReturn = ProblemReport(errString, exceptFile, sendDate)
            HandleNullReads = errString
        End If
    Else
        errString = "NULL meter read workbook object is not set"
        failReturn = ProblemReport(errString, exceptFile, sendDate)
        HandleNullReads = errString
    End If
    
    Workbooks("DailyFTP.xlsm").Sheets("DailyFTP").Activate
End Function    ' HandleNullReads
 
Upvote 0
Try this then adapt to fit in with the rest of your code
Code:
[color=darkblue]Sub[/color] TestIt()
    [color=darkblue]Dim[/color] LstRw [color=darkblue]As[/color] [color=darkblue]Long[/color], meterRdCol [color=darkblue]As[/color] [color=darkblue]Long[/color], ftpRange [color=darkblue]As[/color] Range, ftpWkSht [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] ftpMaxCol [color=darkblue]As[/color] [color=darkblue]Long[/color], filtSheet [color=darkblue]As[/color] Worksheet, fltShtName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]If[/color] [color=darkblue]Not[/color] ftpWkBk [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        ftpWkBk.Activate
        [color=darkblue]Set[/color] ftpWkSht = ActiveWorkbook.Sheets(1)
    LstRw = ftpWkSht.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    fltShtName = "Filtered"
    ftpMaxCol = 10
    meterRdCol = 6
    [color=darkblue]Set[/color] ftpRange = ftpWkSht.Range(Cells(1, 1), Cells(LstRw, ftpMaxCol))
    [color=darkblue]With[/color] ftpRange
        .AutoFilter field:=meterRdCol, Criteria1:="="
        .SpecialCells(xlCellTypeVisible).Copy
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Set[/color] filtSheet = Sheets.Add(After:=Sheets(1))
    filtSheet.Name = fltShtName
    filtSheet.Paste
    Application.CutCopyMode = [color=darkblue]False[/color]
   ftpWkSht.AutoFilterMode = 0
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0
Post a copy of your worksheet on a file hosting site like box.com remember to set it up for sharing and copy a link to the file in the thread.
 
Upvote 0
Wish I could! The spreadsheet has paths, passwords, email addresses and cannot be sent.
Post a copy of your worksheet on a file hosting site like box.com remember to set it up for sharing and copy a link to the file in the thread.
 
Upvote 0
Then make a sample workbook with the same layout but with the confidential paths, passwords, email addresses scrubbed/removed and post a link to that.
There must be something strange with your data/layout
I have run the code below
Code:
Sub TestIt()
    Dim LstRw As Long, meterRdCol As Long, ftpRange As Range, ftpWkSht As Worksheet
    Dim ftpMaxCol As Long, filtSheet As Worksheet, fltShtName As String
Set ftpWkSht = ActiveWorkbook.Sheets("Sheet1")
    LstRw = ftpWkSht.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    fltShtName = "Filtered"
    ftpMaxCol = 10
    meterRdCol = 6
Set ftpRange = ftpWkSht.Range(ftpWkSht.Cells(1, 1), ftpWkSht.Cells(LstRw, ftpMaxCol))
    With ftpRange
        .AutoFilter field:=meterRdCol, Criteria1:="="
        .SpecialCells(xlCellTypeVisible).Copy
    End With
    Set filtSheet = Sheets.Add(After:=Sheets(1))
    filtSheet.Name = fltShtName
    filtSheet.Paste
    Application.CutCopyMode = False
   ftpWkSht.AutoFilterMode = 0
End Sub
on this

Excel Workbook
ABCDEFGHIJ
1aaaaaaaaaa
2bbbbbbbbbb
31bbbbbbbb
42bbbbbbbb
53bbbbbbbb
64bbbbbbbb
73ccccccccc
84ddddddddd
9cccccccccc
10dddddddddd
115dddddddd
126dddddddd
137dddddddd
148dddddddd
15cccccccccc
169dddddddd
Sheet1


and get this every time
Excel Workbook
ABCDEFGHIJ
1aaaaaaaaaa
21bbbbbbbb
32bbbbbbbb
43bbbbbbbb
54bbbbbbbb
65dddddddd
76dddddddd
87dddddddd
98dddddddd
109dddddddd
Filtered
 
Upvote 0
Afraid I can't be of any help because when I put this code in the workbook.
Code:
[color=darkblue]Sub[/color] TestIt()
    [color=darkblue]Dim[/color] LstRw [color=darkblue]As[/color] [color=darkblue]Long[/color], meterRdCol [color=darkblue]As[/color] [color=darkblue]Long[/color], ftpRange [color=darkblue]As[/color] Range, ftpWkSht [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] ftpMaxCol [color=darkblue]As[/color] [color=darkblue]Long[/color], filtSheet [color=darkblue]As[/color] Worksheet, fltShtName [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Set[/color] ftpWkSht = ActiveWorkbook.Sheets("DailyFTP")
    LstRw = ftpWkSht.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    fltShtName = "Filtered"
    ftpMaxCol = 10
    meterRdCol = 6
[color=darkblue]Set[/color] ftpRange = ftpWkSht.Range(ftpWkSht.Cells(1, 1), ftpWkSht.Cells(LstRw, ftpMaxCol))
    [color=darkblue]With[/color] ftpRange
        .AutoFilter field:=meterRdCol, Criteria1:="="
        .SpecialCells(xlCellTypeVisible).Copy
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Set[/color] filtSheet = Sheets.Add(After:=Sheets(1))
    filtSheet.name = fltShtName
    filtSheet.Paste
    Application.CutCopyMode = [color=darkblue]False[/color]
   ftpWkSht.AutoFilterMode = 0
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

and threw a few bits of info in the cells i.e.

Excel Workbook
ABCDEF
1PathsFilesFTP SiteUsernamePasswordCompleted Inspections
21
32
43
54
6d01/02/2013
7d02/02/2013
8d03/02/2013
9d04/02/2013
10dd05/02/2013
11d06/02/2013
12d507/02/2013
136
147
158
16g05/06/2013
17g06/06/2013
18g07/06/2013
19gg08/06/2013
20g09/06/2013
21g10/06/2013
22g11/06/2013
23g12/06/2013
249
2510
DailyFTP


I got
Excel Workbook
ABCDEFGH
1PathsFilesFTP SiteUsernamePasswordCompleted InspectionsExceptionsRecon
21
32
43
54
66
77
88
99
1010
Filtered

and so I can't reproduce the issue. Hope you get a solution
 
Upvote 0

Forum statistics

Threads
1,216,070
Messages
6,128,614
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