Consulting

Results 1 to 6 of 6

Thread: Solved: extract values in a range

  1. #1

    Lightbulb Solved: extract values in a range

    Hi,

    I have values 1,2,3,4,5 in (a1 to a5) cells; I am wondering if these values can be extracted using VBA?

    thanks

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Surya,

    They can, and through several ways. Could you decribe what the goal is?

    Mark

  3. #3
    Greeting Mark,
    I have following data in Excel sheet in a1 to a5 fields
    1
    2
    3
    4
    5

    I have function as shown below; the idea is to append them in the following format
    1,2,3,4,5

    [VBA]

    Function myJoinList(ByVal myRange As Range) As String

    [/VBA]

    thanks

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi again,

    As shown, you could use Transpose and Join I believe. In case you may need the vals to come from, let's say, A1:F1 sometime, I think this should grab either. Sorry for the delay - "fuzzy brains" here couldn't get cols/rows in the correct order...

    In a new blank WB:

    On Sheet1, put some vals in A1:F5

    In a Standard Module:
    [vba]
    Sub test()
    MsgBox myJoinList(ThisWorkbook.Worksheets("Sheet1").Range("A1:A5"))
    MsgBox myJoinList(ThisWorkbook.Worksheets("Sheet1").Range("A1:F1"))
    MsgBox myJoinList(ThisWorkbook.Worksheets("Sheet1").Range("A1:F5"))
    End Sub

    Function myJoinList(ByVal myRange As Range) As Variant
    Dim aryIn, aryOut
    Dim x As Long, y As Long

    aryIn = myRange.Value
    ReDim aryOut(0 To 0)

    For x = LBound(aryIn, 1) To UBound(aryIn, 1)
    For y = LBound(aryIn, 2) To UBound(aryIn, 2)
    ReDim Preserve aryOut(1 To UBound(aryOut) + 1)
    aryOut(UBound(aryOut)) = aryIn(x, y)
    Next
    Next

    myJoinList = Join(aryOut, ",")
    End Function
    [/vba]

    Hope that helps,

    Mark

  5. #5
    Hi Mark,
    Many thanks for your prompt response.
    The solution seems to be working; will revert back to you shortly

  6. #6
    Hi mark, Many thanks for resolving my post..

Posting Permissions

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