VBA code / Loop function to match amounts

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello experts,
The amounts in column B and C are entered using formula '=RANDBETWEEN(10,90*10)*10. I have to match actual amount with the total as per calculation. The difference must show zero(0). I select the amounts from cell B50 upwards till I get the nearest value of the difference and delete it. Then I enter the difference amount in the last empty column in column B to get zero. I do this for every column in the sheet. There are around 30 columns in each sheet and has 12 months sheet data to be matched. It takes a lot of time. I was hoping whether, if this would be possible with the help of a code, it will save a lot of time.

Code to match amounts.xlsx
ABCDEFG
1Apr-2001-04-202002-04-2020Apr-2001-04-202002-04-2020
27770454077704540
3Original Data82705590Matched Data82705590
46400511064005110
558057405805740
64570321045703210
743605004360500
843208504320850
93670545036705450
104280500042805000
113180243031802430
1278303207830320
132770248027702480
146230830062308300
1528046902804690
166760361067603610
1789005008900500
1899043409904340
1973104107310410
204060646040606460
2111017601101760
221570231015702310
233080460030804600
2435070403507040
2579501207950120
26113056803105680
27677031203120
28873017901790
29159084508450
30352032901500
3114607650
3222501910
3321304570
3468804330
3514804530
3661903760
373605610
3887601530
3911804520
4085308740
4177606000
4280407350
4363302210
4442301990
4570508180
4615707350
4771001570
4818907200
4943302950
504103940
51
52Total As per Calculation2,15,260.002,03,580.00Total As per Calculation1,05,900.001,05,900.00
53Actual Amount1,05,900.001,05,900.00Actual Amount1,05,900.001,05,900.00
54Difference1,09,360.0097,680.00Difference--
55207040.000.00
56
April workings
Cell Formulas
RangeFormula
B1,F1B1=A1
C1,G1C1=B1+1
F52:G52,B52:C52B52=SUM(B2:B50)
F54:G54,B54:C54B54=B52-B53
A55,E55A55=SUM(B54:C54)
 

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".
Select any cell in the first of two columns (ie. column B in your example, or column F if that was another data set.)

