+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    02-12-2010
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2003
    Posts
    5

    Copying Multiple Cells in VB

    Hi

    The basic on what i want to be able to do if possible is:

    If 2 or more cells are selected (below one another) and i press run, then Copy the info in the top cell to 1 sheet, and copy the info in the 2nd cell, into a 2nd sheet.

    I also need to make sure that if only 1 cell is selected, that it only copies the info from that single cell.

    Is this possible? Any advise would be great

    Cheers

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    Location
    Leeds, UK
    MS-Off Ver
    2003 (work), 2007 & 2010 (home)
    Posts
    3,517

    Re: Copying Multiple Cells in VB

    Maybe:

    Code:
    Sub test()
    
    Dim myRange As Range, myCount As Integer
    
    Select Case Selection.Cells.Count
    
    Case 1
    
        Selection.Copy Sheets("Sheet2").Range("A1")
    
    Case 2
    
        For Each myRange In Selection
        
            For myCount = 2 To 3
        
            myRange.Copy Sheets("Sheet" & myCount).Range("A1")
            
            Next myCount
            
        Next myRange
    
    Case Is > 2
    
        Exit Sub
    
    End Select
    
    End Sub

    If 1 cell is selected then the cell will just be copied to Sheet2, if 2 then they will be copied to Sheet2 and Sheet3.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    If you haven't already please take some time to read the Forum Rules.

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    02-12-2010
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying Multiple Cells in VB

    Thanks for that Dom, but its not quite what i was after.

    Its still better than anything i've come up with so far....

    At the minute, If 2 cells selected it copies the same the same value to sheet 2 and sheet 3.
    Ideally i need it to copy the 1st cell value into sheet 2 and the 2nd cell value into sheet 3

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    Location
    Leeds, UK
    MS-Off Ver
    2003 (work), 2007 & 2010 (home)
    Posts
    3,517

    Re: Copying Multiple Cells in VB

    Aye, it wasn't quite right. Try:

    Code:
    Sub test()
    
    Dim myRange As Range, myCount As Integer
    
    Select Case Selection.Cells.Count
    
    Case 1
    
        Selection.Copy Sheets("Sheet2").Range("A1")
    
    Case 2
    
        myCount = 2
    
        For Each myRange In Selection
        
            myRange.Copy Sheets("Sheet" & myCount).Range("A1")
            
            myCount = myCount + 1
            
        Next myRange
    
    Case Is > 2
    
        Exit Sub
    
    End Select
    
    End Sub
    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    If you haven't already please take some time to read the Forum Rules.

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    02-12-2010
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying Multiple Cells in VB

    Thats spot on!

    Cheers for your help Dom

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