+ Reply to Thread
Results 1 to 3 of 3

VBA Loops

Hybrid View

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

    VBA Loops

    Hi

    The code below is a loop VBA macro i have which loops through a foder and finds all teh files which are in a list and then opens them and copies the data in Column E and pastes it into the next clean column.

    However there is an if statement attachted to the loop which states that if there is a file which is not on the list to skip that fiel and go to the next one on the list. But i need to change this so it says if the file is not the list to skip the file but also leave a blank column and paste the data from the next file in the column next to the blank column.

    so it looks like this:
    d a Blank a a

      Sub Macro2()
    
    Dim StrFldr As String
    Dim ExtractCSV As Workbook
    Dim ExtractCSVSheet As Worksheet
    Dim lngWriteCol As Long
    Dim Template As Workbook
    Dim TemplateExtract As Worksheet
    Dim LastRow As Long
    Dim FromRow As Long
    Dim FromFileName As String
    Dim ToRow As Long
    Dim TemplateList As Worksheet
    
    'Application.DisplayAlerts = False
    'Application.ScreenUpdating = False
    
    Set Template = Application.Workbooks.Open("C:\Documents and Settings\SeymourJ\Desktop\Tasks\HondaExtractMacro\DealerData_Extract_Feed_Template.xls")
    Set TemplateExtract = Template.Sheets("ExtractData")
    Set TemplateList = Template.Sheets("Sheet1")
    
    StrFldr = "C:\Documents and Settings\SeymourJ\Desktop\Test1\"
    
    LastRow = TemplateList.Cells(Rows.Count, "C").End(xlUp).Row
    
    lngWriteCol = 2
    
    For FromRow = 1 To LastRow
        FromFileName = StrFldr & TemplateList.Cells(FromRow, "C").Value
        
        If Len(Dir(FromFileName)) > 0 Then
        Set ExtractCSV = Workbooks.Open(FromFileName)
        Set ExtractCSVSheet = ExtractCSV.Worksheets(1)
    
        ExtractCSVSheet.Range("E2:E2000").Copy Destination:=TemplateExtract.Cells(3, lngWriteCol)
        
        ExtractCSV.Close
        
        lngWriteCol = lngWriteCol + 1
        End If
    Next
    
    End Sub
    Does anyone know how to do this?

    Thanks

    Jeskit
    Last edited by jeskit; 02-04-2011 at 06:58 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: VBA Loops

    You just need to increment the write column whether the file exists or not:

    For FromRow = 1 To LastRow
        
        FromFileName = StrFldr & TemplateList.Cells(FromRow, "C").Value
        
        If Len(Dir(FromFileName)) > 0 Then
        
            Set ExtractCSV = Workbooks.Open(FromFileName)
            Set ExtractCSVSheet = ExtractCSV.Worksheets(1)
            ExtractCSVSheet.Range("E2:E2000").Copy Destination:=TemplateExtract.Cells(3, lngWriteCol)
            ExtractCSV.Close
        
        End If
    
        lngWriteCol = lngWriteCol + 1
    
    Next

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

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

    Re: VBA Loops

    hI

    Thanks it works now!!!

+ 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