+ Reply to Thread
Results 1 to 7 of 7

Search column "header" an print the entire column in another worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Question Search column "header" an print the entire column in another worksheet

    Hi

    I have some logs with alot of columns (range A5:CQ:5), I need to read this range, and if it have some value print the entire column on another worksheet.
    e.g.


    File |	Shot | Line Name |Point Number | etc until CQ:5
    1        20          1             50
    Let's say I only need the columns "File" and "Point Number" to be printed on another worksheet named "Result", my idea was something like this

    Sub SelectColumns()
    
    Dim Column As Range
    
    Sheets.Add.Name = "Result"
    
    For Each Column In Range("A5:CQ5")
        If Column.Value = "File" Or Column.Value = "Point Number" Then
        Sheets.Select ("Result")
        # print columns on new worksheet
        End If
        
    Next
    
    
    End Sub

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Search column "header" an print the entire column in another worksheet

    Try this

    Sub SelectColumns()
    Dim Column As Range
    Dim SourceSheet As Worksheet
    Set SourceSheet = ActiveSheet
    Sheets.Add.Name = "Result"
    SourceSheet.Activate
    For Each Column In Range("A5:CQ5")
        If Column.Value = "File" Or Column.Value = "Point Number" Then
            Column.EntireColumn.Copy Destination:=Sheets("Select").Cells(1, Columns.Count).End(xlToLeft).Offset(1, 0)
        End If
    Next
    End Sub

  3. #3
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Search column "header" an print the entire column in another worksheet

    Thanks for the reply.
    If I run this code I get runtime error 9.
    Dont know if its right but I changed "Destination:=Sheets("Select")" to "Destination:=Sheets("Result")" and now I get runtime error 1004

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Search column "header" an print the entire column in another worksheet

    You made a correct change - sorry for my mistake. When you get the error, which line is highlighted in yellow when you hit the debug button?

  5. #5
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Search column "header" an print the entire column in another worksheet

    The line is
    Column.EntireColumn.Copy Destination:=Sheets("Result").Cells(1, Columns.Count).End(xlToLeft).Offset(1, 0)
    Last edited by CassioGodinho; 04-22-2012 at 12:19 PM.

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Search column "header" an print the entire column in another worksheet

    OK - I can see the problem.

    Please replace with

    Column.EntireColumn.Copy Destination:=Sheets("Result").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)

  7. #7
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Search column "header" an print the entire column in another worksheet

    Thanks man, I just made it work using a less elegant aproach.
    Dim Count As Integer
    Count = 1
    For each
            Column.EntireColumn.Copy Destination:=Sheets("Result").Cells(1, Count)
            Count = Count + 1
    next
    Thats ugly code but aparently worked haha

+ 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