Consulting

Results 1 to 5 of 5

Thread: Ctrl-C selection in memory - how work with it by VBA Excel code?

  1. #1
    VBAX Regular
    Joined
    Jul 2017
    Posts
    6
    Location

    Ctrl-C selection in memory - how work with it by VBA Excel code?

    I have Form1 with TextBox1. I made TextBox1.Text selection - manually with mouse and Ctrl-C or by code

    TextBox1.SelStart = 0
    TextBox1.SelLength = TextBox1.TextLength
    TextBox1.Copy

    It means that value TextBox1.Text is copied in memory in some buffer.

    My questions:

    1. How can I copy this value from memory to some string type variable S? Note: Forgot the S = TextBox1.Text solution, my problem is to read the value from memory.

    2. How can I clean this selected value from memory by some code? It means Ctrl-V will return nothing (zero string).

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    To clear the Clip Board, most websites for VBA recommend using : Application.CutCopyMode=False placed as the last line in your macro code.

    Or you can utilize this function placed into a Routine Module:

    Option Explicit 
    Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long 
    Public Declare Function EmptyClipboard Lib "user32" () As Long 
    Public Declare Function CloseClipboard Lib "user32" () As Long 
     
    Public Function ClearClipboard() 
        OpenClipboard (0&) 
        EmptyClipboard 
        CloseClipboard 
    End Function 
     
    Sub ccc() 
        Call ClearClipboard 
    End Sub
    At the end of your macro code, to clear the clipboard, 'call' the above function with : Call ccc
    although it is not necessary to use the word Call - you can simply type ccc

    See the attached workbook for a demonstration.

    To copy/paste something from the textbox to another location in your workbook you can use a macro code something like this :

    Option Explicit
    
    
    Private Sub CommandButton1_Click()
     
        Sheets("Sheet1").Range("A1").Value = Me.TextBox1.Text
        Label1.Caption = Me.TextBox1.Text
        Label2.Visible = True
        Sheets("Sheet2").Range("A1:A10").Value = Me.TextBox1.Text
        Label3.Visible = True
        
        ccc
    End Sub


    Run the UserForm in the workbook and see.
    Attached Files Attached Files

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Juraj View Post
    How can I copy this value from memory to some string type variable S? Note: Forgot the S = TextBox1.Text solution, my problem is to read the value from memory.
    See http://wellsr.com/vba/2015/tutorials...d-paste-clear/ about 1/3rd down, section headed VBA Paste from Clipboard
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Regular
    Joined
    Jul 2017
    Posts
    6
    Location
    Thank you for answers both of you. That's it what I needed :-)

  5. #5
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    You are welcome.

Tags for this Thread

Posting Permissions

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