Results 1 to 23 of 23

Optimize code

Threaded View

  1. #1
    Registered User
    Join Date
    02-05-2014
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    14

    Optimize code

    Hi all, I'm new here and I have a question and I hope you guys could help me out.
    In the code on the on the bottom of this message I'd like to optimize, for example, Somethimes I have 10 PCB-flies (PCBS01-PCBS10), somethimes I get more, for example (PCBS01-PCBS27), is there a way to easily adjust everything by using loops or something? Also every collumn I copy from one file must be placed on the adjacent collumn and at the end I want as you can see have an extra collumn with average, and also one with the stdev. If more info required or if this impossible, please let me know

    Thanks and greetings from Belgium!


    Sub ImportPCB()
    '
    ' ImportPCB Macro
    '
    
    '
        ChDir "C:\Users\allaer81\Documents\Dimitri04Feb2014"
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS01.CSV"
        Columns("A:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("A:A").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS01.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS02.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("C:C").Select
        ActiveSheet.Paste
        Windows("PCBS02.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS03.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("D:D").Select
        ActiveSheet.Paste
        Windows("PCBS03.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS04.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("E:E").Select
        ActiveSheet.Paste
        Windows("PCBS04.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS05.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("F:F").Select
        ActiveSheet.Paste
        Windows("PCBS05.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS06.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("G:G").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS06.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
            Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS07.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("H:H").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS07.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
            Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS08.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("I:I").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS08.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
            Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS09.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("J:J").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS09.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
            Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS10.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("K:K").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS10.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
            Workbooks.Open Filename:= _
            "C:\Users\allaer81\Documents\Dimitri04Feb2014\PCBS11.CSV"
        Columns("B:B").Select
        Selection.Copy
        Windows("Dimitri04Feb2014Analysis.xlsx").Activate
        Columns("L:L").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("PCBS11.CSV").Activate
        Application.CutCopyMode = False
        ActiveWindow.Close
        
        Cells.Select
        Selection.NumberFormat = "0.00"
        
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "PCBS01"
        Range("B2").Select
        Selection.AutoFill Destination:=Range("B2:L2"), Type:=xlFillDefault
        Range("B2:L2").Select
        Range("M2").Select
        ActiveCell.FormulaR1C1 = "Average"
        Range("N2").Select
        ActiveCell.FormulaR1C1 = "STDEV"
        Range("O2").Select
        ActiveCell.FormulaR1C1 = "%"
        Range("M4").Select
        ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-11]:RC[-1])"
        Range("N4").Select
        ActiveCell.FormulaR1C1 = "=STDEV(RC[-12]:RC[-2])"
        Range("O4").Select
        ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-2]"
        Range("O4").Select
        Selection.Style = "Percent"
        Selection.NumberFormat = "0.0%"
        Selection.NumberFormat = "0.00%"
        Selection.NumberFormat = "0.000%"
        Selection.NumberFormat = "0.0000%"
        Range("M4").Select
        Selection.AutoFill Destination:=Range("M4:M204")
        Range("M4:M204").Select
        Range("N4").Select
        Selection.AutoFill Destination:=Range("N4:N204")
        Range("N4:N204").Select
        Range("O4").Select
        Selection.AutoFill Destination:=Range("O4:O204")
        Range("O4:O204").Select
        
        
        Cells.Select
        Cells.EntireColumn.AutoFit
    End Sub
    Last edited by arlu1201; 02-05-2014 at 05:59 AM. Reason: Use code tags in future.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Optimize a slow code...
    By benoitly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2013, 01:44 PM
  2. Optimize code
    By miso.dca in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2011, 03:35 PM
  3. Optimize Alphabetizing VBA Code
    By NewExcelUser in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2010, 11:51 PM
  4. How can I optimize/simplify that code ?
    By Grek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2010, 03:38 PM
  5. Optimize VBA code
    By doodlebug in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2007, 07:53 AM

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