+ Reply to Thread
Results 1 to 4 of 4

passing a variable to a function

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2015
    Location
    Pattaya, Thailand
    MS-Off Ver
    2010
    Posts
    2

    passing a variable to a function

    Hi,
    I want to modify this code to change "J" to a variable value
    ActiveWKS.Range("E3").Formula = "=INDEX(Raw_Data!J8:J100,MATCH(Reconciliation!C2:C89,Raw_Data!A8:A88))"

    A friend suggested this approach. I'm wondering if there is a cleaner, simpler one:

    (pseudo code)
    Function ColLtr(iCol As Long) As String
    ltr = “”
    If iCol > 0 And iCol <= Columns.Count Then
    ltr = Evaluate("substitute(address(1, " & iCol & ", 4), ""1"", """")")
    End If
    pos = InStr(1, ltr, ":")
    If pos > 0 Then
    ltr = Left(ltr, pos - GC_ONE)
    End If
    ColLtr = ltr
    End Function

    So you would do something like:
    Dim colNo as Long
    Dim retVal as String
    ……
    retVal = ColLtr(colNo)
    ActiveWKS.Range("E3").Formula = "=INDEX(Raw_Data!” & retVal & “8:” & retVal & “100,MATCH(Reconciliation!C2:C89,Raw_Data!A8:A88))"

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: passing a variable to a function

    Just
    ActiveWKS.Range("E3").Formula = "=INDEX(Raw_Data!" & range("A8:A100").offset(0, iCol-1).address & ",MATCH(Reconciliation!C2:C89,Raw_Data!A8:A88))"
    shall do.

    Please note how code tags improve readability of the post and edit your post to comply with forum rules http://www.excelforum.com/forum-rule...rum-rules.html
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    06-01-2015
    Location
    Pattaya, Thailand
    MS-Off Ver
    2010
    Posts
    2

    Re: passing a variable to a function

    Quote Originally Posted by Kaper View Post
    Just
    ActiveWKS.Range("E3").Formula = "=INDEX(Raw_Data!" & range("A8:A100").offset(0, iCol-1).address & ",MATCH(Reconciliation!C2:C89,Raw_Data!A8:A88))"
    shall do.

    Please note how code tags improve readability of the post and edit your post to comply with forum rules http://www.excelforum.com/forum-rule...rum-rules.html
    Sorry - I tried and they didn't show in the preview, so I thought maybe the board didn't use them. Maybe I did something wrong.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: passing a variable to a function

    Please do read (and follow) forum rules: http://www.excelforum.com/forum-rule...rum-rules.html

    This time you are breaking:
    12. Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
    PS. If the answer ( concatenated pieces of your formula with
    range("A8:A100").offset(0, iCol-1).address
    in the middle) takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Passing a variable from a Procedure to a Function
    By Hellga in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-01-2013, 10:30 AM
  2. Trouble passing variable to another function
    By Jasmith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-28-2011, 10:15 AM
  3. Passing Boolean Variable To A Function
    By Shama in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-25-2008, 11:20 PM
  4. [SOLVED] Passing multiple strings to variable in Replace Function
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2006, 01:10 PM
  5. [SOLVED] Passing variable to VarType of other Variable
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2005, 10:06 AM

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