Results 1 to 9 of 9

Thread: [RESOLVED] Excel 2003 - Get certain column from all XLS in a directory

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [RESOLVED] Excel 2003 - Get certain column from all XLS in a directory

    Hi all,

    Excuse my completely newbie question but I have virtually no experience with programming Excel. What I'm trying to do is grab addresses from a certain column on a certain sheet from many Excel files in a directory

    So my task is:
    -Loop through all Excel files in a certain directory.
    -Grab the values of cells in a certain column (they are all in the same column) up to a point.
    --This would be everything in between the first non-blank cell and the next non-blank cell inclusive: 3-5 lines worth.
    --There are no headers. It is an invoice-like sheet.
    -Place each value in a specific column in a new workbook.

    If I can provide further information just let me know.

    Frankly, I don't know where to start. Any push in the right direction is certainly appreciated. Somebody kick this new Excel programmer in the head.

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Excel 2003 - Get certain column from all XLS in a directory

    1. Build a list of file names (using file functions which I have not used in years).
    2. Load an instance of Excel Dim XL as New Excel.Application
    3. Xl.Open to all of your files in your list, make sure that you tell Excel to not ask you if you want to update linked data etc or that will throw you off. It is a property for the XL object.
    4. Open a new workbook, set its reference Set Wkb = Xl.Workbooks("thisisme")
    5. Cycle through the workbooks
    For I = 1 to Xl.Workbooks.Count Then
    'select the correct sheet
    'test workbook name to make sure your not ripping data from your composite data sheet
    'cycle through the rows until your done (test for empty/null)
    AddData "Data Ripped from other sheet"
    Next I
    6. Save your new workbook
    7. Close all workbooks

    Thats the gist of it. I can elaborate. As I have coded what your looking for except for the building a list of file names from a directory part.

  3. #3
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Excel 2003 - Get certain column from all XLS in a directory

    Heres what I have done. Not perfectly matched to what you want.
    Code:
    Private Type RiskItem
        FileN As String
        Name  As String
        Risk As String
        Efficiency As String
        MemberCount As String
    End Type
    
    Sub GetRiskData()
        Const StartRow = 10
        Const FirstCheckRow = 3
        Const FirstCheckCol = 5
        Const SecondCheckRow = 6
        Const SecondCheckCol = 9
        Const NameCol = 2
        Const RiskCol = 9
        Const EffCol = 23
        Const MembCol = 11
        Const NoMoreData = 10
        Dim index As Long
        Dim cur_index As Long
        Dim itms() As RiskItem
        Dim itm As RiskItem
        Dim blankcnt As Integer
        Dim wkb As Excel.Workbook
        Dim sht As Excel.Worksheet
        Dim tmpinfo As String
        Dim offset As Long
        index = 1
        'Loop through all Risk Workbooks
        For i = 1 To Excel.Workbooks.Count
            For ii = 1 To Excel.Workbooks(i).Sheets.Count
                Set sht = Excel.Workbooks(i).Worksheets(ii)
                blankcnt = 0
                If (CStr(sht.Cells(FirstCheckRow, FirstCheckCol)) = "Efficiency Summary") And _
                        (CStr(sht.Cells(SecondCheckRow, SecondCheckCol)) = "Relative Risk") Then
                    cur_index = StartRow    'Reset to first data row
                    tmpinfo = Excel.Workbooks(i).Name   'Use workbook name to determine
                                                        'LOB/Client/Spec
                    offset = 0
                    Do
                        If (Len(sht.Cells(cur_index + offset, NameCol)) <= 0) Then
                            blankcnt = blankcnt + 1
                            If blankcnt >= NoMoreData Then
                                Exit Do
                            End If
                        Else
                            blankcnt = 0
                            ReDim Preserve itms(index)
                            With itms(index - 1)
                                .FileN = tmpinfo
                                .Name = sht.Cells(cur_index + offset, NameCol)
                                .Risk = sht.Cells(cur_index + offset, RiskCol)
                                .Efficiency = sht.Cells(cur_index + offset, EffCol)
                                .MemberCount = sht.Cells(cur_index + offset, MembCol)
                            End With
                            index = index + 1
                        End If
                        offset = offset + 1
                    Loop
                End If
            Next ii
        Next i
    
    'Dump Data into Spreadsheet
        Excel.Workbooks.Add
        Set wkb = Excel.ActiveWorkbook
        Set sht = Excel.ActiveSheet
        sht.Cells(1, 1) = "File Name"
        sht.Cells(1, 2) = "Name"
        sht.Cells(1, 3) = "Risk"
        sht.Cells(1, 4) = "Efficiency"
        sht.Cells(1, 5) = "Member Count"
        For l = 0 To index - 1
            sht.Cells(l + 2, 1) = itms(l).FileN
            sht.Cells(l + 2, 2) = itms(l).Name
            sht.Cells(l + 2, 3) = itms(l).Risk
            sht.Cells(l + 2, 4) = itms(l).Efficiency
            sht.Cells(l + 2, 5) = itms(l).MemberCount
        Next l
    Cleanup:
        Set wkb = Nothing
        Set sht = Nothing
        MsgBox "Done"
    End Sub

  4. #4

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Excel 2003 - Get certain column from all XLS in a directory

    Thanks for the quick reply! I'm going to digest this and I'm sure I'll be back with some questions.

  5. #5

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Excel 2003 - Get certain column from all XLS in a directory

    So here is what I ended up doing. This is probably ugly and I'm certain many improvements can be made but until I get a chance to play around with the Excel object a bit more, this will do.

    The one question I did have is why can't I get to the cell's Value, Text or any properties for that matter when I use Cell(1,1) however I can when I use Range("A1")?

    Thanks again for the push in the right direction.

    vb.net Code:
    1. Imports System.IO
    2. Imports Microsoft.Office.Interop.Excel
    3.  
    4. Module Module1
    5.  
    6.     Sub Main()
    7.  
    8.         Console.WriteLine("Working...")
    9.  
    10.         Dim di As New DirectoryInfo("\\path\to\files")
    11.         Dim fi() As FileInfo = di.GetFiles()
    12.  
    13.         Dim app As New Application()
    14.         app.Visible = False
    15.  
    16.         Dim fromWb As Workbook = Nothing
    17.         Dim toWb As Workbook = app.Workbooks.Open("C:\Addresses.xls")
    18.         Dim fromSheet As Worksheet = Nothing
    19.         Dim toSheet As Worksheet = Nothing
    20.  
    21.         Dim record As Integer = 1
    22.  
    23.         For Each f As FileInfo In fi
    24.             fromWb = app.Workbooks.Open(f.FullName, UpdateLinks:=False)
    25.  
    26.             If SheetExists("royalty detail", fromWb) Then
    27.                 Dim firstData As Integer = 5
    28.  
    29.                 fromSheet = CType(fromWb.Worksheets("royalty detail"), Worksheet)
    30.                 toSheet = CType(toWb.Worksheets("Main"), Worksheet)
    31.  
    32.                 Do Until fromSheet.Range("C" & firstData).Text.ToString().Trim().Length > 0
    33.                     firstData += 1
    34.                 Loop
    35.  
    36.                 toSheet.Cells(record, 1) = fromSheet.Cells(firstData, 3)
    37.                 toSheet.Cells(record, 2) = fromSheet.Cells(firstData + 1, 3)
    38.                 toSheet.Cells(record, 3) = fromSheet.Cells(firstData + 2, 3)
    39.                 toSheet.Cells(record, 4) = fromSheet.Cells(firstData + 3, 3)
    40.                 toSheet.Cells(record, 5) = fromSheet.Cells(firstData + 4, 3)
    41.  
    42.                 record += 1
    43.             End If
    44.  
    45.             fromWb.Close(SaveChanges:=False)
    46.         Next f
    47.  
    48.         toWb.Close(SaveChanges:=True)
    49.  
    50.         Console.WriteLine("Complete.")
    51.  
    52.     End Sub
    53.  
    54.     Private Function SheetExists(ByVal sheet As String, ByVal wb As Workbook) As Boolean
    55.  
    56.         For Each sht As Worksheet In wb.Worksheets
    57.             If sht.Name = sheet Then
    58.                 Return True
    59.             End If
    60.         Next sht
    61.  
    62.         Return False
    63.  
    64.     End Function
    65.  
    66. End Module
    Last edited by nmadd; Aug 29th, 2007 at 04:01 PM.

  6. #6
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Excel 2003 - Get certain column from all XLS in a directory

    Yours is far more nice to look at then mine haha

    So your saying your fromSheet.Cells(firstData + 4, 3) returns nothing though there should be a value?

    The reason you get more options I am guestimating from the Range, is simply the object model support. The Cells references is probably a quick and dirty, and the Range exposes all properties. My thoughts.

  7. #7

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Excel 2003 - Get certain column from all XLS in a directory

    It does return a value and the code works as expected.
    I'm saying that I had to do this
    vb.net Code:
    1. Do Until fromSheet.Range("C" & firstData).Text.ToString().Trim().Length > 0
    because this
    vb.net Code:
    1. Do Until fromSheet.Cells(firstData, 3).Text.ToString().Trim().Length > 0
    doesn't work. I can't get to any of the cells properties using Cells in place of Range.

    Just curious.

  8. #8
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Excel 2003 - Get certain column from all XLS in a directory

    Shouldn't it be "Do While" instead of "Do Until"?

    I probably would of did..

    Do While Len(Trim((sht.Cells(3,3))))>0

  9. #9

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Excel 2003 - Get certain column from all XLS in a directory

    It's "Until" for my purpose. I was looking in the column for the first cell that had something in it. I was trying to get a reference to the first cell that contained some data.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width