from macro to code

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
ONEDAYTEST.xlsm
WX
1
2199.986
3199.986
4133.324
5199.986
666.662
7133.324
8199.986
9266.648
10199.986
11199.986
Sheet4
Cell Formulas
RangeFormula
W2:W11W2=(X2*100)/3.0003


hello.
I am trying to looping the following macro
VBA Code:
Sub Macro1()

    Range("W2").Formula = "=(X2*100)/3.0003"
    Range("W2").Select
    Selection.AutoFill Destination:=Range("W2:W11"), Type:=xlFillDefault
    Range("W2:W11").Select
    
End Sub

what I tried was
VBA Code:
Sub TEST()
Dim I As Variant
For I = 2 To 11
For j = 2 To 11

Cells(I, j).Value = Cells(I, j).Value * 100 / 3.0003

Next
Next

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This is similar code with your recording macro:
VBA Code:
Range("W2:W11").Formula = "=(X2*100)/3.0003"

For the 2nd code you tried, I am not sure if you were trying to loop from column 2 to column 11 ( j = 2 to 11) (column B to L) ?

If that is your actual requirement , try to post mini sheet again with your input and expected output.
 
Upvote 0
I'm not sure why you are trying to loop. If you goal is get values (instead of formulas) into W2:W11 I would recommend dealing with the whole range at once, rather than one cell at a time.

You can apply formulas first and change them to the formula results ..

VBA Code:
Sub Test_1()
  With Range("W2:W11")
    .Formula = "=X2*100/3.0003"
    .Value = .Value
  End With
End Sub

.. or you can go straight to the results.

VBA Code:
Sub Test_2()
  With Range("W2:W11")
    .Value = Evaluate(.Offset(, 1).Address & "*100/3.0003")
  End With
End Sub
 
Upvote 0
Solution
Thank you.
If I talk about loops, is because I am dragging down something, for me is repeating something, and loops are repetitions. and for bebo yes is my mistake, thanks for pointing out that.
I am not looping through the col. just the rows, good lesson, here is about the ideas really no the code, is to see how is possible to do it with for next for example then I got better
idea about looping. the transformation from regular English to an algorithm.

Thank you Peter.
 
Upvote 0
Hi. again/ I figure out.

VBA Code:
Sub addrows()
Dim x As Long

      For x = 2 To 11
            Cells(x, "w").Value = (Cells(x, "x").Value * 100) / 3.0003
      Next x
 
End Sub
 
Upvote 0
Hi. again/ I figure out.

VBA Code:
Sub addrows()
Dim x As Long

      For x = 2 To 11
            Cells(x, "w").Value = (Cells(x, "x").Value * 100) / 3.0003
      Next x
 
End Sub
Loop is OK for few rows (11 rows)
But for several thousand rows, solution in #2 & #3 help to increase code speed.
 
Upvote 0
bebo021999 - Thank you for your lesson. I am trying in my little time, to learn this language by myself. So I really appreciate your opinion.
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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