+ Reply to Thread
Results 1 to 4 of 4

Using variables within functions in a macro.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Using variables within functions in a macro.

    Hi,

    I am trying to write a macro that uses variables as the input to a formula within the macro.

    I would rather not assign the variables A or B to a cell and just assign C to a chosen cell. So for example I am trying to achieve the following:

    Sub macro1()

    Dim A As Integer
    Dim B As Integer
    Dim C As Integer

    A = 4
    B = 5
    C = Evaluate("sum(A,B)")
    Cells(1,1) = C

    End Sub

    This gives me a run-time error 13.

    I feel like I'm just missing something simple so thanks in advance.

  2. #2
    Forum Contributor
    Join Date
    04-13-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    185

    Re: Using variables within functions in a macro.

    perhaps not a good solution, but try this,
    Sub macro1()
    
    Dim A As Integer
    Dim B As Integer
    Dim C As Integer
    
    Cells(1, 1) = 4
    Cells(1, 2) = 5
    C = Cells(1, 1) + Cells(1, 2)
    
    Cells(1, 3) = C
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    10-02-2012
    Location
    Bumi Nusantara
    MS-Off Ver
    Excel 2010; Excel 2016
    Posts
    136

    Re: Using variables within functions in a macro.

    please change

    C = Evaluate("sum(A,B)")

    with this one

    C = WorksheetFunction.Sum(A, B)

  4. #4
    Registered User
    Join Date
    10-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Using variables within functions in a macro.

    Thanks dwint. That did exactly what I wanted.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1