VBA to help change the value from 1 to 0 based on a value of another cell

UmairKamal

New Member
Joined
Aug 27, 2021
Messages
17
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Dear Users,

I have the following dataset. If the values in column A are duplicate, and if the value in Column D has the wording "Review" in it, then the number in column E for the first result are converted to zero.

ReferenceDecision DateOutcomeOutcome ReasonNumber
222023566426/04/2022Refused1
222023566426/05/2022AllowedReview1

The idea is that the data will be added on a daily basis and overtime it will be difficult to change the results of the "Number" column based on the duplicates. I was hoping to have a VBA code that I can run on a weekly or monthly basis to update the Number column.

Please assist in this matter.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Can there be more than 2 occurrences of the Reference number in column A? Are the duplicate Reference numbers always grouped together, one underneath the other?
 
Upvote 0
Dear Mumps,

Thank you for your response.

There can be more than one occurrence of the reference number in Column A. so whether it is occurring twice or more than twice, it should always convert all the other 1's to 0s except the latest one (which you can see is based on the second column "Decision Date")

The reference numbers will not always be grouped together, in fact, there is 99.99% chance that they will be randomly placed in the worksheet.
 
Upvote 0
Can you post data that is more representative of your actual data? It would be best if if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
I have used the XL2BB.

As mentioned in my earlier post, I will be pasting the data into the sheet on a daily basis. sometimes, the Ref. No. in column B will have more than one occurrence. let us say, if the Ref No. has two occurrences, one in April-22, second in May-22, and the second occurrence has "Review" in "Outcome Reason" Columns, then when the code is run, it should exclude the previous occurrence(s) by converting its corresponding 1 in the Number Column to 0.

I hope this helps in explaining the requirement.

