Finding LastRow after filter

maxfli

New Member
Joined
Sep 14, 2010
Messages
26
I am really close to having this process working. I am sending emails with attachments for a table on Sheet1 after I filter on column 12. The problem is
VBA Code:
For Each c In sh.Range("A2", sh.Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
Does not find the last row after the filter is applied. It is finding the entire 1,004,000 rows in the spreadsheet. Can anyone offer a fix to the code.
Thanks


VBA Code:
Sub create_multiple_emails()
  Dim wb As Workbook, sh As Worksheet, c As Range, m As Range
  Dim sBody As String, wFile As String
  Dim dam As Object, dict As Object
  Dim pCode As String
  Dim fileN As String
  Dim sOutTo As String
  Dim sOutSub As String
  Dim sOutInv As String
  Dim sOutCont As String
  Dim sOutDue As String
  Dim sOutFNm As String
  Dim answer As Integer
  Dim lastRow As Long
 
  pCode = InputBox("Enter Pull Code for mailing")
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  '
  Set sh = ActiveSheet
  Set dict = CreateObject("scripting.dictionary")
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  sh.Range("A1").AutoFilter Field:=12, Criteria1:=pCode
  For Each c In sh.Range("A2", sh.Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
        sOutFNm = c.Value
        sOutInv = c.Offset(, 2).Value
        sOutTo = c.Offset(, 7).Value
         sOutCont = c.Offset(, 9).Value
          sOutDue = c.Offset(, 10).Value
    

 
 Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

'On Error Resume Next
With OutMail
.To = sOutTo
.Subject = "AT&T Capital Services invoice #" & sOutInv & " Contract # " & sOutCont & " Due " & sOutDue
.HTMLBody = "<b>" & "INVOICE ATTACHED: Due " & sOutDue & "</b>" & "<br>" _
& "I have attached the AT&T Capital Services invoice #" & sOutInv & " " & ". Please confirm receipt." & "<br>" _
& " Let us know if there are any changes to the billing contact or mailing address on the invoice." & "<br>" _
& " I appreciate your help." & "<br>" _
& "<br>" _
& "<br>" _
& "Thank you" & "<br>" _
& MyName & "<br>" _
& "Capital Services, Inc."
.Attachments.Add sPath & "\" & sOutFNm
.ReadReceiptRequested = True
.OriginatorDeliveryReportRequested = True
.display
Range("P" & Rows.Count) = "Invoice sent to customer on " & Format(Now, "mm-dd-yy") & " " & MyName 'Environ$("username")
End With
  Next
  sh.ShowAllData
   MsgBox "Emails sent"
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You said table, so i wrote something for a table
you have to check first if there is at least 1 cell visible left, otherwise you get an error
VBA Code:
For Each c In sh.Range("A2").ListObject.ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible)
 
Upvote 0
I guess I shouldn’t refer to it as a table. I get an Object variable or with block variable not set.
It is simply rows of data in sheet1.
 
Upvote 0
Just to be sure I turned Off the AutoFilter on field 12. This ensured I had data in the left most A2 cell. I still get the block variable not set error. Is there any other possible solutions???
 
Upvote 0
just an idea without testing
VBA Code:
     With sh.Range("A1").CurrentRegion 'the range around A1
          .AutoFilter Field:=12, Criteria1:=pcode 'filter your 12th column
          For Each c In .Columns(1).SpecialCells(xlCellTypeVisible) 'the remaining visible cells of currentregion
               If c.Row <> .Row Then 'not the headerrow
                   ....
 
Upvote 0
Maybe:

VBA Code:
sh.Range("A1").AutoFilter Field:=12, Criteria1:=pCode
LastRow=sh.AutoFilter.Range.Columns(1).SpecialCells(12).Count-1
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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