+ Reply to Thread
Results 1 to 9 of 9

Macro to autofilter data then add value to cells within table based on filtered criteria

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Macro to autofilter data then add value to cells within table based on filtered criteria

    Hi,

    Really looking for some assistance so thank you in advance !!

    I have a table (cost) of data on sheet "Raw Data" starting in row 2 , I'm trying to run through a series of filters and apply a value to column BE based on the filtered data.

    The issue I have if the filter returns no data the macro stops and doesn't move onto the next filter criteria.

    The macro has about 10 different filter criterias, the below is just an example

    Sub Scheme()

    'BMW

    Sheets("Raw Data").Select
    ActiveSheet.ListObjects("Cost").AutoFilter.ShowAllData

    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=3, Criteria1:= _
    "=BMW", Operator:=xlOr, Criteria2:="=MINI"
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=11, Criteria1:= _
    "=*MW*", Operator:=xlAnd
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=57, Criteria1:= _
    "=BMW", Operator:=xlAnd

    Dim c As Excel.Range

    For Each c In Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)

    If c.Offset(, -1).Value = vbNullString Then Exit For
    c.Value = "BMW-Scheme"

    Next

    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=57, Criteria1:= _
    "<>BMW-Scheme", Operator:=xlAnd
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=58, Criteria1:= _
    "=B*", Operator:=xlAnd

    For Each c In Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)

    If c.Offset(, -1).Value = vbNullString Then Exit For
    c.Value = "BMW-Scheme"

    Next

    Sheets("Raw Data").Select
    ActiveSheet.ListObjects("Cost").AutoFilter.ShowAllData
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=3, Criteria1:= _
    "=BMW", Operator:=xlOr, Criteria2:="=MINI"
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=57, Criteria1:= _
    "=BMW", Operator:=xlAnd

    For Each c In Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)

    If c.Offset(, -1).Value = vbNullString Then Exit For
    c.Value = "BMW-Non-Scheme"

    Next

    End Sub

  2. #2
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    It would help a lot if you attach a file with some unpersonalised data

    Erwin

  3. #3
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    Hi,
    I'm really stuck, I need the macro to filter to criteria 1 then overwrite the applicable cells with certain text then move onto the next filter criteria and overwrite with a different text, if the filter criteria returns no data I need it to move onto the next filter criteria etc.

    Sub Scheme()

    Sheets("Raw Data").Select
    ActiveSheet.ListObjects("Cost").AutoFilter.ShowAllData

    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=3, Criteria1:= _
    "=BMW", Operator:=xlOr, Criteria2:="=MINI"
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=11, Criteria1:= _
    "=*MW*", Operator:=xlAnd
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=57, Criteria1:= _
    "=BMW", Operator:=xlAnd

    Dim c As Excel.Range

    If Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible) > 0 Then

    For Each c In Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)

    c.Value = "BMW-Scheme"

    Next

    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=57, Criteria1:= _
    "<>BMW-Scheme", Operator:=xlAnd
    ActiveSheet.ListObjects("Cost").Range.AutoFilter Field:=58, Criteria1:= _
    "=B*", Operator:=xlAnd

    If Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible) > 0 Then

    For Each c In Range("be3:be" & Range("be" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)

    c.Value = "BMW-Non-Scheme"

    End With

    End Sub

  4. #4
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    With attachment
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    Hi RobUP,
    first of all, your code was not readable/runnable because it was lacking a with statement and some end-if statements.
    I removed the "End With" statement and added where I thought it makes sense the necessary "End If" statements, as per below:
    Please Login or Register  to view this content.
    Maybe you can update your procedure and see if this is helping
    B/R
    Erwin
    Last edited by Eastw00d; 03-04-2020 at 07:23 AM. Reason: typo

  6. #6
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    Hi Erwin,

    That is perfect !!! Thank you very very much.

    Kind Regards Rob

  7. #7
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    Hi,

    Weird running it this morning it errors as there are no cells visible post the first filter criteria
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    09-25-2014
    Location
    Ips
    MS-Off Ver
    2010
    Posts
    6

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    Any help would be really appreciated as really struggling with this

  9. #9
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Macro to autofilter data then add value to cells within table based on filtered criter

    So it seems that when you meet the first two criteria, field 57 has no "BMW" left to hide, only "BMW-Non-Scheme"

    B/R
    Erwin
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Clear Filtered Data From Autofilter Result on Table
    By Vlad717 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-20-2019, 01:46 PM
  2. [SOLVED] Macro to Autofilter a table and delete filtered rows: Error 1004 cannot alter table
    By brytsyt in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-05-2018, 04:57 AM
  3. Replies: 6
    Last Post: 03-03-2017, 02:45 AM
  4. VBA to autofilter data based on set criteria into a new sheet
    By indik in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2015, 04:54 PM
  5. [SOLVED] Can a Userform Combo Box selection run a macro based on the selections filtered criteria?
    By jmotuk in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-09-2013, 02:23 PM
  6. Hide and Autofilter Macro based on Criteria
    By dawnmau in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-22-2010, 05:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1