You are here:

Excel/Call Macro from IF

Advertisement


Question
QUESTION: Have recorded macro below.  I have in a cell B17 a ref.  When that ref = say  "VB" I want the macro to call another macro called "CutPaste".  At the moment it just places the words 'Call CutPaste ...' in a cell.

I would like the macro to show  B17  instead of the R/C codes and how do I show the code for calling the next macro "CutPaste"
************
ActiveCell.FormulaR1C1 = "=IF(R[-4]C[-4]=""VB"",""Call CutPaste"","""")"
************

ANSWER: Hi Charlie,

A cell function cannot call a macro.
However, you can have VBA respond to changes made in your cells, this is called event code.

If you rightclick the tab of the sheet and choose "View code" you're taken to the module behind the worksheet. In this module you can write VBA code that will respond to an event, for example:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
   'only do something if we're changing a cell in column A
   If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
   
   'If the corresponding cell in column B contains VB then we do something
   If Target.Cells(1, 1).Offset(, 1).Value = "VB" Then
       'call your macro here:
       Call Macro1
   Else
       MsgBox "Not VB"
   End If
End Sub


---------- FOLLOW-UP ----------

QUESTION: Hi
I have done as explained. I have not done a macro thru right click on sheet. (I am quite new to
There seems to be nowhere to give the macro a name and save.At the moment if I go to Tools-Macros it does not list.
I only haveone cell that will change and start event, can that part give cell ref.
...Range("A:A"))     could this be  ..Range("B17")  ?

Thanks for your help.
Regards
Charlie

Answer
To achieve what you need, modify the code behind the sheet like this:

(This assumes you have recorded a macro called "CutPaste" which needs to be run when B17 contains BV)

Private Sub Worksheet_Change(ByVal Target As Range)
  'only do something if we're changing a cell in column A
  If Intersect(Target, Me.Range("B17")) Is Nothing Then Exit Sub
  
  'If the corresponding cell in column B contains VB then we do something
  If Target.Cells(1, 1).Offset(, 1).Value = "VB" Then
      'call your macro here:
      Call CutPaste
  Else
      'Do nothing
  End If
End Sub

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jan Karel Pieterse

Expertise

Excel and Excel/VBA questions

Experience

Excel MVP

Organizations
Self employed Excel developer

Education/Credentials
Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2012 About.com, a part of The New York Times Company. All rights reserved.