Consulting

Results 1 to 10 of 10

Thread: Solved: Select columns to copy

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    7
    Location

    Solved: Select columns to copy

    I am trying to copy all active columns and paste to another worksheet. When I do this without a macro, it works, but I cannot get the same result with the macro. The following macro was recorded. When I clicked on column B and hit end, right, it chose columns B-I which are all of the active columns and inserted them on the other sheet. However, when I tried running the macro, it only copied columns B-E. HELP PLEASE!

    [VBA]Sheets("Sheet1").Select
    Columns("B:B").Select
    Range("B2").Activate
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Columns("B:B").Select
    Range("B2").Activate
    Selection.Insert Shift:=xlToRight[/VBA]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    does
    [vba]Sheets("Sheet1").Range(Range("B2"), Range("B2").End(xlToRight)).EntireColumn.Copy
    Sheets("Sheet2").Select
    Columns("B:B").Select
    Range("B2").Activate
    Selection.Insert Shift:=xlToRight
    [/vba]work?
    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.

  3. #3
    VBAX Regular
    Joined
    Feb 2010
    Posts
    7
    Location
    It gives me "Application-defined or object-defined error"

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Bonnie,

    I see you just joined! Welcome to vbaexpress . You will be glad you joined, as there's some great folks here.

    As to your question, if there is nothing to the right of this block of data, maybe locate the last column with data, and set our range that way.

    In a junk copy (lest I goober up your wb), try:

    Option Explicit
        
    Sub exa()
    Dim rngLastCol As Range
        
        With ThisWorkbook
            With .Worksheets("Sheet1")
                
                Set rngLastCol = RangeFound(Range(.Range("B2"), .Cells(Rows.Count, Columns.Count)), , , , , xlByColumns)
                
                If rngLastCol Is Nothing Then Exit Sub
                
                Range(.Range("B2"), .Cells(Rows.Count, rngLastCol.Column)).Copy
            End With
            
            .Worksheets("Sheet2").Range("B2").Insert xlToRight
        End With
        
        Application.CutCopyMode = False
    End Sub
        
    Function RangeFound(SearchRange As Range, _
                        Optional FindWhat As String = "*", _
                        Optional StartingAfter As Range, _
                        Optional LookAtTextOrFormula As XlFindLookIn = xlValues, _
                        Optional LookAtWholeOrPart As XlLookAt = xlPart, _
                        Optional SearchRowCol As XlSearchOrder = xlByRows, _
                        Optional SearchUpDn As XlSearchDirection = xlPrevious, _
                        Optional bMatchCase As Boolean = False) As Range
        
        If StartingAfter Is Nothing Then
            Set StartingAfter = SearchRange(1)
        End If
        
        Set RangeFound = SearchRange.Find(What:=FindWhat, _
                                          After:=StartingAfter, _
                                          LookIn:=LookAtTextOrFormula, _
                                          LookAt:=LookAtWholeOrPart, _
                                          SearchOrder:=SearchRowCol, _
                                          SearchDirection:=SearchUpDn, _
                                          MatchCase:=False)
    End Function
    Hope that helps,

    Mark

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by BonnieG
    It gives me "Application-defined or object-defined error"
    Ah yes, that fails if sheet 1 isn't active. Try this:
    [vba]With Sheets("Sheet1")
    Range(.Range("B2"), .Range("B2").End(xlToRight)).EntireColumn.Copy
    End With
    Sheets("Sheet2").Select
    Columns("B:B").Select
    Range("B2").Activate
    Selection.Insert Shift:=xlToRight
    Application.CutCopyMode = False
    [/vba]
    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.

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    deleted (duplicate post)
    Last edited by p45cal; 02-18-2010 at 09:03 AM.
    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.

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    deleted (duplicate post)
    Last edited by p45cal; 02-18-2010 at 09:03 AM.
    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.

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    There are odd things going on with the posting system. Any, even shorter:

    [VBA]With Sheets("Sheet1")
    Range(.Range("B2"), .Range("B2").End(xlToRight)).EntireColumn.Copy
    Worksheets("Sheet2").Range("B1").Insert xlToRight
    End With
    Application.CutCopyMode = False[/VBA]
    Last edited by p45cal; 02-18-2010 at 09:04 AM.
    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.

  9. #9
    VBAX Regular
    Joined
    Feb 2010
    Posts
    7
    Location
    Thanks Mark. It looks like this is going to work.

  10. #10
    VBAX Regular
    Joined
    Feb 2010
    Posts
    7
    Location
    Thank you all. This is wonderful. I'm so glad that I joined.

Posting Permissions

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