Home |
Search |
Today's Posts |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Your posting got truncated because it was so long. It did not see a close
statement in the code that was posted. Are you closing each file after you read the data? "MikeZz" wrote: I have a VBA application that reads in many excel files (sometimes in the 100's), one at a time, scans them for key info, and summarizs the data in a new workbook. The routine actually doing the reading is the first one: ReadNewContract below. After about the 150th file (each usually under 40k), the macro started coming to a crawl and I noticed in Task Manager that Excel was using more and more memory (pushing 100MB). Excel was not releasing the files out of VBA Project. In a post from Tom Ogilvy, I saw that I could open the file in a new xlApp, then close it when I was done reading the data. This got rid of the memory issue but it dramatically increased the time it took to import the excel data worksheet into an array I could use. In the original code, I read the excel file into an array using this: arrMaster(r, c - LeftIndent) = ActiveSheet.Cells(r, c) I didn't need the xlApp reference because of the way I opened the workbook. In my revised code, I read the excel file into an array using this: arrMaster(r, c - LeftIndent) = xlApp.ActiveSheet.Cells(r, c) For some reason, it is just taking an incredible amount of time just to put an excel worksheet into an array. In my original code, it happened in a blink of an eye. With the "improved" code, it takes several seconds. Can anyone tell what I'm doing wrong? Is there a better way to read in the excel data? Thanks! MikeZz To make it a cut and paste for testing, I have all basic code attached. I put all the delcarations at the end so it's easier to find the code in question. Search for: '############### QUESTION HERE to find the area in question. Thanks! MikeZz Sub ReadNewContract(fileNo, arrMaster) Dim MasterFile Dim f, c, r Dim lngCount As Long Dim Master As Workbook Dim masterSht As Worksheet Dim rowsMaster, colsMaster, lastCellMaster Dim rowMax, rightCol Dim FoundIndent Dim matCount, matTotal Dim ctCellMaster Dim testRowCountMat Dim alertStat Dim tempXLFile Dim xlApp As New Excel.Application 'ADDED FOR MEMORY Dim FileString As String 'ADDED FOR MEMORY xlApp.Application.Visible = True 'ADDED FOR MEMORY '################################################# ########################################### '########### READ IN MASTER FILE '################################################# ########################################### If fileNo = 1 Or IsEmpty(fileLocExcel) Then fileLocExcel = Get_File_Info(arrFiles(fileNo, colFileName), "Directory") End If FileString = arrFiles(fileNo, colFileName) 'ADDED FOR MEMORY xlApp.Workbooks.Open (FileString) 'Focus is now on the workbook 'ADDED FOR MEMORY 'Workbooks.Open (arrFiles(fileNo, colFileName)) Set Master = xlApp.ActiveWorkbook Set masterSht = xlApp.ActiveSheet MasterFile = Master.Name lastCellMaster = LastCellIn(masterSht) rowsMaster = LastRowIn(masterSht) arrFiles(fileNo, colFileRows) = rowsMaster colsMaster = LastColIn(masterSht) If rowsMaster = Empty Or colsMaster = Empty Then Exit Sub End If ctCellMaster = 0 ReDim arrMaster(0) ReDim arrMaster(1 To rowsMaster, 0 To colsMaster) For r = 1 To rowsMaster LeftIndent = 0 FoundIndent = False rightCol = 0 For c = 1 To colsMaster '################################################# #################### '############### QUESTION HERE ########################### ' ' "xlApp.ActiveSheet.Cells(r, c)" seems to run magnitudes slower than using ' ActiveSheet.Cells(r, c) on a regular active sheet ' in original application instance. ' Is there another way? '################################################# #################### '################################################# #################### If alignLeft = True And FoundIndent = False And Len(xlApp.ActiveSheet.Cells(r, c)) = 0 Then LeftIndent = LeftIndent + 1: GoTo nextMc End If FoundIndent = True arrMaster(r, c - LeftIndent) = xlApp.ActiveSheet.Cells(r, c) If Len(arrMaster(r, c - LeftIndent)) < 0 Then rightCol = c - LeftIndent nextMc: Next c arrMaster(r, 0) = rightCol Next r Master.Close SaveChanges:=False Set masterSht = Nothing Set Master = Nothing xlApp.Quit Set xlApp = Nothing 'ADDED FOR MEMORY End Sub Private Sub Get_File_List() Dim lngCount Dim maxcols Call Initialize_Values maxcols = colFileMaxx With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = True .Show fileCount = .SelectedItems.Count ReDim arrFiles(0) ReDim arrFiles(0 To .SelectedItems.Count, 1 To maxcols) ' Display paths of each file selected If fileCount = 0 Then End For lngCount = 1 To fileCount arrFiles(lngCount, colFileName) = .SelectedItems(lngCount) Next lngCount End With Dim f For f = 1 To fileCount Call ReadNewContract(f, arrImport) Next f End Sub Private Sub Initialize_Values() Dim col0, r ScanTime = Format(Now, "Medium Time") dateSummaryFormat = Control.Range("dateSummaryFormat") Select Case dateSummaryFormat Case "yyyy-mm-dd" ScanDate = Format(Date, dateSummaryFormat) Case "dd-mmm-yy" ScanDate = Format(Date, dateSummaryFormat) Case "yyyy-ww-ddd" ScanDate = Format(Date, dateSummaryFormat) Case Else ScanDate = Format(Date, "Medium Time") End Select 'AutoSaveFile = "x" & ScanDate & "Contracts Scanned " & " " & Replace(ScanTime, ":", "_") & ".xls" AutoSaveFile = ScanDate & "Contracts Scanned " & " " & Replace(ScanTime, ":", "_") & ".xls" fileCount = 0 completeCount = 0 col0 = 1 colFileName = col0: col0 = col0 + 1 'Filename colFileStat = col0: col0 = col0 + 1 'Scan Status colFileCust = col0: col0 = col0 + 1 colFileType = col0: col0 = col0 + 1 'Contract Type - Part, Tooling, etc colFileProd = col0: col0 = col0 + 1 'Contract Part Product Area AB/SB/SW/EL colFilePro2 = col0: col0 = col0 + 1 colFileProj = col0: col0 = col0 + 1 'Contract ALV Project Number colFileDesc = col0: col0 = col0 + 1 'Contract Part Description colFilePNum = col0: col0 = col0 + 1 'Contract Part Number colFileLNum = col0: col0 = col0 + 1 'Contract Less Finish Part Number colFileCNum = col0: col0 = col0 + 1 'Contract Number colFileRevs = col0: col0 = col0 + 1 'Contract Revision colFileDate = col0: col0 = col0 + 1 'Contract Date colFileReas = col0: col0 = col0 + 1 'Contract Amendment Reason colFileESOP = col0: col0 = col0 + 1 'Contract Effective Start Date colFileEEOP = col0: col0 = col0 + 1 'Contract Effective End Date colFilePeri = col0: col0 = col0 + 1 'Price Periods colFilePric = col0: col0 = col0 + 1 'Starting Price colFileLTAs = col0: col0 = col0 + 1 'LTA %= col0: col0 = col0 + 1 's colFilePri2 = col0: col0 = col0 + 1 'Ending Price (part contracts) colFileCurr = col0: col0 = col0 + 1 'Currency Type - First colFileCurX = col0: col0 = col0 + 1 'Currency Type - Change to colFilePack = col0: col0 = col0 + 1 'Packaging Type - First colFilePacX = col0: col0 = col0 + 1 'Packaging Type - Change to colFileCapa = col0: col0 = col0 + 1 'Starting Price colFileCap2 = col0: col0 = col0 + 1 'Ending Price (part contracts) colFileHPDa = col0: col0 = col0 + 1 'Hours Per Day - First colFileHPDX = col0: col0 = col0 + 1 'Hours Per Day - Change to colFileDunM = col0: col0 = col0 + 1 'Contract Mfg Dunns Code colFileDDun = col0: col0 = col0 + 1 'Delivery To Duns - First colFileDDuX = col0: col0 = col0 + 1 'Delivery To Duns - Change to colFileSDun = col0: col0 = col0 + 1 'Ship From Duns - First colFileSDuX = col0: col0 = col0 + 1 'Ship From Duns - Change to colFileTFre = col0: col0 = col0 + 1 'Terms Start - Freight colFileTFrX = col0: col0 = col0 + 1 'Terms Ending - Freight colFileTPay = col0: col0 = col0 + 1 'Terms Start - Payment colFileTPaX = col0: col0 = col0 + 1 'Terms Ending - Payment colFileTDel = col0: col0 = col0 + 1 'Terms Start - Delivery colFileTDeX = col0: col0 = col0 + 1 'Terms Ending - Delivery colFileBuyr = col0: col0 = col0 + 1 'Contract Buyer Name colFileHead = col0: col0 = col0 + 1 'Contract Header colFileDown = col0: col0 = col0 + 1 'Contract Download Date colFileSNam = col0: col0 = col0 + 1 'Contract Sheet Name colFileRows = col0: col0 = col0 + 1 'Contract Effective End Date colFileNam2 = col0: col0 = col0 + 1 'New Smart File Name colFileKeys = col0: col0 = col0 + 1 'Contract Key - Part English, Part Mexico, Tooling English etc.. colFileMaxx = col0 + 5 col0 = 0 colKeyDeffName = col0 + 1 'Key Definition: Name colKeyDeffCust = colKeyDeffName + 1 'Key Definition: OEM colKeyDeffType = colKeyDeffCust + 1 'Key Definition: Contract Type colKeyDeffLang = colKeyDeffType + 1 'Key Definition: Language colKeyDeffIden = colKeyDeffLang + 1 'Key Definition: Unique Identifier String used to deterime which key to use colKeyDeffSNum = colKeyDeffIden + 1 'Key Definition: Key Sheet Number colKeyDeffIMxR = colKeyDeffSNum + 1 'Key Definition: colKeyDeffIMxC = colKeyDeffIMxR + 1 'Key Definition: colKeyDeffITyp = colKeyDeffIMxC + 1 'Key Definition: colKeyDeffSNam = colKeyDeffITyp + 1 'Key Definition: Key Sheet Name colKeyDeffMaxx = colKeyDeffSNam + 5 col0 = 0 colKeyAnchName = col0 + 1 'Key Anchor: Name colKeyAnchStri = colKeyAnchName + 1 'Key Anchor: Search String (which defines location) colKeyAnchType = colKeyAnchStri + 1 'Key Anchor: Match Type (Full, Partial Match) colKeyAnchStar = colKeyAnchType + 1 'Key Anchor: Start Location in File (Top, Previous Key, Bottom) colKeyAnchDire = colKeyAnchStar + 1 'Key Anchor: Search Direction from Start (Down, Up) colKeyAnchLoca = colKeyAnchDire + 1 'Key Anchor: Row Location (Left, Right, Any) colKeyAnchFunc = colKeyAnchLoca + 1 'Key Anchor: Special Function to perform (such as count repeats) colKeyAnchRowX = colKeyAnchFunc + 1 'Key Anchor: Row in current file where this Anchor is found colKeyAnchColY = colKeyAnchRowX + 1 'Key Anchor: Col in current file where this Anchor is found colKeyAnchMaxx = colKeyAnchColY + 5 colKeyAnchStat = colKeyAnchMaxx colKeyAnchFSta = colKeyAnchStat - 1 'Key Anchor: Status of Anchor Function col0 = 0 colKeyCodeCode = col0 + 1 'Key Code: Code colKeyCodeDesc = colKeyCodeCode + 1 'Key Code: Description of search: colKeyCodeStri = colKeyCodeDesc + 1 'Key Code: Search String (which defines location) colKeyCodeStar = colKeyCodeStri + 1 'Key Code: Start Location in File (Top, Previous Key, Bottom) colKeyCodeType = colKeyCodeStar + 1 'Key Code: Match Type (Full, Partial Match) colKeyCodeDirS = colKeyCodeType + 1 'Key Code: Direction from Start to find Key Text String colKeyCodeDirA = colKeyCodeDirS + 1 'Key Code: Direction from Key Test String to find Answer colKeyCodeLook = colKeyCodeDirA + 1 'Key Code: Look Location to from Key String to find answer (next value, last value in row) colKeyCodeComm = colKeyCodeLook + 1 'Key Code: Command to perform |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Reading current opened Excel book(file) into streams | Excel Programming | |||
Why Excel files opened from Explorer are slow - MVP response pleas | Excel Discussion (Misc queries) | |||
MY ARRAY FORMULA IS SLOW IF DATA LIST IS MORE THAN 10000R0WS | Excel Programming | |||
Reading from a excel sheet to an array | Excel Programming | |||
[excel 97 vba ] Reading ranges into an array | Excel Programming |