Filter and Sorting

Adrian Low

New Member
Joined
Apr 30, 2019
Messages
23
[FONT=&quot]Hi I have two questions.[/FONT]

  • Create filter to find any blanks cells and delete the entire row with it after which show all data
  • Sort in one of the input on Range("E3:E") from A to Z
[FONT=&quot]I tried this but keeps getting error "Method of object worksheet error"[/FONT]
<code style="box-sizing: inherit; font-family: Consolas, "Courier New", Courier, monospace; font-size: 1em; border: 0px; margin: 0px; padding: 0px; vertical-align: top; -webkit-font-smoothing: antialiased; text-size-adjust: none;"> Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")
Set wsOutput = Workbooks("Output.xls").Worksheets("Sheet1")
Set Ws2 = Workbooks("InputA.xls").Worksheets("Sheet0")


With wsInput

'Error starts from here
wsInput
.Range("F3:F").AutoFilter Criteria1:="="
wsInput
.Range("F3:F").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
wsInput
.ShowAllData

LastRow
= wsInput.Cells(wsInput.Rows.Count, "E").End(xlUp).Row
Rows
("3:LastRow").Select
.Sort.SortFields.Clear
.Sort.SortFields.Add Key _
:=Range("E3:LastRow"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal

With wsInput.Sort
.SetRange Range("A2:LastRow")
.header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply

End With

</code>[FONT=&quot]I believe my sorting have some issues too please let me know if any of you see any mistakes.

[/FONT]

[FONT=&quot]Many thanks,[/FONT]
[FONT=&quot]Adrian[/FONT]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Adrian,

A few things that you can check:
  • Your first "With" does not close with "End With" statement
  • Your second "With" closes with "EndWith" statement, but you need to add a space in order to correct it (i.e. "End With")
  • wsInput.Range("F3:F") won't work as Excel does not know what is the end of this range (is it F3:F10? F3:F50?). As you already have "LastRow" parameter, you can move it up and then use it in your code like:
    LastRow = Sheet1.Cells(.Rows.Count, "E").End(xlUp).Row
    wsInput.Range("F3:F" & LastRow).AutoFilter Field:=1, Criteria1:=""
  • Another issue is with Rows("3:LastRow").Select - you need to move LastRow outside of double-quotes (i.e. Rows("3:" & LastRow).Select )
  • Both Range("E3:LastRow") and Range("A2:LastRow") are also incorrect (assuming LastRow is 50, what does E3:50 mean?). Maybe you want to apply the following:
    Range("E3:E" & LastRow)

I hope it's a good starting point.
 
Upvote 0
Hi Adrian,

A few things that you can check:
  • Your first "With" does not close with "End With" statement
  • Your second "With" closes with "EndWith" statement, but you need to add a space in order to correct it (i.e. "End With")
  • wsInput.Range("F3:F") won't work as Excel does not know what is the end of this range (is it F3:F10? F3:F50?). As you already have "LastRow" parameter, you can move it up and then use it in your code like:
    LastRow = Sheet1.Cells(.Rows.Count, "E").End(xlUp).Row
    wsInput.Range("F3:F" & LastRow).AutoFilter Field:=1, Criteria1:=""
  • Another issue is with Rows("3:LastRow").Select - you need to move LastRow outside of double-quotes (i.e. Rows("3:" & LastRow).Select )
  • Both Range("E3:LastRow") and Range("A2:LastRow") are also incorrect (assuming LastRow is 50, what does E3:50 mean?). Maybe you want to apply the following:
    Range("E3:E" & LastRow)

I hope it's a good starting point.

Hi, I tried your suggestion but I am getting an error of AutoFilter method of Range class failed. Here's my code:

Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")
Set wsOutput = Workbooks("Output.xls").Worksheets("Sheet1")
Set Ws2 = Workbooks("InputA.xls").Worksheets("Sheet0")


With wsInput




LastRow = wsInput.Cells(wsInput.Rows.Count, "E").End(xlUp).Row


'Error starts from here
wsInput.Range("F3:F" & LastRow).AutoFilter Criteria1:="="
wsInput.Range("F3:F" & LastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
wsInput.ShowAllData




Rows("3:" & LastRow).Select
.Sort.SortFields.Clear
.Sort.SortFields.Add Key _
:=Range("E3:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal


End With
With wsInput.Sort
.SetRange Range("A2:A" & LastRow)
.header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply


End With


Thank you,
Adrian
 
Upvote 0
Hi Adrian,

Try changing it to:
Code:
wsInput.Range("F3:F" & LastRow).AutoFilter Field:=1, Criteria1:="="

Adding "Field" parameter should help.
 
Last edited:
Upvote 0
Hi Adrian,

Try changing it to:
Code:
wsInput.Range("F3:F" & LastRow).AutoFilter Field:=1, Criteria1:="="

Adding "Field" parameter should help.

wsInput.Range("G3:G" & LastRow).SpecialCells(xlCellTypeVisible).Delete - No cells were found on the specialCells
 
Upvote 0
Do you have any blank cells in column F? Try stopping your macro before jumping to this step and see if the previous line filters blank cells properly. Then select xlCellTypeVisible cells before deleting them, just to check if code behaves properly:

Code:
wsInput.Range("G3:G" & LastRow).SpecialCells(xlCellTypeVisible).Select

Alternatively, you can share your file on the forum and I'll try to replicate the issue on my side.
 
Upvote 0
Yes but I am doing in on column G instead. I can share my entire code with you here:

Code:
Sub Button3_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = True


Dim filespec As Variant
Dim Input1 As String
Dim FirstFile As Workbook


Dim filespec1 As Variant
Dim Input2 As String
Dim SecondFile As Workbook


Dim Result As Workbook
Dim strChar As String


'Prompt user to select fileA
filespec = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xls), *.xls", _
Title:="Please Open Summary file", _
MultiSelect:=False)
 
If filespec = False Then
GoTo Finish1
Else
End If


'Open selected workbookA
Workbooks.Open (filespec)
Input1 = ActiveWorkbook.Name
Set FirstFile = ActiveWorkbook
With FirstFile
.SaveAs Filename:="InputA.xls"
End With


'Prompt user to select fileB
filespec1 = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xls), *.xls", _
Title:="Please Open Summary file", _
MultiSelect:=False)


