Start Condition For Macro

sb1989

New Member
Joined
Jul 30, 2014
Messages
7
Hello,
I am new with VBA/Macros.

I am currently working on a macro and I need the macro to go down an entire column and run everytime the number '2002' is in column R. I have a button set up, but it is currently only working when I am on an active cell. Below is the formula from VBA.

Private Sub CommandButton1_Click()
'
ActiveCell.FormulaR1C1 = _
"=INDEX(R3C3:R8C8,MATCH(RC[-3],R3C1:R8C1,0),MATCH(RC[-2],R1C3:R1C8,0))+IFERROR(INDEX(R3C3:R8C8,MATCH(RC[-2],R3C1:R8C1,0),MATCH(RC[-1],R1C3:R1C8,0)),0)"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A2"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A2").Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = _
"=INDEX(R3C3:R8C8,MATCH(R[-1]C[-2],R3C1:R8C1,0),MATCH(RC[-2],R1C3:R1C8,0))+IFERROR(INDEX(R3C3:R8C8,MATCH(RC[-2],R3C1:R8C1,0),MATCH(RC[-1],R1C3:R1C8,0)),0)"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
ActiveCell.Offset(-1, 0).Range("A1").Select
End Sub


Any help will be greatly appreciated!

Thanks,
Sam
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If you are starting in Col R

Rich (BB code):
range("R2").select
while activecell.value <> ""      'continue untill the rows end
     
     'put your code here  BUT DO NOT USE THE COMMAND:   cell.SELECT
  

     ActiveCell.Offset(1, 0).select       'move to the next row, 
wend
 
Upvote 0
Your code makes no sense, try describing what you want to do where when a cell in column R = 2002.
 
Upvote 0
Your code makes no sense, try describing what you want to do where when a cell in column R = 2002.


I am trying to automate the calculating of distance in for a truck driver.

When a cell in column R = 2002 I want macro to run (this is the home store). When the macro runs it looks up numbers in a matrix and calculates distance for each store the driver delievers to. The beginning store will always be 2002, which is why everytime 2002 is in column R I want the macro to begin.
Here is an example of the trip:

StartStoreEnd
20022005
20062008

<COLGROUP><COL style="WIDTH: 48pt" span=3 width=64><TBODY>
</TBODY>
and here is a sample of the matrix


Store # 200220042005200620072008
Postal CodeL6V 1B71747 countryside drive, bramptonL6Z 1Y4L6T 4G8L7E 4Z8L7G 4B1
2002L6V 1B70.0012.407.566.7425.4018.35
20041747 countryside drive, brampton12.400.006.069.4516.6020.23
2005L6Z 1Y48.086.600.0010.7621.3316.34
2006L6T 4G86.759.4510.210.0023.7222.19
2007L7E 4Z825.3916.5920.7623.230.0032.23
2008L7G 4B118.3620.2316.5322.1632.260.00

<COLGROUP><COL style="WIDTH: 48pt" span=8 width=64><TBODY>
</TBODY>

Please let me know if you need any more clarification.
 
Upvote 0
That makes sense, I would assume from the formula in your code that the matrix range is $A$1:$H$8 (including the headings).

Would I be right to assume Start in column R, Store in Column S, End in Column T?

If there were more stores in the trip list, would the layout follow

Code:
Start Store End 
2002  2004
      2005 
      2006  2008

After finding the relevant data from the matrix, how and where should it appear in relation to the trip list?
 
Upvote 0
That makes sense, I would assume from the formula in your code that the matrix range is $A$1:$H$8 (including the headings).

Would I be right to assume Start in column R, Store in Column S, End in Column T?

If there were more stores in the trip list, would the layout follow

Code:
Start Store End 
2002  2004
      2005 
      2006  2008

After finding the relevant data from the matrix, how and where should it appear in relation to the trip list?


Yes you are right for the assumptions about the matrix range, Column R,S,T and the layout when more stores are in the list.

After finding the relevant data from the matrix, I just want it to appear in column S.

I am beginning to look into IF statements for the conditional start (when 2002 appears in column R), let me know if you think this is a good method to approach this.

Thanks for your help!
 
Upvote 0
I am beginning to look into IF statements for the conditional start (when 2002 appears in column R), let me know if you think this is a good method to approach this.

As a formula approach or vba if statement?

A formula should work, I've already been thinking on that line, but it could be a bit messy, I'll look into it a bit more and see what I come up with.
 
Upvote 0
As a formula approach or vba if statement?

A formula should work, I've already been thinking on that line, but it could be a bit messy, I'll look into it a bit more and see what I come up with.

I was thinking VBA IF statement. Thanks again for the help
 
Upvote 0
Just to keep you updated, I have an 'almost working' vba solution, because of the way your data is laid out in the sheet, I have encountered a scenario that I have not seen before so a bit of research is required.

As soon as I have it figured out, I'll post the code for you, but I might not have it done until morning (uk time).
 
Upvote 0
Just to keep you updated, I have an 'almost working' vba solution, because of the way your data is laid out in the sheet, I have encountered a scenario that I have not seen before so a bit of research is required.

As soon as I have it figured out, I'll post the code for you, but I might not have it done until morning (uk time).

Sounds great.

I do not want to complicate it anymore, but I will also be adding a loop to the formula so that the index/match for the stores (column S) will repeat until there is a blank (no more delieveries to make). You do not have to add that into the code, but if you could tell what loop (do while, until, for) you think would work best for this, that would be helpful so I am moving in the right direction.

Thank you for all your help and time Jason!
 
Upvote 0

Forum statistics

Threads
1,215,745
Messages
6,126,632
Members
449,323
Latest member
Smarti1

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