Vba Minus

erdow

New Member
Joined
May 30, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi to all experts,

i have a file with multiple sheets with same format and it subtracts automatically C-D using vba and puts the answer E when i paste new data on it
What i want to do is apply this vba code to F-G=H and I-J=K and P-Q=R on each sheet but couldn't :(

I use this for consolidating datas which i get every month from branches and i just want to copy and paste whole sheet at once (not column by column) whitout controlling if there are any wrong formulas on it..
I couldnt attach my exact file but have a wetransfer link for it.

https://wetransfer.com/downloads/6e042427420d4274366afc6a9295fed920211123070850/706911dd6926a8cf70831d8afcda69c920211123070909/98cde3

Thanks a looottt in advance.
Best regards..
 

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".
There is two code on each sheet, one is,

Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRw As Long
LastRw = ListObjects(1).DataBodyRange.Rows.Count + 10 'Last table row + 10
If Not Intersect(Target, Range("C11:d" & LastRw)) Is Nothing Then

Call CminusO(LastRw)
End If
End Sub


second code is like that,

Sub CminusO(LastRw As Long)
Dim LastE As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

LastE = Cells(Rows.Count, "E").End(xlUp).Row
Range("e11:e" & LastE) = vbNullStr 'clear any stray E data below table if exists?

Range("e11:e" & LastRw) = Evaluate("C11:C" & LastRw & "-d11:d" & LastRw)

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

End Sub
 

Attachments

  • minus0.jpg
    minus0.jpg
    201.1 KB · Views: 5
Upvote 0
this is a simpler solution using Table instead of VBA.
let say you've 2 columns of data like this;

101
202
303
404
505
606
707
808
909
10010


Select all the data and enter with control-T to create a table of the data;

Column1Column2
101
202
303
404
505
606
707
808
909
10010


input the formula in Cell C2, it will automatically copy down to the end of the table;

Book1
ABC
1Column1Column2Column3
21019
320218
430327
540436
650545
760654
870763
980872
1090981
111001090
Sheet1
Cell Formulas
RangeFormula
C2:C11C2=[@Column1]-[@Column2]


whenever you add data to the end of the table, formula will copy down automatically;

Book1
ABC
1Column1Column2Column3
21019
320218
430327
540436
650545
760654
870763
980872
1090981
111001090
1240436
1350545
1460654
1570763
Sheet1
Cell Formulas
RangeFormula
C2:C15C2=[@Column1]-[@Column2]
 
Upvote 0
Thanks AlanY but it does not work for me, its just using formula but every time i copy and paste b11:r35 from the sheets which sent by branches to me and if there are any wrong or corrupted formulas on tables which i copy datas from, my table shows wrong report. All those 3 columns must calculate and put the correct result even i paste a corrupted formula on it. You can see my file on link, it just does it for first copumn but i want to apply it to other columns using same code. Thanks for your inerest.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Subtract using vba minus0
and Subtract using vba minus0 - OzGrid Free Excel/VBA Help Forum
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

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