Dummy Data.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1Notification DateRef No.Appeal referenceHearing TypeHearing date Decision Date Outcome Outcome ReasonAllowed Reason Direction Case Type Static/MovingCodeLocation Pack completed by Date Evidence submittedNor Created ByDate NoR Created Additional information Ref TypeNumberDateMonthYear
204/04/2022UK056896512221127412 Postal 25/03/202202/04/2022Appeal RefusedMoving 33Sachin Tendulkar RoadLW16/03/2022GBU05/01/2022Up102April2022
304/04/2022UK056051962221141515 Postal 02/04/202202/04/2022Appeal RefusedMoving 33Shane Ware RoadTG28/03/2022KMO06/01/2022Down102April2022
404/04/2022UK070110462221171944Postal 02/04/202202/04/2022Appeal RefusedMoving 33Curtly Ambrose RoadAW23/02/2022KMO06/01/2022Left102April2022
504/04/2022UK073032342221153151Postal 02/04/202202/04/2022Appeal RefusedStatic12Mark Waugh RoadTG28/03/2022JBU24/01/2022Left102April2022
607/07/2022UK070865552221181185Postal 03/04/202207/07/2022Appeal AllowedStatic40Steve Waugh RoadMU07/03/2022Up107July2022
707/04/2022UK035241402221115239Postal 06/04/202206/04/2022Appeal AllowedMoving 33Brian Charles Lara RoadDown106April2022
807/04/2022UK06942613 2221241111Postal 06/04/202206/04/2022Appeal AllowedStatic1Wasim Akram RoadVC04/03/2022Down106April2022
907/04/2022UK07295384 2221241937 Postal 06/04/202206/04/2022Appeal AllowedStatic19Imran Khan RoadVC04/03/2022Down106April2022
1008/04/2022 UK054867892221164238personal07/04/202207/04/2022Appeal AllowedMoving 33Waqar Younis RoadTG09/03/2022TH15/02/2022Down107April2022
1108/04/2022UK058440842221252516personal07/04/202207/04/2022Appeal RefusedMoving 33Javed Miandad RoadPH18/03/2022Down107April2022
1208/04/2022UK072131752221241812Postal 07/04/202207/04/2022Appeal RefusedStatic40Glen Mcgrath RoadVC04/03/2022Left107April2022
1308/04/2022UK058915722221214517Postal 07/04/202207/04/2022Appeal RefusedMoving 32dVirat Kohli RoadGBU04/03/2022Right107April2022
1408/04/2022UK058616952221151972Postal 07/04/202207/04/2022Appeal RefusedMoving 52Jaques Kallis RoadTG04/03/2022JBE28/01/2022Up107April2022
1508/04/2022UK058440842221252516Postal 07/04/202207/04/2022Appeal AllowedReviewMoving 33Javed Miandad RoadMU04/03/2022Up107April2022
1608/04/2022UK070110462221171944personal08/04/202207/04/2022Appeal AllowedReviewStatic01Curtly Ambrose RoadMU03/03/2022Up107April2022
1708/04/2022UK072926152221186489personal08/04/202208/04/2022Appeal RefusedStatic12Sir Garfield Sobers RoadAW14/03/2022AJ18/02/2022Up108April2022
1811/04/2022UK05851895222118754APostal 08/04/202209/04/2022Appeal RefusedMoving 33Harsha Bhogle RoadAW11/03/2022KMO10/02/2022Down109April2022
1911/04/2022UK044382042221162935Postal 04/04/202209/04/2022Appeal AllowedMoving 52Richie Benaud RoadTG30/03/2022JN18/02/2022Down109April2022
2011/04/2022UK058725342221182818Postal 09/04/202209/04/2022Appeal AllowedMoving 52Alistair Cook RoadTG01/04/2022KMO15/02/2022Down109April2022
2111/04/2022UK044308032221162979Postal 04/04/202209/04/2022Appeal AllowedMoving 52MGraeme Swann RoadTG30/03/2022JN18/02/2022Down109April2022
2212/04/2022UK071927892221156433Postal 29/03/202211/04/2022Appeal AllowedStatic12James Anderson RoadLW21/03/2022VC25/02/2022Down111April2022
2312/04/2022UK059194232221139717Postal 11/04/202211/04/2022Appeal RefusedMoving 52GStuart Broad RoadAW25/02/2022JBU16/02/2022Down111April2022
2427/04/2022UK060000882221235664Postal 26/04/202226/04/2022Appeal RefusedLSTMoving50Babar Azam RoadLW22/04/2022JBE21/03/2022Left126April2022
2512/05/2022JK06006073222123316APostal 26/04/202211/05/2022Appeal refused Andy Roberts RoadRight111May2022
2612/05/2022JK060275942221251638personal11/05/202211/05/2022Appeal allowed Rahul Dravid RoadRight111May2022
2712/05/2022JK060224112221251513personal11/05/202211/05/2022Appeal allowed Allan Donald RoadDown111May2022
2812/05/2022JK060104232221251615personal11/05/202211/05/2022Appeal allowed Shoaib Akhtar RoadUp111May2022
2912/05/2022JK059460402221214966personal11/05/202211/05/2022Appeal refused Brett Lee RoadUp111May2022
3001/05/2022UK058440842221252516personal11/05/202223/05/2022Appeal AllowedReviewMoving 33Javed Miandad RoadMU04/03/2022Up123May2022
3127/04/2022UK060000882221235664personal26/04/202226/05/2022Appeal AllowedReviewMoving50Babar Azam RoadLW22/04/2022JBE21/03/2022Down126May2022
Data
Cell Formulas
RangeFormula
V2:V31V2=TEXT(F2, "DD")
W2:W31W2=IF(F2="",0,TEXT(F2, "MMMM"))
X2:X31X2=IF(F2="",0,TEXT(F2, "YYYY"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B:BCell ValueduplicatestextNO
 
Upvote 0
Try:
VBA Code:
Sub ChangeValue()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, lRow As Long, fVisRow As Long, lVisRow As Long, rng As Range
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("B2:B" & lRow).Value
    With CreateObject("scripting.dictionary")
        For i = LBound(v) To UBound(v)
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Range("A1").CurrentRegion.AutoFilter 2, v(i, 1)
                fVisRow = Rows("2:" & lRow).SpecialCells(xlCellTypeVisible).Row
                lVisRow = Cells(Rows.Count, "A").End(xlUp).Row
                If fVisRow <> lVisRow Then
                    Set rng = Range("U2:U" & lVisRow - 1).SpecialCells(xlVisible)
                    rng = 0
                End If
            End If
        Next i
    End With
    Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
Dear mumps,

You are a life saver. Initial testing of the data shows that the code works. Thank you so much for your assistance.
 
Upvote 0
You are very welcome. :)
dear mumps,

you were kind enough to help me with this code which was working fine up until today when it has started giving me a runtime error 1004 method range of object _global failed" VBA error. when i press debug, the following line of the code is highlighted.

Set rng = Range("T2:T" & lVisRow - 1).SpecialCells(xlVisible)

can you please advise a solution?
 
Upvote 0
The original code referred to column U instead of column T. Has your file changed? Please use the XL2BB add-in (icon in the menu) to attach an updated screenshot (not a picture) of your sheet.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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