+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Save VBA Variable to Clipboard

  1. #1
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Save VBA Variable to Clipboard

    Hello All.. I'm trying to save a variable to the clipboard so that I can paste it to another area.

    Background is that there's an online game I play that you can sell in game items on a forum. The forum can support certain tags to set up a table but trying to code a table for it pretty much makes me go crosseyed or want to drink a fifth of whiskey. So I'v generated some VBA to take the table I make in Excel and turn it into the code for the forum. I've learned after creating this code, that I cannot copy from the msgbox when it displays the code. I have tried various solutions I've found when googling but none of them have worked.

    End result shoudl be that it saves the variable Msgcode to the clipboard so I can just jump over to the forum and paste it.... Please let me know what I'm missing.


    Thanks in advance.

    Vamosj



    Sub Createcode()
    
    Range("A1").Select
    i = 0
    C = 0
    
    Do
    If ActiveCell.Offset(0, i).Value = "" Then GoTo A
    i = i + 1
    Loop
    
    A:
    Do
    If ActiveCell.Offset(C, 0).Value = "" Then GoTo B
    C = C + 1
    Loop
    
    
    B:
    i = i - 1
    C = C - 1
    Ti = 0
     
    CD = ""
    MsgCode = CD
    
    MsgBox ("Your code has been generated.  Just go to where you want to post it and select Paste.")
    
    End Sub
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010
    Posts
    856

    Re: Save VBA Variable to Clipboard

    Added a couple of lines to your code:
    Sub Createcode()
    Dim DataObj As New MSForms.DataObject
    
    Range("A1").Select
    i = 0
    C = 0
    
    Do
    If ActiveCell.Offset(0, i).Value = "" Then GoTo A
    i = i + 1
    Loop
    
    A:
    Do
    If ActiveCell.Offset(C, 0).Value = "" Then GoTo B
    C = C + 1
    Loop
    
    
    B:
    i = i - 1
    C = C - 1
    Ti = 0
     
    CD = ""
    MsgCode = CD
    
    MsgBox ("Your code has been generated.  Just go to where you want to post it and select Paste.")
    DataObj.SetText CD
    DataObj.PutInClipBoard
    End Sub
    If your question has been satisfactorily addressed, please consider marking it solved. Click here to see how.

    Also, you might want to add to the user's reputation by clicking the scales icon - it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hello vamosj,

    An easier way than using the DataObject is to copy MsgCode to a cell on the worksheet. You can then easily copy it to the clipboard. The changes are in bold text.

    Sub Createcode()
    
    Range("A1").Select
    i = 0
    C = 0
    
    Do
    If ActiveCell.Offset(0, i).Value = "" Then GoTo A
    i = i + 1
    Loop
    
    A:
    Do
    If ActiveCell.Offset(C, 0).Value = "" Then GoTo B
    C = C + 1
    Loop
    
    
    B:
    i = i - 1
    C = C - 1
    Ti = 0
     
    CD = ""
    
    ' Change the cell to one that the user will not be using
    Range("AA1") = CD
    Range("AA1").Copy
    
    MsgCode = CD
    
    MsgBox ("Your code has been generated.  Just go to where you want to post it and select Paste.")
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    Thank you both for your reply


    Quote Originally Posted by tlafferty View Post
    Added a couple of lines to your code:[code]
    This is the one I've found using Google but for some reason doesn't work. I get a Compile error: User-Defined type not defined whenever I try and Dim the Dataobject.


    Leith - I was thinking about trying it but didn't figure it would work with my chr(13) in the coding but decided to give it a shot... It works. Thanks for your input.
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hello vamosj,

    Glad to help a fellow squid.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    Back again with oe more quick ? about this. When I go to copy this I'm getting the " at the beginning and end of the code so when I paste it, the " is showing up when I don't want it to. Is there any way to remove this?
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hello Janos,

    Do you mean you when you paste it on the Excel worksheet or in Notepad?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    When I paste it into notepad
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hello Janos,

    I can't reproduce the problem. Can you post an sample Notepad file?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    I'll just post it and wrap some code around it... This is a straight ctrl+v paste

    ""
    See how when I paste it there is the " at the beginning and another " at the end. I know this is normal with any text value but I would like to remove it as the quotation marks do show up in the forum when the table is placed. The user now has to go in, locate, and delete the quotation marks.
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hell Janos,

    Since I can not reproduce the problem using Ctrl+V, it is possible to remove the beginning and ending quotes within the file using VBA. To do so requires that the Notepad file first be saved. You can then use this macro to remove the quotes. You will need to change the file path and file name to what you are using.

    Sub RemoveQuotes()
    
      Dim Filename As String
      Dim Filepath As String
      Dim FSO As Object
      Dim Text As String
      Dim TextFile As Object
    
    
          Filename = "Table Code.txt"
          Filepath = "C:\Documents and Settings\Janos\My Documents"
    
              Set FSO = CreateObject("Scripting.FileSystemObject")
    
              Set TextFile = FSO.OpenTextFile(Filepath & "\" & Filename, 1, False, 0)
                  Text = TextFile.ReadAll
              TextFile.Close
    
              Set TextFile = FSO.OpenTextFile(Filepath & "\" & Filename, 2, False, 0)
                  Text = Mid(Text, 2, Len(Text) - 2)
                  TextFile.Write Text
              TextFile.Close
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #12
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Save VBA Variable to Clipboard

    Probably this code performs the same job:

    Sub snb()
     sn=sheets(1).cells(1).currentregion
     for j=1 to ubound(sn)
      c01=c01 & join(application.index(sn,j),"|") & vbcrLf
     next
     
     open "E:\files.txt" for output as 1
      print 1, c01 
     close
    End Sub
    Last edited by snb; 09-23-2011 at 04:08 AM.



  13. #13
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    Not looking to save or open a notepad file. What I'm trying to do is the individual using this starts the macro, the macro takes the table into excel, and converts it to a string of code that can be pasted into a forum. Let me go ahead and upload my file..

    I tried pasting it to notepad and for some reason it doesn't have the " show up on it, but when I post it to the forum it does.

    To run the macro, go to the table tab and hit ctrl+m
    Attached Files Attached Files
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Save VBA Variable to Clipboard

    Hello Janos,

    Okay, I see what you mean. The quotes being added by the forum are most likely due to HTML code tags that included in the text. Placing the code inside quotes prevents the HTML code from being executed. Nothing you can due about that.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  15. #15
    Registered User
    Join Date
    04-23-2004
    Location
    Washington State
    MS-Off Ver
    Whatever the government has on my computer
    Posts
    43

    Re: Save VBA Variable to Clipboard

    Here is an image of what is happenning if I post it as is... You can see the " in the upper left hand corner. There is another after the table also..


    http://imgur.com/tV8gu
    Janos S. Vamos FC1 (X-DS)
    US Navy
    Xerox Tech
    Advance Combat Direction System Tech
    DD973 Computer/Peripheral Tech
    Snap II Tech
    MWR Leading Petty Officer
    Corrections Officer
    A man with many hats




    There are only 10 kind of people in this world. Those that understand Binary and those that don't...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0