+ Reply to Thread
Results 1 to 24 of 24

A command button that copies cells

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80

    A command button that copies cells

    Hello All,
    So what I need is the code for a command button to copy the info in specific cells, to a text box that I have created.

    Lets say the cells are in sheet1 and the text box is on sheet 2. The command button will be on sheet3

    Also I am copying multiple cells. so If the code can include an example with multiple cells it would be great

    Thanks All.
    Last edited by slmi1313; 01-14-2009 at 04:40 PM.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    As long as you want a line feed after each cell in the text box, this should work:

    Private Sub CommandButton1_Click()
        Dim Rng As Range, Cell As Range
        Dim Str As String
        
        Set Rng = Sheets("Sheet1").Range("A1:D4")
        
        For Each Cell In Rng
            Str = Str & Cell & Chr(10)
        Next Cell
        Str = Left(Str, Len(Str) - 1)
        
        With Sheets("Sheet2")
            .Activate
            .Shapes(1).Select
        End With
        Selection.Text = Str
    End Sub
    You will need to name the sheets and range as noted in green above. You may also need to change the index number of the shape (text box).


    HTH

    Jason

  3. #3
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    So this what I wrote as a code and I get a run time error 438 on the last line "Selection.Text = Str" the error says Object doesn't support this property or method.

    Sheet Calculator is where I want to get the cells copied
    Sheet Parrs is where the textbox is(which is called TextBox1) and where I want the info from cell E3, E4 and E5 copied to. Any other ideas?

    Private Sub CommandButton30_Click()
        Dim Rng As Range, Cell As Range
        Dim Str As String
        
        Set Rng = Sheets("Calculator").Range("E5:E3:E4")
        
        For Each Cell In Rng
            Str = Str & Cell & Chr(10)
        Next Cell
        Str = Left(Str, Len(Str) - 1)
        
        With Sheets("Parrs")
            .Activate
            .Shapes(1).Select
        End With
        
    End Sub

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    First, change:

    Range("E5:E3:E4")
    to:

    Range("E3:E5")
    Next, try changing:

    .Shapes(1).Select
    to:

    .Shapes("Text Box 1").Select
    HTH

    Jason

  5. #5
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    I still get the same error

  6. #6
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Any chance you can post up a copy of your workbook?

  7. #7
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    Im getting an error from the site when I try to upload it

  8. #8
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Try zipping it first, then post the zip file.

  9. #9
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    Here you go thanks a million
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Ah. The text box is from the Controls tool bar. Try this:

    Private Sub CommandButton30_Click()
        Dim Rng As Range, Cell As Range
        Dim Str As String
        
        Set Rng = Sheets("Calculator").Range("E3:E5")
        
        For Each Cell In Rng
            Str = Str & Cell & Chr(10)
        Next Cell
        Str = Left(Str, Len(Str) - 1)
        
        Sheets("Parrs").TextBox1 = Str
    End Sub
    HTH

    Jason

  11. #11
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    that works great, the only thing that happens is that when it copies the info in the text box, they are not in line, there pasted verticaly not horizontaly. I need it to paste it like a sentence . can you help with that?

  12. #12
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Change

    Str = Str & Cell & Chr(10)
    to

     Str = Str & Cell & ", "
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  13. #13
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    Awesome you guys rock

  14. #14
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    Oh actually what happens now is that if there is stuff written in the text box, it erases them, is there any way to fix that

  15. #15
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe change

    Sheets("Parrs").TextBox1 = Str
    to
    With Sheets("Parrs").TextBox1
          .Text = .Value & Str
    End With
    VBA Noob

  16. #16
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Try changing:

    Sheets("Parrs").TextBox1 = Str
    to:

    Sheets("Parrs").TextBox1
        .Value = .Value & Str
    End With
    Jason

  17. #17
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    So this is the code that works perfectly. If I would need to copy cells H3:H6 and I3:I6 how would I write that in. I tried "&", I tried .Range("I3:I6") and I always get an error. Any Ideas?

    Dim Rng As Range, Cell As Range
        Dim Str As String
        
        Set Rng = Sheets("Calculator").Range("H3:H6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
        Str = Left(Str, Len(Str) - 1)
        
        With Sheets("Parrs").TextBox1
          .Text = .Value & Str
    End With

  18. #18
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    If you changed:

    .Range("H3:H6")
    to:

    .Range("H3:I6")
    The text box will include H3, I3, H4, I4, etc. Does that work? If not, you can use:

        Set Rng = Sheets("Calculator").Range("H3:H6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
    
        Set Rng = Sheets("Calculator").Range("I3:I6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
        Str = Left(Str, Len(Str) - 2)
        
        With Sheets("Parrs").TextBox1
          .Text = .Value & Str
    HTH

    Jason

  19. #19
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    This code below gives me an error on the Line Str = Str & Cell & ", " Compile error argument not optional

    Set Rng = Sheets("Calculator").Range("H3:H6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
    
        Set Rng = Sheets("Calculator").Range("I3:I6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
        Str = Left(Str, Len(Str) - 2)
        
        With Sheets("Parrs").TextBox1
          .Text = .Value & Str
    Using this code is not good for me cause I need H3:H6 first then I3:I6
    .Range("H3:I6")
    Thanks for all the help I love this site

  20. #20
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Are any of I3:I6 merged cells? Any chance you can post an example copy of your workbook?

  21. #21
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    I already posted a copy

  22. #22
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Sorry for the confusion. I plugged the below code into your attached workbook, and it worked fine for me:

    Private Sub CommandButton30_Click()
        Dim Rng As Range, Cell As Range
        Dim Str As String
        
        Set Rng = Sheets("Calculator").Range("H3:H6")
        
        For Each Cell In Rng
            Str = Str & Cell & Chr(10)
        Next Cell
        
        Set Rng = Sheets("Calculator").Range("I3:I6")
        
        For Each Cell In Rng
            Str = Str & Cell & Chr(10)
        Next Cell
        
        Str = Left(Str, Len(Str) - 1)
        
        With Sheets("Parrs").TextBox1
            .Value = .Value & Str
        End With
    End Sub
    Jason

  23. #23
    Registered User
    Join Date
    09-13-2008
    Location
    Montreal
    MS-Off Ver
    Office XP
    Posts
    80
    ok I got this is the code thanks for all your help
    Dim Rng As Range, Cell As Range
        Dim Str As String
     
     Set Rng = Sheets("Calculator").Range("H3:H6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
    
        Set Rng = Sheets("Calculator").Range("I3:I6")
    
        For Each Cell In Rng
            Str = Str & Cell & ", "
        Next Cell
        Str = Left(Str, Len(Str) - 2)
        
        With Sheets("Parrs").TextBox1
          .Text = .Value & Str
    End With
    End Sub

  24. #24
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Glad you have it fixed. Please mark the thread as solved. Thanks.

    Jason

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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