VBA divide two tables?

Dimitris254

Board Regular
Joined
Apr 25, 2016
Messages
139
i have two tables (not Excel tables, but data in a table format) like this: Imgur: The most awesome images on the Internet
i'd like to divide table "part" with table "whole", so that i get the percentages like this: Imgur: The most awesome images on the Internet

there is no extreme formula here, just M4/B4 in cell B4, M5/B5 in cell B5 etc. (formatted as percentage)

is it possible to grab the whole range of table "part" and divide it by table "whole", ie. .Range(M4:U13) / .Range(B4:J13) ?
or the division has to be cell by cell?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
If you're happy using an array formula ...

Table1: =A2:B3
Table2: =D2:E3

Output: {=Table2/Table1} Array-entered


Excel 2010
ABCDEFGH
1Table1Table2Output
210202220%10%
3304062020%50%
Sheet1
 
Upvote 0
i'm looking for a macro, since this action is part of a bigger macro

as aforementioned, the following does not work ("type mismatch" error) :(
Code:
With ActiveSheet

    .Range("B4:J13") = .Range("M4:U13") / .Range("B4:J13")


End With
 
Upvote 0
Assuming data in Sheet1 results in Sheet2 maybe this

Code:
With Sheets("Sheet1")
    Sheets("Sheet2").Range("[B]B[/B]4:J13") = Evaluate( _
        Replace("@" & .Range("M4:U13").Address & "/@" & .Range("B4:J13").Address, "@", .Name & "!"))
End With

Hope this helps

M.
 
Last edited:
Upvote 0
i've heard stories told in the shadows about Evaluate, now i see it with my eyes :eek:
if you could explain the logic too, that would great

but in any case it does work perfectly, thanks! :biggrin:
 
Upvote 0
Last edited:
Upvote 0
one quick follow-up: some cells have #DIV/0 (expected), how do i integrate this into the Evaluate, so that these cells return 0 ?
 
Upvote 0
one quick follow-up: some cells have #DIV/0 (expected), how do i integrate this into the Evaluate, so that these cells return 0 ?

Try

Code:
With Sheets("Sheet1")
    Sheets("Sheet2").Range("B4:J13") = Evaluate( _
        Replace("=IFERROR(@" & .Range("M4:U13").Address & "/@" & .Range("B4:J13").Address & ",0)", "@", .Name & "!"))
End With

M.
 
Upvote 0
i'm looking for a macro, since this action is part of a bigger macro

Sorry, didn't notice the reference in the heading to "VBA".

Here's another way you could do it:

Code:
With Sheets("Sheet2").Range("B4:J13")
    .Formula = "=IFERROR(Sheet1!M4/Sheet1!B4,0)"
    .Value = .Value     'optional
End With
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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