Consulting

Results 1 to 7 of 7

Thread: VBA Code Help

  1. #1

    VBA Code Help

    I need to use the following excel code in a userform:

    "=IFERROR(INDEX('C:\[Query 4.xlsx]Complex Query 4'!$AB$2:$AB$1008,MATCH($B8&TEXT1,'C:\[Query 4.xlsx]Query 4'!$E$2:$E$1008&'C:\[Query 4.xlsx]Query 4'!$A$2:$A$1008,0)),"")"

    Where TEXT1:

    "=LEFT('Project Allocation Sheet'!J$6,2)&TEXT(LEFT(SUBSTITUTE(SUBSTITUTE(LEFT('Project Allocation Sheet'!J$6,FIND(" ",'Project Allocation Sheet'!J$6&" ")-1),LEFT('Project Allocation Sheet'!J$6,2),""),"-",REPT(" ",10)),10),"000")&"-"&TEXT(RIGHT(SUBSTITUTE(SUBSTITUTE(LEFT('Project Allocation Sheet'!J$6,FIND(" ",'Project Allocation Sheet'!J$6&" ")-1),LEFT('Project Allocation Sheet'!J$6,2),""),"-",REPT(" ",10)),10),"000")"

    What is the VBA code so I could use it in a userform?

    Thank you

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    [vba]

    Const ws As String = "'C:\[Query 4.xlsx]Complex Query 4'!"
    Const wsE As String = ws & "$E$2:$E$1008"
    Const wsAB As String = ws & "$AB$2:$AB$1008"
    Dim tmp As Variant
    Dim res As Variant

    With Worksheets("Project Allocation Sheet")

    tmp = Replace(Replace( _
    Left$(.Range("J6").Value, InStr(.Range("J6").Value & " ", " ") - 1), _
    Left$(.Range("J6").Value, 2), ""), _
    "-", " ")
    tmp = Left$(.Range("J6").Value, 2) & _
    Format(Left$(tmp, 10), "000") & "-" & _
    Format(Right$(tmp, 10), "000")
    End With

    res = Application.Evaluate("IFERROR(INDEX(" & wsAB & "," & _
    "MATCH($B8&" & tmp & "," & wsE & "&" & wsAB & ",0)),"""")")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    Try this

    [vba]

    Const ws As String = "'C:\[Query 4.xlsx]Complex Query 4'!"
    Const wsE As String = ws & "$E$2:$E$1008"
    Const wsAB As String = ws & "$AB$2:$AB$1008"
    Dim tmp As Variant
    Dim res As Variant

    With Worksheets("Project Allocation Sheet")

    tmp = Replace(Replace( _
    Left$(.Range("J6").Value, InStr(.Range("J6").Value & " ", " ") - 1), _
    Left$(.Range("J6").Value, 2), ""), _
    "-", " ")
    tmp = Left$(.Range("J6").Value, 2) & _
    Format(Left$(tmp, 10), "000") & "-" & _
    Format(Right$(tmp, 10), "000")
    End With

    res = Application.Evaluate("IFERROR(INDEX(" & wsAB & "," & _
    "MATCH($B8&" & tmp & "," & wsE & "&" & wsAB & ",0)),"""")")
    [/vba]
    Thanks. For some reason, the userform is returning nothing although the formula works fine. When I drag the formula across the remaining cells, all the results are returned accurately. I don't know what I am missing

  4. #4
    Quote Originally Posted by xld
    Try this

    [vba]

    Const ws As String = "'C:\[Query 4.xlsx]Complex Query 4'!"
    Const wsE As String = ws & "$E$2:$E$1008"
    Const wsAB As String = ws & "$AB$2:$AB$1008"
    Dim tmp As Variant
    Dim res As Variant

    With Worksheets("Project Allocation Sheet")

    tmp = Replace(Replace( _
    Left$(.Range("J6").Value, InStr(.Range("J6").Value & " ", " ") - 1), _
    Left$(.Range("J6").Value, 2), ""), _
    "-", " ")
    tmp = Left$(.Range("J6").Value, 2) & _
    Format(Left$(tmp, 10), "000") & "-" & _
    Format(Right$(tmp, 10), "000")
    End With

    res = Application.Evaluate("IFERROR(INDEX(" & wsAB & "," & _
    "MATCH($B8&" & tmp & "," & wsE & "&" & wsAB & ",0)),"""")")
    [/vba]
    When I use:

    MsgBox res, I get a "Type Mismatch" error

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Post the workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Quote Originally Posted by xld
    Post the workbook.
    Find attached a sample
    Attached Files Attached Files

  7. #7
    Quote Originally Posted by xld
    Post the workbook.
    and the data worksheet
    Attached Files Attached Files

Posting Permissions

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