If filespec1 = False Then
GoTo Finish1
Else
End If


'Open selected workbookB
Workbooks.Open (filespec1)
Input2 = ActiveWorkbook.Name
Set SecondFile = ActiveWorkbook
With SecondFile
.SaveAs Filename:="InputB.xls"
End With


Set Result = Workbooks.Add
With Result
.SaveAs Filename:="Output.xls"
End With


'Setting of first row
Range("A1").Select
ActiveCell.FormulaR1C1 = "Material"


Range("B1").Select
ActiveCell.FormulaR1C1 = "Level-1"


Range("C1").Select
ActiveCell.FormulaR1C1 = "Material Description"


Range("D1").Select
ActiveCell.FormulaR1C1 = "Item"


Range("E1").Select
ActiveCell.FormulaR1C1 = "Qty per"


Range("F1").Select
ActiveCell.FormulaR1C1 = "Rev.Lvl"


Range("G1").Select
ActiveCell.FormulaR1C1 = "ROHS"


Range("H1").Select
ActiveCell.FormulaR1C1 = "Reference"


Range("I1").Select
ActiveCell.FormulaR1C1 = "Qual.Mafr."


Workbooks("InputA.xls").Worksheets("Sheet0").Range("B2").Copy _
        Workbooks("Output.xls").Worksheets("Sheet1").Range("A2")
        
Workbooks("InputA.xls").Worksheets("Sheet0").Range("D2").Copy _
        Workbooks("Output.xls").Worksheets("Sheet1").Range("C2")
        
Workbooks("Output.xls").Worksheets("Sheet1").Range("F2").Value = _
        Mid(Workbooks("InputA.xls").Worksheets("Sheet0").[G2], 2, 1)
        


Dim wsInput As Worksheet, wsOutput As Worksheet, LastRow As Long, C As Range, D As Range, A As Range, B As Range, V As Range, Z As Range, i As Long, Where As Range


    Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")
    Set wsOutput = Workbooks("Output.xls").Worksheets("Sheet1")
    Set Ws2 = Workbooks("InputA.xls").Worksheets("Sheet0")
    


LastRow = wsInput.Cells(wsInput.Rows.Count, "E").End(xlUp).Row




'Error starts from here


With wsInput
wsInput.Range("G3:G" & LastRow).AutoFilter Field:=1, Criteria1:=""
wsInput.Range("G3:G" & LastRow).SpecialCells(xlCellTypeVisible).Delete
wsInput.ShowAllData


