Results 1 to 7 of 7

Thread: [RESOLVED] Copy column of items in clipboard to an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Resolved [RESOLVED] Copy column of items in clipboard to an array

    I would like to create an array of strings that is created from the items in the clipboard.

    Example text:
    Code:
    Item Name ABC
    Item Name 123
    Item Name QWE
    Item Name 456
    Say the above cells data I've copied into the clipboard. I'd like to have a loop look at each item in my clipboard and assign it to an array, say myStrArray.

    Basically i'll have:
    Code:
    Item Name ABC = myStrArray0
    Item Name 123 = myStrArray1
    Item Name QWE = myStrArray2
    Item Name 456 = myStrArray3
    Eventually I will be using this in a bigger script but I just cant find/figure out how to do this. Could anyone help me? Your help is greatly appreciated.
    I have searched around the net for this and can't find it. Is this possible?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy column of items in clipboard to an array

    use the split function
    try
    vb Code:
    1. mystrarray = split(mystr, vbnewline)
    where the clipboard data is inputted into mystr

    you don't say how you are getting the data from the clipboard
    Last edited by westconn1; Mar 16th, 2010 at 02:59 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: Copy column of items in clipboard to an array

    I was going to use the following code:

    Code:
    Set MyData = New DataObject
    MyData.GetFromClipboard
    myStr = MyData.GetText(1)
    Do you think this will work? I will either be copying from notepad where all my data will be in a row by row entry into the cell, or from excel and in column format. Thanks for your help! =)

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copy column of items in clipboard to an array

    i believe it should work, but you will need to test
    data copied from excel should be a string separated by tabs for column and newline for row
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: Copy column of items in clipboard to an array

    testing right meow! Will let you know the results. Thank you!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: Copy column of items in clipboard to an array

    That seems to be working great! Let me know if i could improve the code i have so far:

    Code:
    Public Sub ec_es_validation()
        Dim MyData As DataObject
        Dim myStr As String
        Dim myStrArray As Variant
        Dim i As Long
        
        Set MyData = New DataObject
        MyData.GetFromClipboard
        myStr = MyData.GetText(1)
        On Error GoTo Error_Handling
        
        myStrArray = Split(myStr, vbNewLine)
        
        For i = LBound(myStrArray) To UBound(myStrArray)
            Debug.Print myStrArray(i)
            'do more within For loop
    
        Next i
    
    ExitHere:
        Exit Sub
    
    Error_Handling:
        MsgBox "The clipboard is empty!", vbCritical
        Resume ExitHere
    
    End Sub
    Eventually i want to be able to search a worksheet by the myStrArray(i), if found, get the column its in paste that into a new spreadsheet.sheet1 "A - myStrArray(i)" / if not found it pastes a row on a new spreadsheet.sheet2 in the next unused row. id like the script to be smart enough to know "if spreadsheet is open, paste row on correct sheet, if not open, open excel instance and then paste."

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Resolved [Resolved] Copy column of items in clipboard to an array

    I'll post further questions on my script in another post when needed. Thanks for your help westconn1

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