VBA choose value from column

I need to choose only the rows where column "P" in sheet Filter_Data is => A3 in Sheet Details.
All others rows need to be deleted.

The code should be in code Sub GroundRating() in Filtered_Data sheet, but anything will help. :)
MWT-TOOL-v10.3.xlsm
Euro5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Sub GroundRating()
    Application.ScreenUpdating = False
    Dim NextCol As Long, LastRow As Long
    

    Dim HRow As Range
    Dim cl As Object
    Dim RWt, Zn As String
    Set HRow = Range("A1", Range("A1").End(xlToRight))
    Dim lngRow As Long
    
    For Each cl In HRow
        Select Case cl.Value
            Case "Rated Weight"
                RWt = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
            Case "Zone"
                Zn = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
        End Select
    Next cl
    
    With Sheets("Filtered_Data")
        For lngRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row To 2 Step -1
            If .Cells(lngRow, "P") < Sheets("Details").Range("A3") Then
                .Cells(lngRow, "P").EntireRow.Delete
            End If
        Next
        
        NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Cells(1, NextCol) = "Ground Rates"
        .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
            "=IF(" & RWt & "2>=150,(VLOOKUP(" & RWt & "2,GR," & Zn & "2,TRUE)/150)*" & RWt & _
            "2,VLOOKUP(" & RWt & "2,GR," & Zn & "2,FALSE))"
    End With
    
 Range("P1") = "Average Group Weight"
    
    With Sheets("Filtered_Data")
    NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
"=roundup(AVERAGEIFS($G:$G,$A:$A,A2,$B:$B,B2,$C:$C,C2,$F:$F,F2,$I:$I,I2,$J:$J,J2,$K:$K,K2,$L:$L,L2),0)"
End With



End Sub

Open in new window

0
Rgonzo1971Commented:
Hi,

pls try

Sub GroundRating()
    Application.ScreenUpdating = False
    Dim NextCol As Long, LastRow As Long
    

    Dim HRow As Range
    Dim cl As Object
    Dim RWt, Zn As String
    Set HRow = Range("A1", Range("A1").End(xlToRight))
    For Each cl In HRow
        Select Case cl.Value
            Case "Rated Weight"
                RWt = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
            Case "Zone"
                Zn = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
        End Select
    Next cl
    
    With Sheets("Filtered_Data")
        NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Cells(1, NextCol) = "Ground Rates"
        .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
            "=IF(" & RWt & "2>=150,(VLOOKUP(" & RWt & "2,GR," & Zn & "2,TRUE)/150)*" & RWt & _
            "2,VLOOKUP(" & RWt & "2,GR," & Zn & "2,FALSE))"
    End With
    
 Range("P1") = "Average Group Weight"
    
    With Sheets("Filtered_Data")
    NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
"=roundup(AVERAGEIFS($G:$G,$A:$A,A2,$B:$B,B2,$C:$C,C2,$F:$F,F2,$I:$I,I2,$J:$J,J2,$K:$K,K2,$L:$L,L2),0)"

    .Range(Range("A1"), Range("P" & Cells.Rows.Count)).End(xlUp).AutoFilter Field:=16, _
        Criteria1:="<" & Sheets("Details").Range("A3")
    Set rng = .UsedRange.SpecialCells(xlCellTypeVisible)

    If (rng.Rows.Count > 1) Then _
        .Range(Range("A1"), Range("A" & Cells.Rows.Count).End(xlUp)).EntireRow.Delete
    .Range("P1").AutoFilter

End With
End Sub

Open in new window

Regards
0
Euro5Author Commented:
@martin - my error.
I do not know that the weight will be in column P.
I will only know that it is in last COLUMN.

Can you help?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Euro5Author Commented:
I get an error here
        NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1

