Excel/VBA Solution

Advertisement


Question
I recorded a Macro to enter formulas in specific cells on the second sheet in the workbook. The second sheet is called "EventAvailability". The formulas refer to specific cells on the first sheet, which is called "PasteHere". The problem is that whenever I run the macro, it doesn't always enter the formulas in the correct cells.

I am very new to vba, which is why I stick with the recorder, but I cannot find a solution. I've come across your name on forums when I search for answers to my excel questions, so I was hoping you might be able to offer some assistance here as well.

Here is what my recorded Macro looks like.

Sub formulas()
'
' formulas Macro
'

'
   ActiveCell.FormulaR1C1 = "=PasteHere!R1C4"
   Range("A5").Select
   ActiveCell.FormulaR1C1 = "=PasteHere!R[-3]C1"
   Range("A5").Select
   Selection.AutoFill Destination:=Range("A5:A19"), Type:=xlFillDefault
   Range("A5:A19").Select
End Sub

Answer
Jason,

Sub formulas()
'
' formulas Macro
'

'
   ActiveCell.FormulaR1C1 = "=PasteHere!R1C4"
   Range("A5").Select
   ActiveCell.FormulaR1C1 = "=PasteHere!R[-3]C1"
   Range("A5").Select
   Selection.AutoFill Destination:=Range("A5:A19"), Type:=xlFillDefault
   Range("A5:A19").Select
End Sub

in the above code. the first command works on the ActiveCell when you run the macro.  

I have no way of knowing where you want that formula entered, but you need to specify that in the macro by adding a command above that command.   Let us say you want to enter that first formula in cell  A3.  then you would modify the macro to be:

Sub formulas()
'
' formulas Macro
'

'
   Range ("A3").Select   '<== added line.  Change to refer correct cell
   ActiveCell.FormulaR1C1 = "=PasteHere!R1C4"
   Range("A5").Select
   ActiveCell.FormulaR1C1 = "=PasteHere!R[-3]C1"
   Range("A5").Select
   Selection.AutoFill Destination:=Range("A5:A19"), Type:=xlFillDefault
   Range("A5:A19").Select
End Sub

modified like the above, then the first formula is not dependent on what cell is the activecell, but would input the formula in the cell you want.

--
Regards,
Tom Ogilvy

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


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.