+ Reply to Thread
Results 1 to 11 of 11

Copy, Paste, Tidy macro help

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Red face Copy, Paste, Tidy macro help

    G'day.
    A while ago JBeaucaire provided me with code for a macro that copied data from one sheet, pasted it into another sheet, deleted empty and duplicate cells and sorted the results. I tweaked the code to get it to work in my work book and ended up with the following code:
    Option Explicit
    
    Sub MakeList()
    Dim LR As Long, LC As Long, i As Long
    LR = Range("AA4").SpecialCells(xlCellTypeLastCell).Row
    LC = Range("AA4").SpecialCells(xlCellTypeLastCell).Column
    Application.ScreenUpdating = False
    
    'Copy all data to second sheet in one column
        For i = 26 To LC
            Range(Cells(2, i), Cells(LR, i)).Copy _
                Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
        Next i
    
    'Clear out null cells and sort
        Sheets("Sheet1").Activate
        Range("A1") = "List"
        Range("B1") = "Key"
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("B2:B" & LR).FormulaR1C1 = "=OR(RC1="" "",RC1="""")"
        Range("A2").AutoFilter Field:=2, Criteria1:="TRUE"
        Range("A2:B" & LR).SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
        
    'Cleanup
        Rows("1:1").Delete
        Columns("B:B").ClearContents
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("A1:A" & LR).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
        Range("A1:A" & LR).RemoveDuplicates Columns:=1, Header:=xlNo
        Range("A1").Select
        Application.ScreenUpdating = True
    End Sub
    Can anyone tell me how to make the code paste the first result into cell A2 so I can have a title in cell A1. I'm sure it is very easy, I just don't know how to do it.

    Cheers
    Last edited by falcon5nz; 07-20-2009 at 08:47 AM. Reason: Solved

  2. #2
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Re: Copy, Paste, Tidy macro help

    Ideas anyone?

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Copy, Paste, Tidy macro help

    Im not the best with this stuff though it seams that you have a heading of "list" in cell A1

  4. #4
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Re: Copy, Paste, Tidy macro help

    That gets deleted in the clean up section

  5. #5
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Copy, Paste, Tidy macro help

    if you take Rows("1:1").Delete out does it leave the heading like you want.

  6. #6
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Re: Copy, Paste, Tidy macro help

    No. Ideally I want to have the headings:
    Head-stamp in A1
    Manufacturer in B1
    Country of Origin in C1
    With the list of headstamps in Column A etc
    I have attached a quick book with what I have (Sheet:Headstamp Table) and what I need (Headstamp ID)
    Attached Files Attached Files

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste, Tidy macro help

    Try this, it will clear the existing Headstamp ID sheet and reinsert a new, unduplicated, sorted set of values in column A. You'll have to add the column B and C stuff since none of that exists anywhere in the original data.

    Option Explicit
    
    Sub MakeList()
    Dim LR As Long, LC As Long, i As Long
    Application.ScreenUpdating = False
    
    Sheets("Headstamp ID").Range("A2:C" & Rows.Count).ClearContents
    Sheets("Headstamp Table").Activate
    LR = Range("AA4").SpecialCells(xlCellTypeLastCell).Row
    LC = Range("AA4").SpecialCells(xlCellTypeLastCell).Column
    
    'Copy all data second sheet
        For i = 1 To LC
            Range(Cells(2, i), Cells(LR, i)).Copy _
                Sheets("Headstamp ID").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        Next i
    
    'Clear out null cells and duplicates and sort
        Sheets("Headstamp ID").Activate
        Range("A1:A" & LR).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("B2:B" & LR).FormulaR1C1 = "=OR(RC1="" "",RC1="""",RC1=0,COUNTIF(R1C1:RC1,RC1)>1)"
        Range("A1").AutoFilter Field:=2, Criteria1:="TRUE"
        On Error Resume Next
        Range("A2:B" & LR).SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
        On Error GoTo 0
        
    'Cleanup
        Range("A1").AutoFilter
        Range("B2:B" & Rows.Count).ClearContents
        Range("A1").Select
        Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste, Tidy macro help

    Actually, now that I thought about it, I realized you might want to KEEP the existing Manufacturer and Country entries each time you rerun the sheet to create a new listing of Headstamps. Yes?

    If so, use this version. It will backup the existing Headstamp ID sheet, create a new one with new a new list of IDs and then fit the old B & C values back into the sheet from the backup sheet.

    Option Explicit
    
    Sub MakeList()
    Dim LR As Long, LC As Long, i As Long
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'Backup existing ID info
        Sheets("Headstamp ID").Copy After:=Sheets(Sheets.Count)
        Sheets("Headstamp ID (2)").Name = "Backup"
        Sheets("Headstamp ID").Range("A2:C" & Rows.Count).ClearContents
    
    'Copy all data to second sheet
        Sheets("Headstamp Table").Activate
        LR = Range("AA4").SpecialCells(xlCellTypeLastCell).Row
        LC = Range("AA4").SpecialCells(xlCellTypeLastCell).Column
    
        For i = 1 To LC
            Range(Cells(2, i), Cells(LR, i)).Copy _
                Sheets("Headstamp ID").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        Next i
    
    'Find null cells and duplicates, delete them
        Sheets("Headstamp ID").Activate
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("B2:B" & LR).FormulaR1C1 = "=OR(RC1="" "",RC1="""",RC1=0,COUNTIF(R1C1:RC1,RC1)>1)"
        Range("A1").AutoFilter Field:=2, Criteria1:="TRUE"
        On Error Resume Next
        Range("A2:B" & LR).SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
        On Error GoTo 0
        
    'Insert existing infor from backup
        Range("A1").AutoFilter
        LR = Range("A" & Rows.Count).End(xlUp).Row
        With Range("B2:C" & LR)
            .FormulaR1C1 = "=IF(ISERROR(INDEX(Backup!C,MATCH(RC1,Backup!C1,0))),"""",IF(INDEX(Backup!C,MATCH(RC1,Backup!C1,0))=0,"""",INDEX(Backup!C,MATCH(RC1,Backup!C1,0))))"
            .Value = .Value
        End With
        
        With Range("A1:C" & LR)
            .HorizontalAlignment = xlCenter
            .Columns.AutoFit
            .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                DataOption1:=xlSortTextAsNumbers
            .Borders(xlEdgeRight).LineStyle = xlContinuous
            .Borders(xlEdgeRight).Weight = xlThick
            .Borders(xlInsideVertical).LineStyle = xlContinuous
            .Borders(xlInsideVertical).Weight = xlThick
        End With
        
    'Cleanup
        Range("A2").Select
        ActiveWindow.FreezePanes = True
        Sheets("Backup").Delete
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 07-18-2009 at 12:10 AM.

  9. #9
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Re: Copy, Paste, Tidy macro help

    Thank you JBeaucaire. That second code is EXACTLY what I want, BUT (theres always a but) it is picking up the info from column A. How do I change it so it only collects the cells from column B across?

    Cheers for all your help guys

    Nick

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste, Tidy macro help

    This line of code:
    For i = 1 To LC
    ...means start in column "1" and go to the last column (LC), so just change the 1 to a 2.

  11. #11
    Registered User
    Join Date
    01-27-2009
    Location
    NZ
    MS-Off Ver
    Excel 200a
    Posts
    14

    Re: Copy, Paste, Tidy macro help

    Brilliant. Guys you have no idea how thankful I am.

+ 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