Convert formula into code

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello experts,
This is JohnnyL's code and I need some help to correct some issues and also add one line of code in the end to update the application.
I have tried to address the issue as simple as possible in the conditions sheet.
The formula to color the cells in the combined data sheet is set for 218 sheets in this file. The range of rows will vary each time from 100 to 20,000 rows in different scenarios. So, instead of writing the code for a fixed range, if the code can count the number of rows in the Combined data sheet it will be really cool.
The formula is for ____Under one Gstin number, in the combined data sheet color rows which have the same amounts repeated more than 2 times this formula color the cells (Color Yellow) from A: N if applied in CF. I have colored manually to show the result.
Rich (BB code):
=SUMPRODUCT(($C$2:$C$218=$C2)*($G$2:$G$218>$G2-1)*($G$2:$G$218<$G2+1)*($H$2:$H$218>$H2-1)*($H$2:$H$218<$H2+1)*($I$2:$I$218>$I2-1)*($I$2:$I$218<$I2+1))>2
Match Portal with 2B.xlsm
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I have made the following changes to correct the coloring:

VBA Code:
    With wsMatched
        .Range("A2").Resize(UBound(MatchedArray, 1), UBound(MatchedArray, 2)) = MatchedArray        '   Display results to Matched sheet
'
        For Each Cel In .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)                     '   Loop through all cells in column B on the Matched sheet
            If Cel.Value = "PORTAL" Then                                                            '       If Cell value is 'PORTAL' then ...
''                Cel.EntireRow.Interior.Color = RGB(146, 208, 80)                                    '           Color the row
''                Cel.EntireRow.Font.Bold = True                                                      '           Bold the row
                .Range("A" & Cel.Row & ":N" & Cel.Row).Interior.Color = RGB(146, 208, 80)           '           Color the columns
                .Range("A" & Cel.Row & ":N" & Cel.Row).Font.Bold = True                             '           Bold the columns
            End If
        Next                                                                                        '   Loop back
    End With
'
    With wsMismatches
        .Range("A2").Resize(UBound(MismatchesArray, 1), UBound(MismatchesArray, 2)) = MismatchesArray   '   Display results to Mismatches sheet
'
        For Each Cel In .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)                     '   Loop through all cells in column B on the Mismatches sheet
            If Cel.Value = "PORTAL" Then                                                            '       If Cell value is 'PORTAL' then ...
''                Cel.EntireRow.Interior.Color = RGB(146, 208, 80)                                    '           Color the row
''                Cel.EntireRow.Font.Bold = True                                                      '           Bold the row
                .Range("A" & Cel.Row & ":N" & Cel.Row).Interior.Color = RGB(146, 208, 80)           '           Color the columns
                .Range("A" & Cel.Row & ":N" & Cel.Row).Font.Bold = True                             '           Bold the columns
            End If
        Next                                                                                        '   Loop back
    End With

I also made the following change to correct Column O from displaying 'As per purchases':

VBA Code:
    With wsDestination
        .Range("A" & DestinationStartRow).Resize(UBound(CorrectedDataArray, _
            1), UBound(CorrectedDataArray, 2)) = CorrectedDataArray                                 ' Display Results to destination sheet
'
        .Range("F:F").NumberFormat = "dd-mm-yyyy"                                                   ' Format the date the way we want it to appear
'
        .Columns("M:M").TextToColumns Destination:=.Range("M1"), _
            DataType:=xlDelimited, FieldInfo:=Array(1, 4)                                           ' Convert text to numeric
'
        DestinationLastRow = .Range("B" & .Rows.Count).End(xlUp).Row                                ' Recalculate last row used in column B of the destination sheeet
'
''        .Range("N" & DestinationStartRow & ":O" & DestinationLastRow) = "As per " & DataWorkSheet   ' Copy 'As Per ' & sheet name to Column O
        .Range("N" & DestinationStartRow & ":N" & DestinationLastRow) = "As per " & DataWorkSheet   ' Copy 'As Per ' & sheet name to Column N
'
        .Range("A" & DestinationStartRow & ":A" & DestinationLastRow).Formula = "=Row() - 1"        ' Use formula to set row #s
        .Range("A" & DestinationStartRow & ":A" & DestinationLastRow).Copy                          ' Copy formula range into memory (clipboard)
        .Range("A" & DestinationStartRow & ":A" & DestinationLastRow).PasteSpecial xlPasteValues    ' Paste just the vales back to range
        Application.CutCopyMode = False                                                             ' Clear clipboard & 'marching ants' around copied range
    End With
End Sub

I honestly don't understand what the rest of your requests are. Perhaps you could rephrase the rest of what you want to happen. which sheet, which cells, etc.
 
Upvote 0
Good Morning JohnnyL,
Both the issues are solved successfully. Thanks man.
The last query is connected to the combined data sheet only. If you check the Combined Data sheet there are rows where under one GSTIN number there are amounts in G, H and I columns, you will notice that the amounts are same and appear more than twice like in rows 2,3,4 and 7,8,9 and 137,138,139, etc.,,. In some cases, there may be amounts which appear 4 or 5 times also. I want to color those rows with yellow color which appear more than twice. The formula to enter in the conditional formatting > new rule is
Rich (BB code):
=SUMPRODUCT(($C$2:$C$218=$C2)*($G$2:$G$218>$G2-1)*($G$2:$G$218<$G2+1)*($H$2:$H$218>$H2-1)*($H$2:$H$218<$H2+1)*($I$2:$I$218>$I2-1)*($I$2:$I$218<$I2+1))>2
If I edit the formula to 20,000 rows, then it will color all the rows below the data which are blank till the count of 20,000 rows.
So, If you write the code in such a way that it applies CF to color the cells with data only not the blank cells.
Finally, I want to sort the combined sheet like in the image.
 

