Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I create a macro in excel that varies depending on the cell?

I am working on a workbook in excel that has several worksheets within it. On the last page I have created a template to be filled in from numbers on the previous page. The numbers will be taken from all 12 of the previous pages. However, I would like to be able to select a different cells at will to perform this action on. i.e. Sometimes on the first name on the list, sometimes on the last name. How do I do this? I hope I explained this well. If not ask.

Update:

Range("B5").Select

ActiveCell.FormulaR1C1 = "=January!R[-2]C[1]"

Range("B6").Select

ActiveCell.FormulaR1C1 = "=January!R[-5]C[2]"

Range("B7").Select

ActiveCell.FormulaR1C1 = "=January!R[-4]C[-1]"

Range("B10").Select

ActiveCell.FormulaR1C1 = "=January!R[-7]C[6]"

Range("B11").Select

ActiveCell.FormulaR1C1 = "=February!R[-8]C[6]"

Range("B12").Select

ActiveCell.FormulaR1C1 = "=March!R[-9]C[6]"

Range("B13").Select

ActiveCell.FormulaR1C1 = "=April!R[-10]C[6]"

Range("B14").Select

ActiveCell.FormulaR1C1 = "=May!R[-11]C[6]"

Range("B15").Select

ActiveCell.FormulaR1C1 = "=June!R[-12]C[6]"

Range("B16").Select

ActiveCell.FormulaR1C1 = "=July!R[-13]C[6]"

Range("B17").Select

ActiveCell.FormulaR1C1 = "=Aug!R[-14]C[6]"

Range("B18").Select

ActiveCell.FormulaR1C1 = "=Sep!R[-15]C[6]"

Range("B19").Select

ActiveCell.FormulaR1C1 = "=Oct!R[-16]C[6]"

Range("B20").Select

Active

Update 2:

Thanks devilish. This is my first macro, so some of this went over my head. I simply recorded that code. So doing this in VBS is kinda difficult. If you could simplify that would be great. ha sorry

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    You'll need to use an InputBox. Go to the Visual Basic Editor (Alt + F11), then use help to look up the InputBox Method. Look at the last example for the InputBox method. That will allow you to select a cell range.

    Dim myCell As Range

    Worksheets("Sheet1").Activate

    Set myCell = Application.InputBox( _

    prompt:="Select a cell", Type:=8)

    Then set the cell you want equal to myCell.

    Type 8 specifies a range object.

    By the way, I was looking at your sample code. You could way simplify that by using a For Each...Next Loop. You also don't need all of those select statements.

    For example:

    For Each x In [B5:B20]

    'Your code here.

    Next x

Still have questions? Get your answers by asking now.