From macro to vba

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone.
I am here trying to see the transformation from a simple Macro to a Code, I know guy for you is something so Simple, but “ sorry “ no for me, I am a new self-thaught bee.
I just sum two numbers with the macro recorder I got this:

Range("H2").Select
ActiveCell.Formula = "=SUM(A2:B2)"

So with my Basic knowledge I see that H2 has to be a variable, sum I can think as a total, or another variable, and the two location as well, so I got this:

Sub real_code()
Dim H As Integer, A As Integer, B As Integer
A = Range("A2").Value: B = Range("B2").Value
H = A + B
Range("H1").Value = H
End Sub

And “”Bingo”” work well.

But now I intent to transform the macro using the function COUNT,

Sub C_m()
Range("H6").Select
ActiveCell.Formula = "=COUNT(B:B)"
End Sub

BUT then, I realize my logic do not worked as before, the only thing
So far, I can see is, must be a loop, how I will deduce that?, “ I don’t know” that why I am here.
Thank you.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
What do you need?
Forget the macro and the code for a moment, you can explain with your words what you want to do.
 
Upvote 0
wow, thank you so much Mr. D a n t e A m o r, your offer is really Priceless.
explain with your words what you want to do.
Still I am in the same idea of counting my Dynamic Array.
VBA Code:
Sub DanteAmor()    Dim r As Range, wMin As Double, wMax As Double, wRow As Long, wCol As Long, i As Long, j As Long
    
    Set r = Range("A1", Range("A" & Rows.Count).End(xlUp))
    wMin = WorksheetFunction.Min(r)
    wMax = WorksheetFunction.Max(r)
    r.Offset(0, 2).Resize(r.Count, wMax + 1).ClearContents
    
    wRow = 2
    For i = wMin To wMax
        wCol = 4
        Cells(wRow, "C").Value = i
        For j = wMin To wMax
            Cells(1, wCol).Value = j
            Cells(wRow, wCol).Value = Evaluate("SUM((" & r.Address & "=" & j & ")*(" & r.Offset(1).Address & "=" & i & "))")
            wCol = wCol + 1
        Next
        wRow = wRow + 1
    Next
    MsgBox "Done"
End Sub

in this code, you know count one column and displays a matrix, so now I would like count a dynamic array of six columns (B2:G2600) and the numbers in this array is a set from one to 53 the maximun number.
BCDEFG
1151730424449
2111518192249
381522254023
4121319203647
5357131948
6131517204420
73410233848
8131727404242
92611143147
103413283534
11123132846
12102326363748
13121820323842
14121517183142
1571415223742
1662428404738
1762026294248
183519202745
1921027313335
2012124383945
2151721384144
22162027314047
23121730434441
24101621294146
2531114152743
264824344340
2741112193145
2892223293140
2971524303144
30171923313349

and the matrix will be
OPQRSTETC
FROM 123 UNTIL 53
FROM 1
2
3
UNTIL
53
 
Last edited by a moderator:
Upvote 0
And what is the result you expect in the matrix?
You could explain a couple of examples with the data that you put.
 
Upvote 0
And what is the result you expect in the matrix?
You could explain a couple of examples with the data that you put.
So, Sorry DanteAmor
[TABLE="width: 512"]
<tbody>[TR]
[TD="class: xl63, width: 64"][/TD]
[TD="class: xl64, width: 64, align: right"]1[/TD]
[TD="class: xl64, width: 64, align: right"]2[/TD]
[TD="class: xl64, width: 64, align: right"]3[/TD]
[TD="class: xl64, width: 64, align: right"]4[/TD]
[TD="class: xl64, width: 64, align: right"]5[/TD]
[TD="class: xl64, width: 64, align: right"]6[/TD]
[TD="class: xl64, width: 64, align: right"]7[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]1[/TD]
[TD="class: xl63, align: right"]37[/TD]
[TD="class: xl63, align: right"]30[/TD]
[TD="class: xl63, align: right"]25[/TD]
[TD="class: xl63, align: right"]29[/TD]
[TD="class: xl63, align: right"]23[/TD]
[TD="class: xl63, align: right"]24[/TD]
[TD="class: xl63, align: right"]21[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]2[/TD]
[TD="class: xl63, align: right"]30[/TD]
[TD="class: xl63, align: right"]21[/TD]
[TD="class: xl63, align: right"]25[/TD]
[TD="class: xl63, align: right"]26[/TD]
[TD="class: xl63, align: right"]15[/TD]
[TD="class: xl63, align: right"]16[/TD]
[TD="class: xl63, align: right"]11[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]3[/TD]
[TD="class: xl63, align: right"]31[/TD]
[TD="class: xl63, align: right"]35[/TD]
[TD="class: xl63, align: right"]15[/TD]
[TD="class: xl63, align: right"]23[/TD]
[TD="class: xl63, align: right"]22[/TD]
[TD="class: xl63, align: right"]18[/TD]
[TD="class: xl63, align: right"]13[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]4[/TD]
[TD="class: xl63, align: right"]29[/TD]
[TD="class: xl63, align: right"]21[/TD]
[TD="class: xl63, align: right"]22[/TD]
[TD="class: xl63, align: right"]21[/TD]
[TD="class: xl63, align: right"]25[/TD]
[TD="class: xl63, align: right"]16[/TD]
[TD="class: xl63, align: right"]11[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]5[/TD]
[TD="class: xl63, align: right"]19[/TD]
[TD="class: xl63, align: right"]18[/TD]
[TD="class: xl63, align: right"]20[/TD]
[TD="class: xl63, align: right"]23[/TD]
[TD="class: xl63, align: right"]11[/TD]
[TD="class: xl63, align: right"]19[/TD]
[TD="class: xl63, align: right"]13[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]6[/TD]
[TD="class: xl63, align: right"]24[/TD]
[TD="class: xl63, align: right"]20[/TD]
[TD="class: xl63, align: right"]21[/TD]
[TD="class: xl63, align: right"]14[/TD]
[TD="class: xl63, align: right"]14[/TD]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63, align: right"]18[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]7[/TD]
[TD="class: xl63, align: right"]24[/TD]
[TD="class: xl63, align: right"]16[/TD]
[TD="class: xl63, align: right"]13[/TD]
[TD="class: xl63, align: right"]20[/TD]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63, align: right"]16[/TD]
[TD="class: xl63, align: right"]7[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]8[/TD]
[TD="class: xl63, align: right"]20[/TD]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63, align: right"]11[/TD]
[TD="class: xl63, align: right"]7[/TD]
[TD="class: xl63, align: right"]13[/TD]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63, align: right"]2[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]9[/TD]
[TD="class: xl63, align: right"]13[/TD]
[TD="class: xl63, align: right"]6[/TD]
[TD="class: xl63, align: right"]12[/TD]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63, align: right"]8[/TD]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63, align: right"]9[/TD]
[/TR]
[TR]
[TD="class: xl65, align: right"]10[/TD]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63, align: right"]14[/TD]
[TD="class: xl63, align: right"]13[/TD]
[TD="class: xl63, align: right"]5[/TD]
[TD="class: xl63, align: right"]8[/TD]
[TD="class: xl63, align: right"]10[/TD]
[/TR]
</tbody>[/TABLE]
instead of search into one column, this time will count 6 columns and display how many time each number follow each number, the random numbers in this array are a set of 1 to 53. Thank you for your time.
 