Rows("3:" & LastRow).Select
wsInput.Sort.SortFields.Clear
wsInput.Sort.SortFields.Add Key _
:=Range("E3:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal


End With


With wsInput.Sort
.SetRange Range("E3:E" & LastRow)
.header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply


End With


    Dim SearchValue As String, AddValue As String


    With wsInput ' Always select your worksheet name


        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        Counter = 0
        AddValue = ""
        SearchValue = ""


        For p = LastRow To 3 Step -1


            SearchValue = .Range("C" & p).Value
            
            'Check for duplicates
            If SearchValue <> "" Then


            If Application.WorksheetFunction.CountIf(.Range("C3:C" & LastRow), SearchValue) > 1 Then


            For n = p To 3 Step -1


            If .Range("C" & n).Value = SearchValue Then


            If AddValue = "" Then
                 AddValue = .Range("E" & n).Value
                 
            Else
                 'Concatenate ColumnE and above value
                AddValue = .Range("E" & n).Value & "," & AddValue
                 
                 'Delete row
                 .Rows(n).EntireRow.Delete
                  Counter = Counter + 1
                  
            End If


            End If


            Next n


            .Range("E" & p - Counter).Value = AddValue
             AddValue = ""
             SearchValue = ""
             Counter = 0


            End If


            End If


   Next p


    'delete blank cells from inputB
    LastRow = wsInput.Cells(wsInput.Rows.Count, "i").End(xlUp).Row
    For i = LastRow To 2 Step -1
        If wsInput.Cells(i, "F").Value = "" Then
            wsInput.Rows(i).Delete
        End If
    Next
    
    'copy columns F and G to I from inputB to output
        
       'Copying data from InputB
       
       LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
       For Each C In .Range("F3:F" & LastRow)
          wsOutput.Cells(C.Row, "I").Value = C & "" & "   " & C.Offset(0, 1)
       Next C
    
        LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
        For Each D In .Range("E3:E" & LastRow)
            wsOutput.Cells(D.Row, "H").Value = D & ""
         
        Next D


        LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
         For Each A In .Range("G3:E" & LastRow)
            wsOutput.Cells(A.Row, "G").Value = "01"
        Next A
        
        LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
           For Each V In .Range("D3:D" & LastRow)
            wsOutput.Cells(V.Row, "E").Value = V & ""
        Next V
        
         LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
           For Each Z In .Range("A3:A" & LastRow)
            wsOutput.Cells(Z.Row, "D").Value = Z & ""
            
        Next Z
        
    End With


Dim y As Range, m As Range


    With Ws2
    
    'Copying Data from InputA
    LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
          For Each y In .Range("D3:D" & LastRow)
          wsOutput.Cells(y.Row, "C").Value = y & Null
        Next y
    End With
    


     With Ws2
     LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
           For Each m In .Range("B3:B" & LastRow)
            wsOutput.Cells(m.Row, "B").Value = m & Null
        Next m
        
    End With
    
    With Ws2
    LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
          For Each B In .Range("F3:F" & LastRow)
           wsOutput.Cells(B.Row, "F").Value = Left(Workbooks("InputA.xls").Worksheets("Sheet0").Cells(B.Row, "G").Value, 1)
           
        Next B
        
     
    
    End With
    
wsOutput.Cells.HorizontalAlignment = xlLeft


Dim ws As Worksheet
Set ws = Worksheets("Sheet1")


'Create header
Application.PrintCommunication = False
With ws.PageSetup
  ' .LeftHeaderPicture.Filename = "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
   .CenterHeader = wsOutput.Cells(2, 1).Value & wsOutput.Cells(2, 3).Value & "  " & "REV_" & wsOutput.Cells(2, 6).Value
    .FitToPagesWide = 1
    .FitToPagesTall = False
    
End With
Application.PrintCommunication = True
ws.PageSetup.LeftHeader = "&G"










Finish1:


Application.ScreenUpdating = True
Application.DisplayAlerts = True




End Sub
 
Last edited by a moderator:
Upvote 0
I need to check for blank cells on column G and delete its entire row with it. Then next is to do a sorting at column E from A to Z cause it then can check for duplicate which I already finish the duplicate part.
 
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,177
Members
452,446
Latest member
walkman99

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