VBA Filtered LastRow to specific row where the header is located

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I am having a problem where I am trying to add specific text to a filtered column in the visible cells only but the code below adds the text below the used range. I would like to only add the text to the filtered visible cells not including the header, can some one help with this please

Code:
Sub AddText()
    Dim xTitleId As Variant, Rng As range, Rng2 As range, x As range, i As range, c As range, addStr As String
    Application.ScreenUpdating = False
    For Each i In range("A1:Z2")
    Select Case i.value2
    Case "IPN", "Part", "Part Number"
        If Not Rng Is Nothing Then
            Set Rng = Union(Rng, i)
        Else
            Set Rng = i
        End If
            End Select
        Next
    If Rng Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    If Not Rng Is Nothing Then
        Set Rng = Rng.Resize(Cells(Rows.Count, Rng.Column).End(xlUp).row).Offset(1)
    Rng.AutoFilter Field:=1, Criteria1:="<>*DNF_*", Operator:=xlAnd, Criteria2:="<>*FID_*"
    For Each i In range("A1:Z2")
    Select Case i.value2
    Case "IPN", "Part", "Part Number"
        If Not Rng2 Is Nothing Then
            Set Rng2 = Union(Rng, i)
        Else
            Set Rng2 = i
        End If
            End Select
        Next
    If Rng2 Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    If Not Rng2 Is Nothing Then
        Set Rng2 = Rng2.Resize(Cells(Rows.Count, Rng.Column).End(xlUp).row).Offset(1)
    On Error Resume Next
    addStr = Application.InputBox("Add Prefix", xTitleId, "", Type:=2)
    If addStr = "False" Then
    ActiveSheet.AutoFilterMode = False
    MsgBox "No Prefixes Added!"
    Exit Sub
    Else
    Application.ScreenUpdating = False
    For Each x In Rng2.SpecialCells (xlCellTypeVisible)
    x.Value = addStr & x.Value
    Next x
    End If
    End If
    End If
    ActiveSheet.AutoFilterMode = False
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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