Last edited:
Upvote 0
You can explain how you get a 37 and how you get a 30


12
13730
2
 
Last edited by a moderator:
Upvote 0
You can explain how you get a 37 and how you get a 30


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]37[/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The fact is, those numbers are just an example, about how looks like.
In other words if I don't have the code I really don't know what the real results will be.
Sorry.
 
Upvote 0
The fact is, those numbers are just an example, about how looks like.
In other words if I don't have the code I really don't know what the real results will be.
Sorry.

But to make the code, I need you to explain how to get to the result.

Forget the code for a moment.
What do you want to do? Do you want to add, or to count, what does it mean?
 
Upvote 0
Sorry about all this. and thank you for your patient.
One post before, with Your code I was able to get the results from one column only. Now I would like to get the results no just from one column, I need to COUNT in each column.
COUNT.
More or less let say, is to expand your code, and instead to count only the column in the example, count the whole entire DYNAMIC array.
BCDEFGH I JKLMN 13
1 12
215 123456789101112
311 13730312919242420131085
48 23021352118201696295
512 32525152220211311121488
63 4292623212314207101359
713 523152225111410138599
83 62416181619916102871
913 72111131113187291046
102 81217611965511545
113 91611141338764343
121 10198101045557226
1310 111098867555232
1412 129116955691443
1512 13910911103365351
167 14963872306142
176 151294380825712
186 16554715210356
193 17434122403410
202 18722312243211
211 19004034430112
225 20321312241111
2316 21423121122000
2412 22021311110010
2510 23120044000011
263 24311103101101
274 25020011100001
284 26122040000000
299 27000000000100
307 28001021101000
3117 29101000010000
3212 30101000002000
338 31001100000000
3415 32000100000000
351 33000000000000
3620 34000000001000
372 35001100000000
385 36000000000000

AND THIS IS YOUR CODE FROM THE LAST POST.
VBA Code:
  Sub g_n()    Dim r As Range, wMin As Double, wMax As Double, wRow As Long, wCol As Long, i As Long, j As Long
    
    Set r = Range("A1", Range("A" & Rows.Count).End(xlUp))
    wMin = WorksheetFunction.Min(r)
    wMax = WorksheetFunction.Max(r)
    r.Offset(0, 2).Resize(r.Count, wMax + 1).ClearContents
    
    wRow = 2
    For i = wMin To wMax
        wCol = 4
        Cells(wRow, "C").Value = i
        For j = wMin To wMax
            Cells(1, wCol).Value = j
            Cells(wRow, wCol).Value = Evaluate("SUM((" & r.Address & "=" & j & ")*(" & r.Offset(1).Address & "=" & i & "))")
            wCol = wCol + 1
        Next
        wRow = wRow + 1
    Next
    MsgBox "Done"
End Sub
 
Last edited by a moderator:
Upvote 0
Try this.

The result will begin in column I

Code:
Sub g_n()
    Dim r As Range, wMin As Double, wMax As Double, wRow As Long
    Dim cini As Long, wCol As Long, i As Long, j As Long
    
    Application.ScreenUpdating = False
    Set r = Range([COLOR=#0000ff]"B1", Range("G" & Rows.Count).End(xlUp)[/COLOR])
    wMin = WorksheetFunction.Min(r)
    wMax = WorksheetFunction.Max(r)
    cini = r.Columns.Count + 3
    Range(Cells(1, cini), Cells(Rows.Count, Columns.Count)).ClearContents
    
    wRow = 2
    For i = wMin To wMax
        wCol = cini + 1
        Cells(wRow, cini).Value = i
        For j = wMin To wMax
            Cells(1, wCol).Value = j
            Cells(wRow, wCol).Value = Evaluate("SUM((" & r.Address & "=" & j & ")*(" & r.Offset(1).Address & "=" & i & "))")
            wCol = wCol + 1
        Next
        wRow = wRow + 1
    Next
    Application.ScreenUpdating = False
    MsgBox "Done"
End Sub

Note:
For a matrix of: B2:G2600, the macro will take a long time.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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