Results 1 to 9 of 9

Thread: Excel macro question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2003
    Location
    Mt. Holly, NJ
    Posts
    31

    Excel macro question

    Hi all,

    Is there a way to design a macro so that it will function from the Active cell and then affect cells in specific relations to that cell instead of having macros that only affect the exact cells you recorded in your macro?

    For example, i want to be able to affect the active cell, and then move over to cells to the right, enter a formula, and then down two to enter another formula. All I can get now is a macro that will do this, but only do it to the specific cells I recorded them in. So, when I record it in A1, and then C1, and then C3, I can't use this macro to affect B1, and then D1, and then D4.

    Any thoughts on this?
    Thanks much,
    Joshua

  2. #2
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Before I start writing you a macro that works, are you going to be going pass column Z, meaning is the active cell ever going to be AA, AB and so on?
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  3. #3
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Also can you post what the current macro looks like?
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  4. #4
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Well here is what your macro should look like, but now if you are going to go pass column 27 (Z) then let me know so I can modify it. Also please put your formulas where I have comments, since I don't know what formulas you want. I am not sure if the macro is attached to a button or not so I just have it in Sub Macro1.

    VB Code:
    1. Sub Macro1()
    2.     Dim sColumn As String
    3.     Dim nColumn As Integer
    4.     Dim lRow As Long
    5.    
    6.    
    7.     nColumn = ActiveCell.Column
    8.     'move over two columns
    9.     nColumn = nColumn + 2
    10.     If nColumn > 27 Then
    11.         'This is for any columns pass Z
    12.     Else
    13.         sColumn = Chr(nColumn + 64)
    14.     End If
    15.    
    16.     lRow = ActiveCell.Row
    17.    
    18.     Range(sColumn & lRow).Select
    19.    
    20.     'enter your formula here
    21.    
    22.     'Move down two rows
    23.     lRow = lRow + 2
    24.     Range(sColumn & lRow).Select
    25.    
    26.     'enter you other formula here
    27.    
    28. End Sub
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2003
    Location
    Mt. Holly, NJ
    Posts
    31
    Well, the formulas I want to use will be widely varried, however, here is an example:

    I have a worksheet that formulates the interest for an account. The factors for this formula are Interest x principle x time. The interest is found two rows up and three to the left of the cell, the principle is two rows up and two to the left, and the time is two rows up and one to the left.

    Thus if the active cell is D4, the formula should be B1xB2xB3, and if it is E14, then it should be C11xC12xC13, etc.

    What changes would need to be made if passing collumn 27 (Z)? This is a general code rule I need to know, not just something I'm applying to one sheet.

    Thanks very much,
    Joshua

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    when you start to record a macro the macro toolbar pops up, one of the buttons is "relative reference" (it looks like a worksheet), just press it so it is down. Then you can record your macro

  7. #7
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    Originally posted by Maldrid


    VB Code:
    1. Sub Macro1()
    2.     Dim sColumn As String
    3.     Dim nColumn As Integer
    4.     Dim lRow As Long
    5.    
    6.    
    7.     nColumn = ActiveCell.Column
    8.     'move over two columns
    9.     nColumn = nColumn + 2
    10.     If nColumn > 27 Then
    11.         'This is for any columns pass Z
    12.     Else
    13.         sColumn = Chr(nColumn + 64)
    14.     End If
    15.    
    16.     lRow = ActiveCell.Row
    17.    
    18.     Range(sColumn & lRow).Select
    19.    
    20.     'enter your formula here
    21.    
    22.     'Move down two rows
    23.     lRow = lRow + 2
    24.     Range(sColumn & lRow).Select
    25.    
    26.     'enter you other formula here
    27.    
    28. End Sub
    I have to say that is a weird way of doing it.
    Have you not seen the Offset command before?
    VB Code:
    1. 'Move down two rows
    2. ActiveCell.Offset(2, 0).Select

  8. #8
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Agmorgan,

    No I haven't heard of the offset command. I don't write macros in excel so I'm not familiar with all the properties and functions. Otherwise I would of use it .

    Yesuslave,

    I think your best bet is to do what Si_The_Geek wrote, because from what I gather it doesn't seem like you know how to write your own macros. I might be wrong . Since what you are trying to do isn't so complex you can just do what he wrote. Let us know if you need anything else.
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  9. #9
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    in tools macro
    paste this code into a module
    you have one already since you recorded a macro
    place the cursor in the cell you want the formulas to start from the run macro mac
    please check that the icols is the number of cols you want to populate

    the select case is where you would build another formula and then
    c.FormulaR1C1 = getformula("interest")
    replace the new name in the paraen and also in the select
    Case "otherform"

    Sub Mac()


    Dim r As Range
    Dim c As Range
    Dim lCols As Long
    lCols = 64


    Set r = Range(ActiveCell.Address, ActiveCell.Offset(0, lCols).Address)


    For Each c In r


    c.FormulaR1C1 = getformula("interest")
    Next

    a = 0


    End Sub


    Public Function getformula(ftype As String) As String
    Select Case ftype
    Case "interest"
    lRow = -2
    i = -3
    p = -2
    t = -1
    getformula = "=R[" & lRow & "]C[" & i & "]*R[" & lRow & "]C[" & p & "]*R[" & lRow & "]C[" & t & "]"
    Case "otherform"
    lRow = -2
    i = -2
    t = 0
    p = -1
    getformula = "=R[" & lRow & "]C[" & i & "]*R[" & lRow & "]C[" & t & "]*R[" & lRow & "]C[" & p & "]"
    Case Else
    MsgBox "not handled"
    getformula = "=err"
    End Select

    End Function
    otherform

Posting Permissions

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



Click Here to Expand Forum to Full Width