+ Reply to Thread
Results 1 to 8 of 8

Macro to copy the contents of the cell, but not the cell itself?

Hybrid View

swolfe2 Macro to copy the contents of... 04-02-2013, 12:38 PM
111StepsAhead Re: Macro to copy the... 04-02-2013, 01:15 PM
swolfe2 Re: Macro to copy the... 04-02-2013, 01:37 PM
111StepsAhead Re: Macro to copy the... 04-02-2013, 01:44 PM
swolfe2 Re: Macro to copy the... 04-02-2013, 01:53 PM
111StepsAhead Re: Macro to copy the... 04-02-2013, 01:57 PM
swolfe2 Re: Macro to copy the... 04-02-2013, 02:02 PM
swolfe2 Re: Macro to copy the... 04-02-2013, 03:31 PM
  1. #1
    Registered User
    Join Date
    04-02-2013
    Location
    Knoxville, TN
    MS-Off Ver
    Office 365
    Posts
    15

    Question Macro to copy the contents of the cell, but not the cell itself?

    Hello all!

    I currently have a userform that places a large amount of data into a cell. I have tried using the code below to copy that text so the end user can paste it into our documentation program.

    Sub writeData()
    Dim lastRow As Integer
    With Sheets("Formatted Answers")
    lastRow = .Cells(.Rows.count, "A").End(xlUp).Row + 1
    'insert date cell
    .Range("A" & lastRow).Value = Format(Now, "MM/DD/YY hh:mm:ss AM/PM")
    'insert type:
    .Range("B" & lastRow).Value = .Range("B3") & "-" & .Range("B4")
    'Insert date
    .Range("C" & lastRow).Value = formattedAnswers
    'Sort by dates:
    .Range("A6:C" & lastRow).Sort Key1:=.Range("A6:A" & lastRow), order1:=xlDescending, Header:=xlNo
    .Range("A6:C" & lastRow).Rows.AutoFit
    End With
    Range("C6").Select
    Selection.Copy
    End Sub
    However, because the code selects the entire cell, it applies quotation marks around the entire body once it is pasted into our program. This is causing errors in our reporting, and they need to be removed. If the user double clicks inside the cell, and manually selects the text, it doesn't apply the quotes.

    Is there a way to have the macro automatically do this? All I really need is to copy the text in cell C6, since that's where the new data is placed each time.

    Thanks for the help, as always!

  2. #2
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Macro to copy the contents of the cell, but not the cell itself?

    The code

    Range("C6").Select
    Selection.Copy
    works fine with notepad and microsoft word on my machine.

    Maybe try the code

    Range("C6").Copy
    Instead

  3. #3
    Registered User
    Join Date
    04-02-2013
    Location
    Knoxville, TN
    MS-Off Ver
    Office 365
    Posts
    15

    Re: Macro to copy the contents of the cell, but not the cell itself?

    Hello 111!

    Thanks for the quick reply!

    I tried the change you suggested, but I got the same result. Like you, I tried pasting into a multitude of programs. However, SAP is the only one that has the quotes apply when the cell is copied.

    What I'm looking for, I guess, is a macro that will select the text inside the cell without selecting the cell box.

    Thanks!

  4. #4
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Macro to copy the contents of the cell, but not the cell itself?

    swolfe2,

    Are you able to copy-paste information from notepad, or some different program besides excel, to your program without the quotes? I'd like to rule out that your program is causing the issue.

  5. #5
    Registered User
    Join Date
    04-02-2013
    Location
    Knoxville, TN
    MS-Off Ver
    Office 365
    Posts
    15

    Re: Macro to copy the contents of the cell, but not the cell itself?

    Yes. I can paste from Word, websites, and any information outside of SAP. I can even paste from Excel, if I double click inside a cell and manually highlight items.
    Last edited by swolfe2; 04-02-2013 at 01:56 PM.

  6. #6
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Macro to copy the contents of the cell, but not the cell itself?

    swolfe2,

    I must go now but I believe the following will work for you.

    Sub myTest()
        Range("C6").Select
        Selection.Copy
        Shell "notepad.exe", vbNormalFocus
        SendKeys "^v", True
        SendKeys "^a", True
        SendKeys "^c", True
        Application.SendKeys "%{F4}"
        Application.SendKeys "{TAB}"
        Application.SendKeys "{ENTER}"
    End Sub
    It copies the value in "C6", pastes it to an instance of notepad, copies the text from notepad, and closes notepad. The results is your value is on the "clipboard" for use. Anyways, good luck!

  7. #7
    Registered User
    Join Date
    04-02-2013
    Location
    Knoxville, TN
    MS-Off Ver
    Office 365
    Posts
    15

    Re: Macro to copy the contents of the cell, but not the cell itself?

    111,

    Thanks for the help in getting started.
    The code that you gave me is still applying the quotes around it, and pasting it into Notepad.


  8. #8
    Registered User
    Join Date
    04-02-2013
    Location
    Knoxville, TN
    MS-Off Ver
    Office 365
    Posts
    15

    Re: Macro to copy the contents of the cell, but not the cell itself?

    So, I figured it out!

    Sub writeData()
    Dim lastRow As Integer
    Dim oData As DataObject
    Set oData = New DataObject
    Dim sClipBoard As String
    
    With Sheets("Formatted Answers")
    lastRow = .Cells(.Rows.count, "A").End(xlUp).Row + 1
    'insert date cell
    .Range("A" & lastRow).Value = Format(Now, "MM/DD/YY hh:mm:ss AM/PM")
    'insert type:
    .Range("B" & lastRow).Value = .Range("B3") & "-" & .Range("B4")
    'Insert date
    .Range("C" & lastRow).Value = formattedAnswers
    'Sort by dates:
    .Range("A6:C" & lastRow).Sort Key1:=.Range("A6:A" & lastRow), order1:=xlDescending, Header:=xlNo
    .Range("A6:C" & lastRow).Rows.AutoFit
    End With
    ' Clears the clipboard
    oData.SetText ""
    oData.PutInClipboard
    
    ' Puts the text from a cell into the clipboard
    sClipBoard = Worksheets("Formatted Answers").Range("C6").Text
    oData.SetText sClipBoard
    
    
    End Sub
    Hopefully, that can help someone in the future that had the same problem as I did.

    Thanks!

+ 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.6.0 RC 1