Attachments

  • sorted.png
    sorted.png
    12.7 KB · Views: 8
Upvote 0
Finally the combined sheet will look like this...
Query Posted Match Portal with 2B.xlsm
ABCDEFGHIJKLMN
1LineAs PerGSTIN of supplierTrade/Legal name of the SupplierInvoice numberInvoice DateIntegrated TaxCentral TaxState/UTRemarksInvoice ValueTaxable ValueFiling DateData from
2112PORTAL06AFCPG4749P1Z264111/07/2022936.00Matched8736.007800.0008-11-2022As Per Portal
3122TALLY06AFCPG4749P1Z263328-06-2022936.00Matched8736.007800.00As per Purchases
4132TALLY06AFCPG4749P1Z264111-07-2022936.00Not Found8736.007800.00As per Purchases
57PORTAL07AAAPM6196P1Z1RMO/22-23/17011/04/2022993.60Matched6514.005520.0005-10-2022As Per Portal
612PORTAL07AAAPM6196P1Z1RMO/22-23/17111/04/2022993.60Not Found6514.005520.0005-10-2022As Per Portal
7163TALLY07AAAPM6196P1Z1RMO15-04-2022993.60Matched6514.005520.40As per Purchases
810PORTAL29ABWPS3367H1ZRGST-0735/22-2329/04/2022207.36207.36Matched2719.002304.0005-10-2022As Per Portal
911PORTAL29ABWPS3367H1ZRGST-0736/22-2329/04/2022207.36207.36Not Found2719.002304.0005-10-2022As Per Portal
10217TALLY29ABWPS3367H1ZRGST-073529-04-2022207.36207.36Matched2719.002304.28As per Purchases
1113PORTAL29AULPB5486A1ZM22-23/MS-32022/04/202270.2070.20Matched920.00780.0005-11-2022As Per Portal
1220PORTAL29AULPB5486A1ZM22-23/MS-32222/04/202270.2070.20Not Found920.00780.0005-11-2022As Per Portal
13182TALLY29AULPB5486A1ZMMS-322 22-04-202270.2070.20Matched920.00779.60As per Purchases
1440PORTAL07AAAFJ0164C1Z02022-23/JK-01904/04/20222673.00Matched17523.0014850.0005-11-2022As Per Portal
15131TALLY07AAAFJ0164C1Z0JK-019 05-04-20222673.00Matched17523.0014850.00As per Purchases
1654PORTAL07AASFP4051E1Z6202223015311/04/20222700.00Matched17700.0015000.0005-11-2022As Per Portal
17158TALLY07AASFP4051E1Z6202223015312-04-20222700.00Matched17700.0015000.00As per Purchases
1855PORTAL07AASFP4051E1Z6202223023716/04/20224608.00Matched30208.0025600.0005-11-2022As Per Portal
Combined Data
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:N229Expression=SUMPRODUCT(($C$2:$C$218=$C2)*($G$2:$G$218>$G2-1)*($G$2:$G$218<$G2+1)*($H$2:$H$218>$H2-1)*($H$2:$H$218<$H2+1)*($I$2:$I$218>$I2-1)*($I$2:$I$218<$I2+1))>2textNO
 
Upvote 0
I forgot to mention that there may be cases where you may not find same amount rows under one GSTIN number in more than 2 rows. So, add an if condition to the code, if found then color the rows and sort by yellow color else do nothing, just sort the data.
 
Upvote 0
See if the following does what you are looking for:

VBA Code:
Sub Add_CF_FormulaThenSort()
'
    Dim LastRow     As Long
    Dim SortRange   As Range
'
    With Sheets("Combined Data")
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
'
        With .Range("A2:N" & LastRow)
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
                    "=SUMPRODUCT(($C$2:$C$" & LastRow & "=$C2)*($G$2:$G$" & LastRow & ">$G2-1)*($G$2:$G$" & LastRow & "<$G2+1)*($H$2:$H$" & _
                    LastRow & ">$H2-1)*($H$2:$H$" & LastRow & "<$H2+1)*($I$2:$I$" & LastRow & ">$I2-1)*($I$2:$I$" & LastRow & "<$I2+1))>2"
'
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).Interior.Color = 65535
            .FormatConditions(1).StopIfTrue = False
        End With
    End With
'
'----------------------------------------------------------------------------------------------------------------------------------------------
'
    Set SortRange = Sheets("Combined Data").Range("A2:N" & LastRow)
'
    With Sheets("Combined Data").Sort.SortFields
        .Clear
        .Add(key:=Range("C2"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal).SortOnValue.Color = RGB(255, 255, 0)
        .Add key:=Range("C2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add key:=Range("G2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add key:=Range("H2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
'
    With Sheets("Combined Data").Sort
        .SetRange SortRange
        .Apply
    End With
End Sub
 
Upvote 0
In the VBA project I have inserted your module and saved it. In the Match portal where do I insert
call Add_CF_FormulaThenSort..?
 
Upvote 0
For now, just run that module after all of the sheets have been generated.
 
Upvote 0

Forum statistics

Threads
1,215,452
Messages
6,124,916
Members
449,195
Latest member
Stevenciu

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