+ Reply to Thread
Results 1 to 10 of 10

Copy Selected Columns to Same Sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Copy Selected Columns to Same Sheet

    Hi All:

    I have a spreadsheet that has all the data I need but the column headings are not in the right order to transmit to the vendor. I am trying to copy and paste the columns I need to a range in the same sheet called "FORMAT". I started building this macro based on info from the net but I get a "Type Mismatch" error when I run it. I need the macro to find the column headers and then copy the data in that column to the new range. The code I have so far is below. Thanks for any help.

    Dim Cell        As Range
    Dim iCol        As Integer
    Set Cell = Sheets("Import").Range("IMPORT").Find("LNAME", "FNAME", "GENDER", "ENTITY") '/These are the column headers
    
        If Cell Is Nothing Then
            MsgBox "Unable to fin Header"
            Exit Sub
        
        Else
            iCol = Cell.Column
    End If
    
    With Sheets("Import")
        .Range(Cells(1, iCol).Cell(Rows.Count, iCol).End(xlUp)).CopytoRange = ("FORMAT")
        
    End With
       
    End Sub
    Last edited by egavasrg; 10-25-2011 at 04:06 PM. Reason: Issue solved

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Selected Columns to Same Sheet

    There is something wrong with the declaration statement -
    Set Cell = Sheets("Import").Range("IMPORT").Find("LNAME", "FNAME", "GENDER", "ENTITY") '/These are the column headers
    I am not sure you can do it this way.
    Can you attach a sample workbook with the details of what you need copied and to where? Maybe someone here can help you with an alternate code.

  3. #3
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Copy Selected Columns to Same Sheet

    Hi Arlu1201. I think I goofed on the tagging and my post with the attachment was deleted by an administrator. I'm really new here so I'm still learning all the nuances. Here is the file with the sample data. Any help is appreciated. I want to post the columns I listed starting at O5 in this same sheet. Thanks.

    rgerard
    Attached Files Attached Files
    Last edited by egavasrg; 10-25-2011 at 03:37 AM. Reason: additional information

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Selected Columns to Same Sheet

    Please provide the details on what data should be copied to which headers.

  5. #5
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Copy Selected Columns to Same Sheet

    Hi Arlu1201. I posted the spreadsheet so you can see the order of the columns as they come in. I need to reset them into the following column order: LNAME, FNAME, YEAR (XXXX), SSN, ENTITY. I want to post all data from those original columns into new columns with the same headings in range O5:S5 and Hope this helps. Thanks.
    Last edited by egavasrg; 10-25-2011 at 11:43 AM. Reason: Additional Information.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Selected Columns to Same Sheet

    I am sure they will be better ways of doing this (considering all the experts we have here) but this is one of them:
    Sub arrange_cols()
    
    Rows("5:5").Select
    Cells.Find(What:="LNAME", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
    cellcol = ActiveCell.Column
    
    Range(Cells(5, cellcol), Cells(5, cellcol).End(xlDown)).Copy
    Range("O5").Select
    ActiveSheet.Paste
    
    
    Rows("5:5").Select
    Cells.Find(What:="FNAME", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
    cellcol = ActiveCell.Column
    
    Range(Cells(5, cellcol), Cells(5, cellcol).End(xlDown)).Copy
    Range("P5").Select
    ActiveSheet.Paste
    
    
    Rows("5:5").Select
    Cells.Find(What:="YEAR  (####)", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
    cellcol = ActiveCell.Column
    
    Range(Cells(5, cellcol), Cells(5, cellcol).End(xlDown)).Copy
    Range("Q5").Select
    ActiveSheet.Paste
    
    
    Rows("5:5").Select
    Cells.Find(What:="SSN", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
    cellcol = ActiveCell.Column
    
    Range(Cells(5, cellcol), Cells(5, cellcol).End(xlDown)).Copy
    Range("R5").Select
    ActiveSheet.Paste
    
    Rows("5:5").Select
    Cells.Find(What:="ENTITY", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
    cellcol = ActiveCell.Column
    
    Range(Cells(5, cellcol), Cells(5, cellcol).End(xlDown)).Copy
    Range("S5").Select
    ActiveSheet.Paste
    
    Columns("O:S").AutoFit
    
    End Sub

  7. #7
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Copy Selected Columns to Same Sheet

    Hi Arlu1201. I'll try this out. Just from reading it, it looks like it will work. I like the FIND function. It's one of the few parts of VBA that I understand. Like you said, there may be a more elegant way to do this, but your code will get it done I think. I'll get back to you as soon as I run it. By the way, do you ever sleep :-).

  8. #8
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Copy Selected Columns to Same Sheet

    Hi Arlu.

    I just dimensioned cellcol as Integer and the program worked!!! Thank you so very much for you help. I really appreciate all the effort you put into this. Thank you.

    rgerard

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Selected Columns to Same Sheet

    I do sleep when you are awake, since you are in US and i am in India.

    I guess you have "Option Explicit" mentioned at the top of your code, which makes it mandatory for all variables to be defined. Its a good way of coding, however i had to do something quick so didnt define the variables.

    Incase cellcol gives you an error (since you have defined it as integer) you can then change it to Long. (Dim cellcol as long).

  10. #10
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Copy Selected Columns to Same Sheet

    Hi Arlu. I inserted the code into a module and ran it. I got an error. When I step through the code with F8, I get the following error when it reaches the first line that reads "cellcol = ActiveCell.Column".

    Compile error:
    Variable not defined

    I tried to dimension the variable cellcol as range = to ActiveCell.Column, but that didn't work. I think I mentioned that I'm very new to this so I'm not sure what to do next. Any help you can provide is appreciated. Thanks.

    rgerart

+ 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