Results 1 to 1 of 1

VBA Loops

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    VBA Loops

    Hi

    I have this piece of code which loops through one folder and opens each csv file within it and performs a counta and pastes the results into a sepeate document which then is saved and closed.
     Sub Get_Average_Count()
    
    'Define Variables
        'Const strFldr As String = "Path2"
        Dim strTemplate As String
        Dim strFldr As String
        Dim strFile As String
        Dim wbExtractSize As Workbook
        Dim wbCsv As Workbook
        Dim wsDealerExtracts As Worksheet
        Dim wsMyCsvSheet As Worksheet
        Dim lNextRow As Long
    
    'set strFldr variables
        strFldr = “c\:Production2\ATX\Outputs\"
    
    'set strFile variables
        strFile = Dir(strFldr & "\*.csv")
    
        strTemplate = "Extract_Size_Checker_template.xls"
    
    'set the calculation mode
        Application.Calculation = xlCalculationManual
    
    'set the workbook and worksheet
        Set wbExtractSize = Workbooks.Open(“c\:Production2\ATX\Outputs\" & strTemplate)
        Set wsDealerExtracts = wbExtractSize.Sheets("Dealer Extracts") 
    
    'find the next row available in ExtractSize, add two to
        lNextRow = 18
       
    'Loop through the csv files
        If Len(strFile) > 0 Then
            Do
                Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile)
                Set wsMyCsvSheet = wbCsv.Sheets(1)
                With wsDealerExtracts
                    .Cells(lNextRow, 6) = strFldr
                    .Cells(lNextRow, 7) = strFile
                    .Cells(lNextRow, 8) = WorksheetFunction.CountA(wsMyCsvSheet.Range("A:A"))
                End With
            
            'increment to the next row
                lNextRow = lNextRow + 1
            
            'close it
                wbCsv.Close
            
            'go to next file
                strFile = Dir
                Application.StatusBar = strFile
            Loop Until Len(strFile) = 0
        End If
    
        ActiveWorkbook.ActiveSheet.Range("A1").Select
        
    'clean up
        Set wbExtractSize = Nothing
        Set wbCsv = Nothing
        Set wsDealerExtracts = Nothing
        Set wsMyCsvSheet = Nothing
    
    End Sub
    However i now must change this piece of code so it opens a folder and performs a count on all the files within each of sub folders that are in this folder. However i am stumped and i have no idea how to do thsi. Can anyone help me???


    Thank you

    Jeskit
    Last edited by jeskit; 01-27-2011 at 06:19 AM.

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