KjBox gijsmo - All requirements were fulfilled. Thanks a ton. Wish I could do something back to you. Please let me know if you are tutoring online... @[email protected].. Thanks again!!
Autofilter and return cell values using VBA
- Hanes109
- Thread is marked as Resolved.
-
-
-
There are values in Column KP which has "service_type" as a header, those values look as if they should be the OP values and should be in Column KO! Is that correct?- Yes
As for the NUMBERS, PEG_3 all start with a "3". PEG_5 with a "4" and PEG_7 with a "2", is that correct?- Yes
-
-
Some of the text has obviously been transposed, this is what that section should look like:
Code
Display MoreWith wTarg 'assume the header is row 1 Set rRng = .Rows(1) 'try finding the matching number first If Not FindInRange(rRng, sNum, rFind) Then GoTo QuickExit lNum = rFind.Column If Not FindInRange(rRng, OP, rFind) Then GoTo QuickExit lOP = rFind.Column If Not FindInRange(rRng, DP, rFind) Then GoTo QuickExit lDP = rFind.Column If Not FindInRange(rRng, DType, rFind) Then GoTo QuickExit lDType = rFind.Column If Not FindInRange(rRng, Exp, rFind) Then GoTo QuickExit lExp = rFind.Column If Not FindInRange(rRng, EDate, rFind) Then GoTo QuickExit lEDate = rFind.Column 'transit time may or may not exist If Not FindInRange(rRng, TTime, rFind) Then lTTime = 0 End With
-
OK, it looks like you changed a number of constants and other bits and pieces of code.
The attached should hopefully fix all that.
-
Based on a quick look at those tabs, it will be failing because there is no column called cargo_type on LEG4 or LEG2
-
How dumb I'm , Thank you so much...!! It works perfect..
-
Here is your file with new code and a couple of other changes.
I made Dashboard C4 to C13 named ranges (simplifies the code a bit) and you will note values in K2 to K4, these are the integers that NUMBER must start with for each given PEG (PEGs are in J2 to J4). You can change the font colour of K2 to K4 to hide the text. This is to check that a valid NUMBER has been entered for the entered PEG.
Further checks for other validities can be incorporated into the main code.
The NUMBER check code is a Worksheet_Change Event Code in the Dasboard Sheet Module, it is fired when a new NUMBER is entered. The code is:
Code
Display MoreOption Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim sPEG As String sPEG = [PEG] If Not Intersect(Target, [NUM]) Is Nothing Then If Target <> vbNullString Then If CInt(Left(Target, 1)) <> Application.VLookup(sPEG, Range("j2:k20"), 2, 0) Then MsgBox "Number invalid for entered PEG", 16 [NUM].ClearContents Application.Goto [NUM] Exit Sub End If End If End If End Sub
The code assigned to the button is still in Module 1, and is
Code
Display MoreOption Explicit Sub Add() Dim sOP As String, sDP As String, sDType As String Dim lNum As Long, dPrice As Double, sCurr As String, lTrans As Long, dtExp As Date, dtEff As Date Dim x, y, i As Long, ii As Long, iii As Long, iv As Long, v As Long, vi As Long, vii As Long, viii As Long sPEG = [PEG]: lNum = [NUM] With Sheets(sPEG).Cells(1).CurrentRegion ' Load all sheet data into array x and headers into array y x = .Value: y = .Rows(1) ' Determine the correct columns that require data ii = Application.Match("OP", y, 0): iii = Application.Match("DP", y, 0) iv = Application.Match("d_type", y, 0): v = Application.Match(lNum, y, 0) vi = Application.Match("expiry", y, 0): vii = Application.Match("effective_date", y, 0) ' Loop though array x checking for matching "OP", "DP" & "d_type" For i = 2 To UBound(x, 1) If x(i, ii) = [OP] And x(i, iii) = [DP] And x(i, iv) = [DType] Then 'Full Match found ' Load data into the array x(i, v) = [Price]: x(i, v + 1) = [Currency]: x(i, vi) = [ExpDate]: x(i, vii) = [EffDate] ' Check if there is a "transit time" column, if there is then add time to array If Not IsError(Application.Match("transit_time", y, 0)) Then ix = Application.Match("transit_time", y, 0) x(i, ix) = [TransTime] End If End If Next ' Write the new values in array x back to the worksheet (updating it) .Value = x End With MsgBox " Price has been Updated... !!! " End Sub
Let me know if you have any issues.
-
-
You're welcome, hope you can transfer everything to you actual workbook ok
-
Haha I guess so..
-
I've just noticed that if you are using the code in my examples, you need to change this part in the Add sub from:
To:
Otherwise Transit Time will never get updated even when it exists in the LEG sheet
-
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!