Consulting

Results 1 to 4 of 4

Thread: setting a cell to a formula

  1. #1

    setting a cell to a formula

    Hi, I am trying to make a cell equal to a formula in VBA using a variable for part of the range. I've tried the following two ways to do it and both fail.

    Range("H" & endsal + 2).Select
    Dim lastsal As String
    lastsal = Selection.Address
    MsgBox (lastsal)
    ActiveCell.Formula = "=AVERAGE(H21:& lastsal)"

    AND
    Range("H" & endsal + 2).Select
    Dim lastsal As String
    lastsal = Selection.Address
    MsgBox (lastsal)
    ActiveCell.Formula = "=AVERAGE(H21:"& lastsal ")"

    Can you help me in getting this to work?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    "=AVERAGE(H21:"& lastsal ")"
    Almost there
    "=AVERAGE(H21:"& lastsal & ")"

    Also
    "=AVERAGE(H21:H" & endsal + 2 & ")"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Thank you so much! That got rid of the error but the formula is not giving me the result I need. it's just giving me a - . Any ideas? the range is correct; each cell has a dollar amount in it.

  4. #4
    Hi again, you can disregard this problem as I figured out what was wrong. Thank you so much for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •