montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello Everybody. PLEASE, I have two sheets and one dynamic array in each one, what I need is to subtract row by row one from another.
On sheet 1 my array is for example purpose A1:F30, and on sheet 2 B2:G30 [in my workbook each are 6 by 2521 and growing].
this is the macro for better understanding purpose:
Code:
Sub Macro1()
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "=RC[-7]-Sheet2!R[1]C[-6]"
    Range("H1").Select
    Selection.AutoFill Destination:=Range("H1:H23"), Type:=xlFillDefault
    Range("H1:H23").Select
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "=RC[-7]-Sheet2!R[1]C[-6]"
    Range("I1").Select
    Selection.AutoFill Destination:=Range("I1:I23"), Type:=xlFillDefault
    Range("I1:I23").Select
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "=RC[-7]-Sheet2!R[1]C[-6]"
    Range("J1").Select
    Selection.AutoFill Destination:=Range("J1:J23"), Type:=xlFillDefault
    Range("J1:J23").Select
End Sub
Really appreciate the time you've taken out of your schedule to help me.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi

You wrote dynamic arrays in your post but you did not post anything dynamic.

Your example is completely static.

This is an example similar to what you posted: just subtracting 2 ranges.

Sssuming

r1: =Sheet1!A1:F30
r2: =Sheet2!B1:G30
r3: =Sheet3!C1:H30

and you want

r3=r1-r2

Try:

Code:
Sub Test()

Worksheets("Sheet3").Range("C1:H30").Value = [Sheet1!A1:F30-Sheet2!B1:G30]

End Sub
 
Upvote 0
Sorry and Thank you for your time. what I upload said is "just a little example", I didn't upload the real workbook. and again thank you, I will be trying the line.
 
Upvote 0
I know you are completely right, I didn't explained properly. So here we go.I am uploading "partial information only"
Sheet 1
A
B
C
D
E
F
1
6
9
16
29
33
52
2
17
26
30
40
45
49
3
2
6
8
22
35
52
4
5
23
24
34
36
41
5
11
16
21
27
30
31
6
7
15
18
30
47
51
7
1
6
41
49
50
51
8
1
16
18
32
40
48
9
1
7
9
10
21
48
10
4
14
22
30
44
48
11
3
15
24
27
37
50
12
3
28
31
38
39
51
13
1
2
14
26
32
38
14
5
7
26
30
37
46
15
11
19
24
43
48
49
16
16
19
21
37
50
51
17
15
21
32
40
41
46
18
10
27
28
30
45
51
19
4
17
20
21
26
39
20
10
12
20
29
38
49
21
1
15
16
24
33
52
22
1
8
22
24
27
50
23
5
19
20
24
27
51


<tbody>
</tbody>
 
Upvote 0
sheet 1 minus sheet 2 row by row

ABCDEF
111171411123
215202218103
33171612111
46737610
541331720
669231930
70102317103
809922190
9371320230
10112372
1101371121
122261712713
134512458
14612213113
15503622
161211395
175641045
18610891912
1965081210
20934553
<colgroup><col width="21" style="width: 16pt; mso-width-source: userset; mso-width-alt: 768;"> <col width="64" style="width: 48pt;" span="6"> <tbody> </tbody>


and the answers I expect are

IJKLMN
582182149
268223546
1118103441
11621273031
71518241311
165114451
1418324048
179102148
20410248
31320273746
3217163549
1214263238
332222730
1524172643
61921374647
151710344146
101528303741
41720212639
21220131429
1916243346
<colgroup><col width="64" style="width: 48pt;" span="6"> <tbody> </tbody>
 
Last edited by a moderator:
Upvote 0
and this is the code I am working on
Code:
Sub addrows()
Dim x As Long
Dim y As Long
    For x = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        For y = 2 To 7
            Worksheets("Sheet2").Range("H1:M30").Value = [Sheet1!B1:G30-Sheet2!A1:F30]
        Next y
    Next x
    
 End Sub
 
Upvote 0
so my real question is: instead of using regular range references ("H1:M30") I am looking for something like ►cells(rows.count,___).End(xlDown or up).Row . something like this.
 
Upvote 0
I am talking for example also, with this line ►Sheet3.Cells(x - 1, y - 1) = Abs(Cells(x, y) - Cells(x, y + 1))◄ is possible to subtract column by column to the end of the rows, but this is in the same worksheet, the question is how to do that in two different worksheets and the answer in one of them.
Thank you for your time.
 
Upvote 0
A much faster more efficient way of doing this is to use variant arrays . VBA is very slow at accessing the worksheet so looping through the worksheet accessing the sheet multiple times every loop is going to very slow. It is Ok for a few hundred interatiosn but as soon as you get into 1000 it will be slow. However loading and entire sheet into memeory as a variant array and then operaitng in memory is much faster.
You specification is ambigous becuase in your first post you said sheet 1 was A to F ansd sheet 2 B to G while your code in post #6 is the opposite.. So this code baiscally doesit but hte columns and shets could be wrong. ( Note untested)
Code:
Sub test()Dim outarr As Variant


With Worksheets("sheet1")
  lr1 = .Cells(Rows.Count, "A").End(xlUp).Row
  sht1 = Range(.Cells(1, 1), .Cells(lr1, 6))
End With
With Worksheets("sheet2")
  lr2 = .Cells(Rows.Count, "B").End(xlUp).Row
  sht2 = Range(.Cells(1, 2), .Cells(lr2, 7))
ReDim outarr(1 To lr1, 1 To 1)
For i = 2 To lr1
 For j = 1 To 6
    outarr(i, 1) = sht1(i, j) - sht2(i, j)
 Next j
Next i
 Range(.Cells(1, 8), .Cells(lr1, 8)) = outarr


End With


End Sub
 
Upvote 0
I see in your post 1 thing that I don't understand: you have 23 rows in the table in sheet1 and the 2 tables in Sheet2 have 20 rows, also the number of columns is not equal

As a first step, I'll assume

1 - all tables have the same number of rows and columns
2 - the 2 tables you are subtracting start at Sheet1!A1 and Sheet2!A1 and are surrounded by empty cells
3 - the result table starts at Sheet2!H1

Using your data under these assumptions, this is another solution:

Code:
Sub addrows()
Dim rTable1 As Range, rTable2 As Range, rTable3 As Range

' define the tables ranges
Set rTable1 = Worksheets("Sheet1").Range("A1").CurrentRegion
Set rTable2 = Worksheets("Sheet2").Range("A1").CurrentRegion
Set rTable3 = Worksheets("Sheet2").Range("I1").Resize(rTable1.Rows.Count, rTable1.Columns.Count)

rTable3.Value = Evaluate("index(" & rTable1.Address(external:=True) & "-" & rTable2.Address(external:=True) & ",0)")

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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