Consulting

Results 1 to 7 of 7

Thread: Copy Paste Ranges - Listed in Column A

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Copy Paste Ranges - Listed in Column A

    Hi Folks,

    good monday

    I've got stuck on this copy paste problem

    I have put the ranges in column A that I want to Select

    Select (A) Paste to (B)
    A1:A10 ----- > D1
    B10 :B20 -------> Q1


    Column B Destination it should paste to




     Sub CopyPasteRanges()
     
     Dim owksht     As Worksheet
     Dim i      As Integer
     
     
      Set owksht = Worksheets("Sheet1")
      For i = 1 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
     
     
      ws.Cells(ws.Rows.Count, "A").Select
      Selection.Copy
        
      ws.Cells(ws.Rows.Count, "B").Paste
    
    next i 
        
     End Sub
    is this the right track?

    can a pro advise as im not sure ive set it up correctly
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Do you mean that on Sheet1 you have

    Cell A1 = "A1:A10"
    Cell B1 = " D1"

    Cell A2 = "B10 :B20"
    Cell B2 = "Q1"

    and you want to copy on THAT sheet?

    Or from another sheet (say Sheet2) what's in A1:A10 to D10?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hi Paul,

    I have a lot of ranges to copy and paste and well it gets confusing so i thought i would list all the ranges to be copied in column A
    as im forever selecting the worng cells to copy paste - so i got fed up and thought a macro can help

    Cells paste.PNG

    in column B where to paste to.

    Same sheet should be fine
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Something like this maybe

    Option Explicit
    Sub CopyByAddress()
        Dim rCopyList As Range, rCopy As Range
        
        Set rCopyList = ActiveSheet.Cells(1, 1).CurrentRegion
    
        For Each rCopy In rCopyList.Rows
            With rCopy
                If Len(.Cells(1).Value) > 1 And Len(.Cells(2).Value) > 1 Then
                    ActiveSheet.Range(.Cells(1).Value).Copy ActiveSheet.Range(.Cells(2).Value)
                End If
            End With
        Next
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hi Paul,


    In the interim I've been fiddling about with the .paste function, i thought i was on the right track with

    ActiveSheet.Range(ws.Cells("i, "A").Value).Copy
    ActiveSheet.Range(ws.Cells("i, "B").Value) .paste

    but then the debugger wouldnt move oh dear.


    thank you very much - your code does the job perfect awesome!

    I really appreciate it.

    The other problem was I overwrote some cells and that was really frustrating when i was manually copy and pasting,

    thanks to the vba gurus i can have a nice monday now

    nice one Paul

    Good monday folks
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Just to show you how to do it the way you started to
    Option Explicit
    
    Sub SamT_CopyPasteRanges()
        Dim i As Long
        Dim ws As Worksheet
         
        Set ws = Worksheets("Sheet1")
        
        For i = 1 To ws.Cells(Rows.Count, "A").End(xlUp).Row
            ws.Range(Cells(i, "A")).Copy Destination:=ws.Range(Cells(i, "B"))
        Next i
        
    End Sub
    Range(Cells(i, "A")) means "the range address contained in cells(i, "A")"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Sam,

    how did you know - you folks are psychic?

    - Im a bit like a dog on a bone, i was just as we speak fiddling with the exact same thing - bit puzzled by my first attempt -
    you beat me to it!

    I missed the destination keyword completely oh well

    I've been on the micorsoft vba reference and its like finding a needle in the haystack.


    You know sometimes when you try to select cells excel goes off like theres no tomorrow and selects al the wrong cells


    thanks for this new bit of code - so i have 2 now to make it into all sorts for my worksheet

    thanks my friends Paul and Sam for all the help

    good evening alll

    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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