Running anothe subroutine via a macro

ProphetofProfit

New Member
Joined
Feb 28, 2011
Messages
28
I've honestly searched every page on the interwebs so far, tried about 10 different approaches but my ineptitude with VBA wins every time.

Everything up to 'application run' works fine.

Code:
Sub BackTest()
Dim i
Dim r
For r = Sheets("Summary").Range("F1") To Sheets("Summary").Range("F2")
 
  Sheets("Data").Cells(1, 3).Value = Sheets("Odds").Cells(r, 7).Value
  Sheets("Data").Cells(2, 3).Value = Sheets("Odds").Cells(r, 8).Value
  
  Application.Run "'Get Odds!'CmdCalculate_Click"

The subroutine I'm trying to run:

Code:
Sub CmdCalculate_Click()
    For i = 0 To 11
        For j = 0 To 11
.
.
.

I've tried lots of different approaches but no luck so far.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Where is the code for the sub you are trying to call located?
 
Upvote 0
Have you tried just calling it?
Code:
Call cmdCalculate_Click
Another thing you could try us moving the sub to a standard module and renaming it.

Then you should be able to call it from anywhere, including the original Click event/sub.

You might need to change it slightly, depending on the code and what it does.
 
Upvote 0
That's probably because the code is in a worksheet module.

Have you tried the second suggestion?

That should work, but like I said the code might need to be slightly altered.

To tell if it does need altered we would need to see the code.
 
Upvote 0
I promise that all the good will I receive will be paid forward to other unfortunates that I can help.

My macro needs to activate cell A32 of worksheet "data", then use a do..loop until a specific value is found, then offset (0,1) and (0,2) and copy these values.

But it's bugged, again, at the first line, which is the simplest and leads me scratching my head.

Code:
  Sheets("Data").Cells(32, 1).Activate
Do Until Sheets("Data").ActiveCell.Value = Sheets("Odds").Cells(r, 18).Value
  Sheets("Odds").Cells(r, 21).Value = ActiveCell.Offset(0, 1).Value
  Sheets("Odds").Cells(r, 22).Value = ActiveCell.Offset(0, 2).Value
  ActiveCell.Offset(1, 0).Select
Loop
 
Upvote 0
Well you rarely, if ever, need to activate/select anything in Excel VBA.

Here's how that could would look without Activate/Select.
Code:
Dim rng As Range

Set rng = Sheets("Data").Cells(32,1)
 
Do Until rng.Value = Sheets("Odds").Cells(r, 18).Value
      Sheets("Odds").Cells(r, 21).Value = rng.Offset(,1).Value
      Sheets("Odds").Cells(r, 22).Value = rng.Offset(,2).Value
      Set rng = rng.Offset(1)
Loop
Now that might not fix the problem but give it a try and if you still have problems post back with details.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,812
Members
452,945
Latest member
Bib195

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