Make a difference Amount from 2 sheet using Macro

abadilala

New Member
Joined
Sep 29, 2017
Messages
27
Hi, I have a problem to be solved. I have data from sheet 2016 and 2017. What I want to do is to make another sheet to calculate the difference money within 2016 and 2017. Please see the image link attached:

https://ibb.co/hniZo6 --- case 1
https://ibb.co/jWcuo6 ---- case 2

What I want to do is simply create the new sheet to compare each column except column A (column name).
and another problem is the number of columns is not fixed. some case may have 1 column to be compared, another might have more. So I guess I need the number of columns to become a variable?

How is the idea to make the total (comparison) sheet using macro that can solve case 1 and case 2? please share your thought!


hniZo6
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Here's my stab:


Book1
ABC
1NameMoneyAsset
2Rei100130
3Komar120150
4Sento300160
5Rashid150180
2016



Book1
ABC
1NameMoneyAsset
2Rei100180
3Komar130160
4Sento290170
5Rashid300160
2017


Code:
Public Sub MakeDifferenceSheet()

Dim sheet2016 As Worksheet
Dim sheet2017 As Worksheet
Dim sheetDiff As Worksheet
Dim lastCol As Long
Dim lastRow As Long
Dim thisRow As Long
Dim thisCol As Long
Dim foundCol As Variant
Dim foundRow As Variant
Dim nextRow As Long

Set sheet2016 = Sheets("2016")
Set sheet2017 = Sheets("2017")
Set sheetDiff = Sheets.Add(After:=sheet2017)

lastCol = sheet2016.Cells(1, sheet2016.Columns.Count).End(xlToLeft).Column
lastRow = sheet2016.Cells(sheet2016.Rows.Count, 1).End(xlUp).Row
sheetDiff.Cells(1, 1).Value = sheet2016.Cells(1, 1).Value
For thisCol = 2 To lastCol
    sheetDiff.Cells(1, thisCol * 3 - 4).Value = sheet2016.Cells(1, thisCol).Value & " 2016"
    sheetDiff.Cells(1, thisCol * 3 - 3).Value = sheet2016.Cells(1, thisCol).Value & " 2017"
    sheetDiff.Cells(1, thisCol * 3 - 2).Value = "Difference " & sheet2016.Cells(1, thisCol).Value
Next thisCol
sheetDiff.Cells(1, 1).EntireRow.Font.Bold = True

nextRow = 2
For thisRow = 2 To lastRow
    foundRow = Application.Match(sheet2016.Cells(thisRow, 1).Value, sheet2017.Range("A:A"), 0)
    If Not IsError(foundRow) Then
        sheetDiff.Cells(nextRow, 1).Value = sheet2016.Cells(thisRow, 1).Value
        For thisCol = 2 To lastCol
            foundCol = Application.Match(sheet2016.Cells(1, thisCol).Value, sheet2017.Range("1:1"), 0)
            If Not IsError(foundCol) Then
                sheetDiff.Cells(nextRow, thisCol * 3 - 4).Value = sheet2016.Cells(thisRow, thisCol).Value
                sheetDiff.Cells(nextRow, thisCol * 3 - 3).Value = sheet2017.Cells(foundRow, foundCol).Value
                sheetDiff.Cells(nextRow, thisCol * 3 - 2).FormulaR1C1 = "=RC[-1]-RC[-2]"
            End If
        Next thisCol
        nextRow = nextRow + 1
    End If
Next thisRow

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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