Sub GroundRating()
    Application.ScreenUpdating = False
    Dim NextCol As Long, LastRow As Long
    

    Dim HRow As Range
    Dim cl As Object
    Dim RWt, Zn As String
    Set HRow = Range("A1", Range("A1").End(xlToRight))
    Dim lngRow As Long
    
    For Each cl In HRow
        Select Case cl.Value
            Case "Rated Weight"
                RWt = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
            Case "Zone"
                Zn = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
        End Select
    Next cl
        
        NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Cells(1, NextCol) = "Ground Rates"
        .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
            "=IF(" & RWt & "2>=150,(VLOOKUP(" & RWt & "2,GR," & Zn & "2,TRUE)/150)*" & RWt & _
            "2,VLOOKUP(" & RWt & "2,GR," & Zn & "2,FALSE))"
    End With
    
    With Sheets("Filtered_Data")
    NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
     .Cells(1, NextCol) = "Ground Rates"
    .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
"=roundup(AVERAGEIFS($G:$G,$A:$A,A2,$B:$B,B2,$C:$C,C2,$F:$F,F2,$I:$I,I2,$J:$J,J2,$K:$K,K2,$L:$L,L2),0)"
End With


    With Sheets("Filtered_Data")
        For lngRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row To 2 Step -1
            If .Cells(lngRow, NextCol) < Sheets("Details").Range("A3") Then
                .Cells(lngRow, NextCol).EntireRow.Delete
            End If
        Next


End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
This always uses the last column.
Sub GroundRating()
    Application.ScreenUpdating = False
    Dim NextCol As Long, LastRow As Long
    Dim lngLastCol As Long

    Dim HRow As Range
    Dim cl As Object
    Dim RWt, Zn As String
    Set HRow = Range("A1", Range("A1").End(xlToRight))
    Dim lngRow As Long
    
    For Each cl In HRow
        Select Case cl.Value
            Case "Rated Weight"
                RWt = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
            Case "Zone"
                Zn = Left(cl.EntireColumn.Address, InStr(1, cl.EntireColumn.Address, ":") - 1)
        End Select
    Next cl
    
    With Sheets("Filtered_Data")
        lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column

        For lngRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row To 2 Step -1
            If .Cells(lngRow, lngLastCol) < Sheets("Details").Range("A3") Then
                .Cells(lngRow, lngLastCol).EntireRow.Delete
            End If
        Next
        
        NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Cells(1, NextCol) = "Ground Rates"
        .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
            "=IF(" & RWt & "2>=150,(VLOOKUP(" & RWt & "2,GR," & Zn & "2,TRUE)/150)*" & RWt & _
            "2,VLOOKUP(" & RWt & "2,GR," & Zn & "2,FALSE))"
    End With
    
 Range("P1") = "Average Group Weight"
    
    With Sheets("Filtered_Data")
    NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
"=roundup(AVERAGEIFS($G:$G,$A:$A,A2,$B:$B,B2,$C:$C,C2,$F:$F,F2,$I:$I,I2,$J:$J,J2,$K:$K,K2,$L:$L,L2),0)"
End With



End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Please consider my code above, but are you aware that the GroundRating sub duplicates the "Ground Rates" column?
0
Euro5Author Commented:
Hmmm...no I wasn't aware. This should bring in the Ground and

       NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Cells(1, NextCol) = "Ground Rates"
        .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
            "=IF(" & RWt & "2>=150,(VLOOKUP(" & RWt & "2,GR," & Zn & "2,TRUE)/150)*" & RWt & _
            "2,VLOOKUP(" & RWt & "2,GR," & Zn & "2,FALSE))"
    End With

Open in new window


This should bring in Average weight
    With Sheets("Filtered_Data")
    NextCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range(.Cells(2, NextCol), .Cells(LastRow, NextCol)).Formula = _
"=roundup(AVERAGEIFS($G:$G,$A:$A,A2,$B:$B,B2,$C:$C,C2,$F:$F,F2,$I:$I,I2,$J:$J,J2,$K:$K,K2,$L:$L,L2),0)"
End With

Open in new window

0
Martin LissOlder than dirtCommented:
Hmmm...no I wasn't aware.
Run the sub a couple of times and you'll see what I mean.
0
Euro5Author Commented:
Ok....but I notice something else now...

The steps are out of order.
Meaning, I need to get the Ground Rates & Average Weight - THEN compare the average weight to the Detail A3. Is it working that way??
0
Euro5Author Commented:
Yes, I see what you mean about the Ground rates. That's because it always just uses the next empty cell, isn't it.
0
Euro5Author Commented:
Got it!!! Thanks!
0
Martin LissOlder than dirtCommented:
You're welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.