+ Reply to Thread
Results 1 to 23 of 23

Optimize code

Hybrid View

DarkKnightLupo Optimize code 02-05-2014, 05:49 AM
teh.format Re: Optimize code 02-05-2014, 06:18 AM
Izandol Re: Optimize code 02-05-2014, 06:22 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 06:27 AM
Izandol Re: Optimize code 02-05-2014, 06:40 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 07:24 AM
Izandol Re: Optimize code 02-05-2014, 07:48 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 08:34 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 08:31 AM
Izandol Re: Optimize code 02-05-2014, 08:52 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 10:14 AM
Izandol Re: Optimize code 02-05-2014, 10:37 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 10:49 AM
Izandol Re: Optimize code 02-05-2014, 11:24 AM
DarkKnightLupo Re: Optimize code 02-05-2014, 12:09 PM
Izandol Re: Optimize code 02-05-2014, 12:11 PM
DarkKnightLupo Re: Optimize code 02-05-2014, 12:17 PM
Izandol Re: Optimize code 02-05-2014, 12:24 PM
DarkKnightLupo Re: Optimize code 02-18-2014, 06:21 AM
Izandol Re: Optimize code 02-18-2014, 12:25 PM
DarkKnightLupo Re: Optimize code 02-19-2014, 05:28 AM
Izandol Re: Optimize code 02-19-2014, 05:43 AM
DarkKnightLupo Re: Optimize code 02-19-2014, 08:58 AM
  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.

  2. #2
    Registered User
    Join Date
    10-03-2010
    Location
    Poland
    MS-Off Ver
    2000 ; 2007
    Posts
    17

    Re: Optimize code

    If u see flickering on the screen I would add at beggining
    Application.ScreenUpdating = False
    and
    Application.ScreenUpdating = False
    at the end.

  3. #3
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    You may try:
    Sub ImportPCB()
    '
    ' ImportPCB Macro
    '
    
    '
       Const csPATH                    As String = "C:\Users\allaer81\Documents\Dimitri04Feb2014\"
       
       ' change this number as required
       Const NUMBER_OF_FILES           As Long = 11
       
       Dim wb                          As Workbook
       Dim n                           As Long
    
       Application.ScreenUpdating = False
    
       Set wb = Workbooks.Open(Filename:=csPATH & "PCBS01.CSV")
       wb.Sheets(1).Columns("A:B").Copy
       Workbooks("Dimitri04Feb2014Analysis.xlsx").ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                                                                           Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       Application.CutCopyMode = False
       wb.Close False
    
       For n = 2 To NUMBER_OF_FILES
    
          Set wb = Workbooks.Open(Filename:=csPATH & "PCBS" & Format(n, "00") & ".CSV")
          wb.Sheets(1).Columns("B:B").Copy Destination:=Workbooks("Dimitri04Feb2014Analysis.xlsx").ActiveSheet.Cells(1, n + 1)
          wb.Close False
       Next n
    
       Cells.NumberFormat = "0.00"
    
       Range("B2").Value = "PCBS01"
       Range("B2").AutoFill Destination:=Range("B2:L2"), Type:=xlFillDefault
       Cells(2, NUMBER_OF_FILES + 2).Resize(, 3).Value = Array("Average", "STDEV", "%")
       Cells(4, NUMBER_OF_FILES + 2).FormulaR1C1 = "=AVERAGE(RC[-11]:RC[-1])"
       Cells(4, NUMBER_OF_FILES + 3).FormulaR1C1 = "=STDEV(RC[-12]:RC[-2])"
       With Cells(4, NUMBER_OF_FILES + 4)
          .FormulaR1C1 = "=RC[-1]/RC[-2]"
          .NumberFormat = "0.0000%"
       End With
       Cells(4, NUMBER_OF_FILES + 2).Resize(, 3).AutoFill Destination:=Cells(4, NUMBER_OF_FILES + 2).Resize(200, 3)
    
    
       Cells.EntireColumn.AutoFit
       
       Application.ScreenUpdating = True
    
    End Sub
    Last edited by Izandol; 02-05-2014 at 07:46 AM. Reason: Correct mistakes!
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

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

    Re: Optimize code

    Already Big thanks Izandol!

    however when I add the code I get a err on:

    Set wb = Workbooks.Open(Filename:=csPATH & "PCBS01.CSV")

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    My mistake in file path:
    Const csPATH                    As String = "C:\Users\allaer81\Documents\Dimitri04Feb2014\"

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

    Re: Optimize code

    thanks Izandol, should have seen that one too, mybad.


    Workbooks("Dimitri04Feb2014Analysis.xlsx").Range("A1").PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Here I tried several things like this but still not working, "Subscript out of range" Is the problem

    Windows("Dimitri04Feb2014Analysis.xlsx").Column("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False



    edit: maybe because I should change the extension now by .xlsm ?
    Last edited by DarkKnightLupo; 02-05-2014 at 07:26 AM.

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    If file is saved as .xlsm then yes you must change this. If it is file with code we may use better syntax:
    Sub ImportPCB()
    '
    ' ImportPCB Macro
    '
    
    '
       Const csPATH                    As String = "C:\Users\allaer81\Documents\Dimitri04Feb2014\"
       
       ' change this number as required
       Const NUMBER_OF_FILES           As Long = 11
       
       Dim wb                          As Workbook
       Dim n                           As Long
    
       Application.ScreenUpdating = False
    
       Set wb = Workbooks.Open(Filename:=csPATH & "PCBS01.CSV")
       wb.Sheets(1).Columns("A:B").Copy
       ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                                                                           Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       Application.CutCopyMode = False
       wb.Close False
    
       For n = 2 To NUMBER_OF_FILES
    
          Set wb = Workbooks.Open(Filename:=csPATH & "PCBS" & Format(n, "00") & ".CSV")
          wb.Sheets(1).Columns("B:B").Copy Destination:=ThisWorkbook.ActiveSheet.Cells(1, n + 1)
          wb.Close False
       Next n
    
       Cells.NumberFormat = "0.00"
    
       Range("B2").Value = "PCBS01"
       Range("B2").AutoFill Destination:=Range("B2:L2"), Type:=xlFillDefault
       Cells(2, NUMBER_OF_FILES + 2).Resize(, 3).Value = Array("Average", "STDEV", "%")
       Cells(4, NUMBER_OF_FILES + 2).FormulaR1C1 = "=AVERAGE(RC[-11]:RC[-1])"
       Cells(4, NUMBER_OF_FILES + 3).FormulaR1C1 = "=STDEV(RC[-12]:RC[-2])"
       With Cells(4, NUMBER_OF_FILES + 4)
          .FormulaR1C1 = "=RC[-1]/RC[-2]"
          .NumberFormat = "0.0000%"
       End With
       Cells(4, NUMBER_OF_FILES + 2).Resize(, 3).AutoFill Destination:=Cells(4, NUMBER_OF_FILES + 2).Resize(200, 3)
    
    
       Cells.EntireColumn.AutoFit
       
       Application.ScreenUpdating = True
    
    End Sub

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

    Re: Optimize code

    Ok one nore esthetical question,

    Range("B2").Value = "PCBS01"
    Range("B2").AutoFill Destination:=Range("B2:L2"), Type:=xlFillDefault

    L2 is for 11 files, but can you adapt it to the number you modify, for example 27 zithout counting everytime the Range ?

    it is not that important!

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

    Re: Optimize code

    Ok Thanks a lot, it works great now!
    You are a Hero

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    Of course:
    Range("B2").AutoFill Destination:=Range("B2").Resize(, NUMBER_OF_FILES), Type:=xlFillDefault

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

    Re: Optimize code

    Hey, I got another interesting question I think,
    Imagine You have files with Names PCBS02, PCBS05, PCBS06, PCBS10 for example, so there is no regular connection and PCBS01, PCBS03, PCBS04, PCBS07 .. does not exist, is it possible to search for the files and putting them next to each other when I set n=10, because now I'll get a err when the file +1 is not there?

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    Do you know the names in advance, or will you only process all CSV files in the folder? Which file must have two columns copied?

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

    Re: Optimize code

    All the files exist out of three columns, The first one is for every file the same and the second one are the different results. ( so I need the first one, and all the second columns). I dont know the names in advance because they can change from experiment to experiment..., but also the total number of files changes but thats easy to look up.


    To better Explain, S01, S02 is the number of the measurement, So it is possible I measure 10 times in a row the same thing, for example PCB, but it ia also possible I measure first 1 Reference, 3 Pcb, 2 A en 2 B, that woul give me a data set with names REFS01, PCBS02, PCBS03, PCBS04, AS05, AS06, BS07, BS08, You understand what I mean:p

    Then I want in Excel a sheet with PCBS together, another sheet with REFs together, another sheet with As together ...

  14. #14
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    I think I understand. You may try:
    Sub ImportPCB()
    '
    ' ImportPCB Macro
    '
    
    '
       Const csPATH                    As String = "C:\Users\allaer81\Documents\Dimitri04Feb2014\"
    
       Dim wb                          As Workbook
       Dim lCounter                    As Long
       Dim sFile                       As String
    
       Application.ScreenUpdating = False
       sFile = Dir(csPATH & "PCB*.CSV")
       
       Do While sFile <> ""
       
          lCounter = lCounter + 1
          Set wb = Workbooks.Open(Filename:=csPATH & sFile)
          
          If lCounter = 1 Then
             wb.Sheets(1).Columns("A:B").Copy
             ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                                                               Operation:=xlNone, SkipBlanks:=False, Transpose:=False
             Application.CutCopyMode = False
          Else
             wb.Sheets(1).Columns("B:B").Copy Destination:=ThisWorkbook.ActiveSheet.Cells(1, lCounter + 1)
          End If
          
          wb.Close False
          sFile = Dir()
          
       Loop
    
       Cells.NumberFormat = "0.00"
    
       Range("B2").Value = "PCBS01"
       Range("B2").AutoFill Destination:=Range("B2:L2"), Type:=xlFillDefault
       Cells(2, lCounter + 2).Resize(, 3).Value = Array("Average", "STDEV", "%")
       Cells(4, lCounter + 2).FormulaR1C1 = "=AVERAGE(RC[-11]:RC[-1])"
       Cells(4, lCounter + 3).FormulaR1C1 = "=STDEV(RC[-12]:RC[-2])"
       With Cells(4, lCounter + 4)
          .FormulaR1C1 = "=RC[-1]/RC[-2]"
          .NumberFormat = "0.0000%"
       End With
       Cells(4, lCounter + 2).Resize(, 3).AutoFill Destination:=Cells(4, lCounter + 2).Resize(200, 3)
    
    
       Cells.EntireColumn.AutoFit
    
       Application.ScreenUpdating = True
    
    End Sub

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

    Re: Optimize code

    Ok this works very good, the only problem is that I get trouble calculating the average and the STDEV, originally I should have replaced -11 by -n, but now, -lCounter doesnt works...

  16. #16
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    You may use fixed column to start - I assume column 2:
       Cells(4, lCounter + 2).FormulaR1C1 = "=AVERAGE(RC2:RC[-1])"
       Cells(4, lCounter + 3).FormulaR1C1 = "=STDEV(RC2:RC[-2])"

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

    Re: Optimize code

    Ok thanks of course ,

    You are a real pro, You really helped me out man, I hope I was not too annoying with stupid questions, Big Thanks o Izandol!

  18. #18
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    You are welcome.

    No annoying at all - questions are good! This is how we all learn.

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

    Re: Optimize code

    Hi Izandol,

    I'm back again, the code worked pretty well untill I realised It loads in files in a random order instead of the 1->x order ( now it is 1, 12 ,14, 3 ,5 ,... for example)
    \. How can I force the sequence of the files I take my collumns of the way it should be so that first col comes from S01, 2nd from S02, etc...
    Thanks already!

    Here you can find the code:

    Sub ImportDRIFTPCB()
    '
    ' ImportDRIFTPCB Macro
    '
    
    '
       Const csPATH                    As String = "C:\Users\allaer81\Documents\DataToAnalyze\"
    
       Dim wb                          As Workbook
       Dim lCounter                    As Long
       Dim sFile                       As String
    
       Application.ScreenUpdating = False
       sFile = Dir(csPATH & "DRIFTPCB*.CSV")
       
       Do While sFile <> ""
       
          lCounter = lCounter + 1
          Set wb = Workbooks.Open(Filename:=csPATH & sFile)
          
          If lCounter = 1 Then
             wb.Sheets(1).Columns("A:B").Copy
             ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                                                               Operation:=xlNone, SkipBlanks:=False, Transpose:=False
             Application.CutCopyMode = False
          Else
             wb.Sheets(1).Columns("B:B").Copy Destination:=ThisWorkbook.ActiveSheet.Cells(1, lCounter + 1)
          End If
          
          wb.Close False
          sFile = Dir()
          
       Loop
    
       Cells.NumberFormat = "0.00"
    
       Range("B2").Value = "DRIFTPCBS01"
       Range("B2").AutoFill Destination:=Range("B2").Resize(, lCounter), Type:=xlFillDefault
       Cells(2, lCounter + 2).Resize(, 3).Value = Array("Average", "STDEV", "%")
       Cells(4, lCounter + 2).FormulaR1C1 = "=AVERAGE(RC2:RC[-1])"
       Cells(4, lCounter + 3).FormulaR1C1 = "=STDEV(RC2:RC[-2])"
       With Cells(4, lCounter + 4)
          .FormulaR1C1 = "=RC[-1]/RC[-2]"
          .NumberFormat = "0.0000%"
       End With
       Cells(4, lCounter + 2).Resize(, 3).AutoFill Destination:=Cells(4, lCounter + 2).Resize(200, 3)
    
    
       Cells.EntireColumn.AutoFit
    
       Application.ScreenUpdating = True
    
    End Sub

  20. #20
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    I have not tested:
    Sub ImportDRIFTPCB()
        '
        ' ImportDRIFTPCB Macro
        '
    
        '
        Const csPATH                   As String = "C:\Users\allaer81\Documents\DataToAnalyze\"
    
        Dim wb                         As Workbook
        Dim lCounter                   As Long
        Dim sFile                      As String
        Dim asFiles()                  As String
        Dim n                          As Long
    
        Application.ScreenUpdating = False
    
        sFile = Dir(csPATH & "DRIFTPCB*.CSV")
    
        Do While sFile <> ""
            ReDim Preserve asFiles(n)
            asFiles(n) = sFile
            sFile = Dir()
            n = n + 1
        Loop
    
        If n = 0 Then Exit Sub
    
        asFiles = BubbleSort(asFiles)
    
        For lCounter = LBound(asFiles) To UBound(asFiles)
            Set wb = Workbooks.Open(Filename:=csPATH & asFiles(lCounter))
    
            If lCounter = 0 Then
                wb.Sheets(1).Columns("A:B").Copy
                ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                                                                  Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Application.CutCopyMode = False
            Else
                wb.Sheets(1).Columns("B:B").Copy Destination:=ThisWorkbook.ActiveSheet.Cells(1, lCounter + 2)
            End If
    
            wb.Close False
        Next lCounter
    
    
        Cells.NumberFormat = "0.00"
    
        Range("B2").Value = "DRIFTPCBS01"
        Range("B2").AutoFill Destination:=Range("B2").Resize(, lCounter), Type:=xlFillDefault
        Cells(2, lCounter + 2).Resize(, 3).Value = Array("Average", "STDEV", "%")
        Cells(4, lCounter + 2).FormulaR1C1 = "=AVERAGE(RC2:RC[-1])"
        Cells(4, lCounter + 3).FormulaR1C1 = "=STDEV(RC2:RC[-2])"
        With Cells(4, lCounter + 4)
            .FormulaR1C1 = "=RC[-1]/RC[-2]"
            .NumberFormat = "0.0000%"
        End With
        Cells(4, lCounter + 2).Resize(, 3).AutoFill Destination:=Cells(4, lCounter + 2).Resize(200, 3)
    
    
        Cells.EntireColumn.AutoFit
    
        Application.ScreenUpdating = True
    
    End Sub
    Public Function BubbleSort(Strings) As String()
    
        Dim a                          As Long
        Dim e                          As Long
        Dim f                          As Long
        Dim g                          As Long
    
        Dim i                          As String
        Dim j                          As String
        Dim m()                        As String
        Dim n()                        As String
    
        e = 1
        n = Strings
        Do While e <> -1
    
            For a = 0 To UBound(Strings) - 1
                i = n(a)
                j = n(a + 1)
                f = StrComp(i, j)
                If f <= 0 Then
                    n(a) = i
                    n(a + 1) = j
                Else
                    n(a) = j
                    n(a + 1) = i
                    g = 1
                End If
            Next a
            If g = 1 Then
                e = 1
            Else
                e = -1
            End If
    
            g = 0
        Loop
        BubbleSort = n
    End Function

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

    Re: Optimize code

    Hi, no error but it still imports the files in a random order...


    For ease I renamed the files to PCBDRIFTS1 instead of PCBDRIFTS01 and so on..

    Thanks anyhow!

  22. #22
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Optimize code

    For ease I renamed the files to PCBDRIFTS1 instead of PCBDRIFTS01 and so on..
    This will break the sorting because in alpha sort PCBDRIFTS10 will come before PCBDRIFTS2. If you will name the file PCBDRIFTS01, PCBDRIFTS02 et cetera the sort will work.

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

    Re: Optimize code

    Thank You !! It seems to work fine now!

+ Reply to Thread

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