Adjust the value of sheet as needed.
The 50 To 2 assumes the same cells used in your example. If your data range is different, change 50 to your last row and 2 to your first row.
The Cells(54, again, assumes the cells used in the example. If your Diffence section is on a different row, change this number to that row number.

This also assumes you has two columns next to each other that need to be calculated.

Please test this on a sample data set first as it removes data.
Let me know if there are any changes that are needed.

VBA Code:
Sub Test()
    Dim sheet As Worksheet
    Dim cell As Range
    
    Set sheet = ThisWorkbook.Sheets("Sheet1")
    Set cell = Application.ActiveCell
    
    For i = 50 To 2 Step -1
        If sheet.Cells(54, cell.Column).Value = 0 Then
            Exit For
        ElseIf sheet.Cells(54, cell.Column).Value - sheet.Cells(i, cell.Column).Value > 0 Then
            sheet.Cells(i, cell.Column).Value = ""
        Else
            sheet.Cells(i, cell.Column).Value = sheet.Cells(i, cell.Column).Value - sheet.Cells(54, cell.Column).Value
        End If
    Next i
    
    For i = 50 To 2 Step -1
        If sheet.Cells(54, cell.Column + 1).Value = 0 Then
            Exit For
        ElseIf sheet.Cells(54, cell.Column + 1).Value - sheet.Cells(i, cell.Column + 1).Value > 0 Then
            sheet.Cells(i, cell.Column + 1).Value = ""
        Else
            sheet.Cells(i, cell.Column + 1).Value = sheet.Cells(i, cell.Column + 1).Value - sheet.Cells(54, cell.Column + 1).Value
        End If
    Next i
    
End Sub
 
Upvote 0
First of all I never expected that it is possible. Thanks Mackc557.
Except the first column which represents the date, columns B to AD (depending on the number of days in that month) contains the data to be matched in each sheet. In the above image I have just posted the required answer in columns E F and G. I understand that, I will have to change the number of rows as per my original data base. Will Check and reply back.
 
Upvote 0
First of all I never expected that it is possible. Thanks Mackc557.
Except the first column which represents the date, columns B to AD (depending on the number of days in that month) contains the data to be matched in each sheet. In the above image I have just posted the required answer in columns E F and G. I understand that, I will have to change the number of rows as per my original data base. Will Check and reply back.
Cash sales workings 2020-21.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
101-04-202002-04-202003-04-202004-04-202005-04-202006-04-202007-04-202008-04-202009-04-202010-04-202011-04-202012-04-202013-04-202014-04-202015-04-202016-04-202017-04-202018-04-202019-04-202020-04-202021-04-202022-04-202023-04-202024-04-202025-04-202026-04-202027-04-202028-04-202029-04-202030-04-2020
2777045402950759052601670209017808860795075905260777045402950167020907590526016702090454077704540759052607770454016702090
3827055904860502057308570892062206530452050205730827055904860857089205020573085708920559082705590502057308270559085708920
April


The original data is like this and it has more than 600 rows.
 
Upvote 0
Select any cell in the first of two columns (ie. column B in your example, or column F if that was another data set.)

Adjust the value of sheet as needed.
The 50 To 2 assumes the same cells used in your example. If your data range is different, change 50 to your last row and 2 to your first row.
The Cells(54, again, assumes the cells used in the example. If your Diffence section is on a different row, change this number to that row number.

This also assumes you has two columns next to each other that need to be calculated.

Please test this on a sample data set first as it removes data.
Let me know if there are any changes that are needed.

VBA Code:
Sub Test()
    Dim sheet As Worksheet
    Dim cell As Range
   
    Set sheet = ThisWorkbook.Sheets("Sheet1")
    Set cell = Application.ActiveCell
   
    For i = 50 To 2 Step -1
        If sheet.Cells(54, cell.Column).Value = 0 Then
            Exit For
        ElseIf sheet.Cells(54, cell.Column).Value - sheet.Cells(i, cell.Column).Value > 0 Then
            sheet.Cells(i, cell.Column).Value = ""
        Else
            sheet.Cells(i, cell.Column).Value = sheet.Cells(i, cell.Column).Value - sheet.Cells(54, cell.Column).Value
        End If
    Next i
   
    For i = 50 To 2 Step -1
        If sheet.Cells(54, cell.Column + 1).Value = 0 Then
            Exit For
        ElseIf sheet.Cells(54, cell.Column + 1).Value - sheet.Cells(i, cell.Column + 1).Value > 0 Then
            sheet.Cells(i, cell.Column + 1).Value = ""
        Else
            sheet.Cells(i, cell.Column + 1).Value = sheet.Cells(i, cell.Column + 1).Value - sheet.Cells(54, cell.Column + 1).Value
        End If
    Next i
   
End Sub
I am getting an error - compile error variable not defined. I think i needs to be defined
 
Upvote 0
I am getting an error - compile error variable not defined. I think i needs to be defined
Seriously man. You are really great. I entered Dim i as integar and changed "Sheets1" to "April Workings" and it worked perfectly. Next I will check in the 30 columns.
 
Upvote 0
Mackc577. The code is running correctly in the first 2 columns only. I added and selected 30 columns, but it is working in the first 2 columns only. How and where do I edit the code for the number of columns. I tried copying and pasting the code from "Next" and pasted it 28 more times. But still it works in the first 2 columns only.
 
Upvote 0
And one more thing in the column where the amount is matched one cell shows zero(0). Can you replace it with blank? When I append all the rows there shouldn't be any 0's in the figures.
Mackc577. The code is running correctly in the first 2 columns only. I added and selected 30 columns, but it is working in the first 2 columns only. How and where do I edit the code for the number of columns. I tried copying and pasting the code from "Next" and pasted it 28 more times. But still it works in the first 2 columns only.
 
Upvote 0
Again, test this on sample data, and I'm not sure why it is requiring you to declare temporary counter variables.

Select a cell within your first column with data. The code will run through every column from the selected column onward.
Again, let me know if you need modifications on this.

VBA Code:
Sub Test()
    Dim sheet As Worksheet
    Dim cell As Range
    Dim CellOffset As Integer
    
    Set sheet = ThisWorkbook.Sheets("Sheet1")
    Set cell = Application.ActiveCell
    
    CellOffset = cell.Column
    
    For x = 0 To (sheet.Columns.Count - CellOffset)
        For i = 50 To 2 Step -1
            If sheet.Cells(54, cell.Column + x).Value = 0 Then
                Exit For
            ElseIf sheet.Cells(54, cell.Column + x).Value - sheet.Cells(i, cell.Column + x).Value > 0 Then
                sheet.Cells(i, cell.Column + x).Value = ""
            Else
                sheet.Cells(i, cell.Column + x).Value = sheet.Cells(i, cell.Column + x).Value - sheet.Cells(54, cell.Column + x).Value
            End If
        Next i
    Next x

    
End Sub
 
Upvote 0
Solution
Again, test this on sample data, and I'm not sure why it is requiring you to declare temporary counter variables.

Select a cell within your first column with data. The code will run through every column from the selected column onward.
Again, let me know if you need modifications on this.

VBA Code:
Sub Test()
    Dim sheet As Worksheet
    Dim cell As Range
    Dim CellOffset As Integer
  
    Set sheet = ThisWorkbook.Sheets("Sheet1")
    Set cell = Application.ActiveCell
  
    CellOffset = cell.Column
  
    For x = 0 To (sheet.Columns.Count - CellOffset)
        For i = 50 To 2 Step -1
            If sheet.Cells(54, cell.Column + x).Value = 0 Then
                Exit For
            ElseIf sheet.Cells(54, cell.Column + x).Value - sheet.Cells(i, cell.Column + x).Value > 0 Then
                sheet.Cells(i, cell.Column + x).Value = ""
            Else
                sheet.Cells(i, cell.Column + x).Value = sheet.Cells(i, cell.Column + x).Value - sheet.Cells(54, cell.Column + x).Value
            End If
        Next i
    Next x

  
End Sub
I am really astonished what all a code can do. I knew that, the work we do for hours, it can do the same work in seconds. But this, it is just awesome. It also does the calculation for us. Thanks Mackc577 for helping to solve this. And more thankful to increase my curiosity to accept more difficult tasks. Your code is perfect Mr.Genius. It doesn't only run in 30 rows but any number of rows. Thanks a ton once again.
To remove the 0's I added this code to your code
Range("B2:B619").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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