Results 1 to 4 of 4

Thread: Excel VBA Macro To Conditional Delete Rows Across Multiple Sheets

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0

    Excel VBA Macro To Conditional Delete Rows Across Multiple Sheets

    I have the following macro below

    The macro is working well except for the code to delete rows. Rows 2:8 must only be deleted on those sheets where "Mens Shoe Department" appears in B3 , except sheet "Data"


    Code:
     Sub Cleanup_Data()
        Dim Sh As Worksheet
        Dim LR As Long
        For Each Sh In ActiveWorkbook.Worksheets
          
    
            If Sh.Name <> "Data" Then
                With Sh
               
                    LR = .Cells(.Rows.Count, "B").End(xlUp).Row
                     .Range("A1:A" & LR).Replace What:="|", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="|", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="v", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="p", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="s", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="f", Replacement:="", LookAt:=xlWhole
                     .Range("B1:H" & LR).Replace What:="-", Replacement:="", LookAt:=xlWhole
                     .Range("E1:E" & LR).Replace What:="?", Replacement:="", LookAt:=xlWhole
                     .Range("E1:E" & LR).Replace What:="I", Replacement:="", LookAt:=xlWhole
                     .Range("C1:D" & LR).ClearContents
                      .Range("G1:G" & LR).ClearContents
                       End With
                       If InStr(Range("B10").Value, "MENS SHOES DEPARTMENT") < 0 Then
                      Range("A2:A8").EntireRow.Delete
                     
            End If
             End If
        Next Sh
    End Sub

    Assistance in resolving this is most appreciated

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Code:
    Sub Cleanup_Data()
        
        Dim Sh As Worksheet
        Dim LR As Long
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Name <> "Data" Then
                With Sh
                    LR = .Cells(.Rows.Count, "B").End(xlUp).Row
                    .Range("A1:A" & LR & ",C1:H" & LR).Replace What:="|", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="v", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="p", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="s", Replacement:="", LookAt:=xlWhole
                    .Range("C1:H" & LR).Replace What:="f", Replacement:="", LookAt:=xlWhole
                    .Range("B1:H" & LR).Replace What:="-", Replacement:="", LookAt:=xlWhole
                    .Range("E1:E" & LR).Replace What:="?", Replacement:="", LookAt:=xlWhole
                    .Range("E1:E" & LR).Replace What:="I", Replacement:="", LookAt:=xlWhole
                    .Range("C1:D" & LR & ",G1:G" & LR).ClearContents
                    If InStr(.Range("B10").Value, "MENS SHOES DEPARTMENT") < 0 Then
                        .Range("A2:A8").EntireRow.Delete
                    End If
                End With
             End If
        Next Sh
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Thanks for the help. Where Mens Shoes Department" appears in B10, Rows 2:8 are not being deleted

    See sample per link below


    https://www.dropbox.com/s/fn4fxhytjt...olidation.xlsm

    Kindly test & amend code
    Last edited by Flupsie; 07-27-2014 at 09:56 PM.

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Hi Excelfox

    Thanks for all your help

    I managed to find the error.

    Code:
       If InStr(.Range("B10").Value, "MENS SHOES DEPARTMENT") < 0 Then
    This should be


    Code:
      If InStr(.Range("B10").Value, "MENS SHOES DEPARTMENT") > 0 Then

Similar Threads

  1. Excel Macro to Split Multiple Columns into rows
    By cali-novice in forum Excel Help
    Replies: 2
    Last Post: 05-16-2014, 12:34 PM
  2. Replies: 9
    Last Post: 08-23-2013, 04:25 PM
  3. Replies: 6
    Last Post: 08-14-2013, 04:25 PM
  4. Delete Remove Rows By Criteria VBA Excel
    By marreco in forum Excel Help
    Replies: 5
    Last Post: 12-20-2012, 05:56 PM
  5. Replies: 2
    Last Post: 11-08-2012, 01:15 PM

Posting Permissions

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