VBA运行导入TXT文件的macros后,清理工作表后会生成一个大的Excel文件

我有一个简单的macros,导入两个txt文件,并把它们放在单独的工作表,应用一些公式,筛选和粘贴结果在另外两张表。 (我是新来的VBA所以它真的很简单,基本上是一个macroslogging修改)

我的问题是,这个macros运行速度非常慢(其中一个txt文件有4MB的数据,大约14000行和12列,但是我有另一个运行在相同数量的行的瞬间的macros)。

我运行macros后,Excel文件达到9MB。 如果我清除所有的床单,它仍然8mb左右。

inheritance人macros观:

Sub Calcular_dif() ' ' Calcular_dif Macro ' Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ' IMPORTA OS DADOS EM TXT DO FINANCEIRO Financ = Application.GetOpenFilename("Text Files (*.txt), *.txt") If Financ = False Then Exit Sub With Sheets("FINANCEIRO").QueryTables.Add(Connection:="TEXT;" & Financ, Destination:=Range("FINANCEIRO!$A$1")) .Name = "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 1252 .TextFileStartRow = 1 .TextFileParseType = xlFixedWidth .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1) .TextFileFixedColumnWidths = Array(3, 7, 52, 10, 4, 11, 11, 11, 12, 6, 3, 17, 17, 17, 17, _ 17, 17, 17, 17, 17, 17) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With ' IMPORTA OS DADOS EM TXT DO BALANCETE Balanc = Application.GetOpenFilename("Text Files (*.txt), *.txt") If Balanc = False Then Exit Sub With Sheets("BALANCETE").QueryTables.Add(Connection:="TEXT;" & Balanc, Destination:=Range("BALANCETE!$A$1")) .Name = "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 1252 .TextFileStartRow = 1 .TextFileParseType = xlFixedWidth .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .TextFileFixedColumnWidths = Array(14, 6, 3, 41, 16, 3, 15, 16, 16) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With ' ORGANIZA A PLANILHA DO FINANCEIRO With Sheets("FINANCEIRO") .Rows("1:7").Delete Shift:=xlUp .Range("$A$1:$V$" & Rows.Count).AutoFilter Field:=1, Criteria1:=Array( _ "---", "Emp", "Fil", "="), Operator:=xlFilterValues .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter .Range("$A$1:$V$" & Rows.Count).AutoFilter Field:=5, Criteria1:=Array( _ "AD", "BOB", "BOL", "CAR", "CD", "DEP", "FC", "FOL", "GPS", "GUI", "JUR", "MNT", "PRC", _ "PRV", "RFS", "RJ", "SIN", "SRE", "SRF", "TAX"), Operator:=xlFilterValues .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter .Range("$A$1:$W$" & Rows.Count).AutoFilter Field:=5, Criteria1:="CON" .Range("$A$1:$W$" & Rows.Count).AutoFilter Field:=3, Criteria1:= _ "<>EXAME AUDITORES INDEPENDENTES - EPP" .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter .Columns("N:N").Insert Shift:=xlToRight .Range("N1").FormulaR1C1 = "Liq Contábil" .Range("N2:N" & .Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=RC[-2]-RC[3]-RC[4]-RC[8]" .Columns("K:K").NumberFormat = "0" .Columns("M:W").Style = "Comma" End With ' ORGANIZA A PLANILHA DO BALANCETE With Sheets("BALANCETE") .Rows("1:6").Delete Shift:=xlUp .Range("$A$1:$L$" & Rows.Count).AutoFilter Field:=1, Criteria1:=Array( _ "BGMRODOTEC TEC", "CNPJ: 00.51", "Conta", "Empresa: 003 P", "="), Operator:= _ xlFilterValues .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter .Columns("C:C").Delete Shift:=xlToLeft .Columns("A:A").Insert Shift:=xlToRight .Range("A2:A" & .Range("B" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=LEN(RC[1])" .Range("A1:L" & Rows.Count).AutoFilter Field:=1, Criteria1:=Array("7", "10", "="), Operator:=xlFilterValues .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter .Columns("A:A").Delete Shift:=xlToLeft .Columns("E:L").Style = "Comma" .Columns("B:B").Insert Shift:=xlToRight .Rows("1:1").ClearContents .Range("A1").FormulaR1C1 = "Classificador" .Range("B1").FormulaR1C1 = "Cód." .Range("C1").FormulaR1C1 = "Conta" .Range("D1").FormulaR1C1 = "Descrição" .Range("E1").FormulaR1C1 = "Saldo Anterior" .Range("G1").FormulaR1C1 = "Débito" .Range("H1").FormulaR1C1 = "Crédito" .Range("I1").FormulaR1C1 = "Saldo Atual" .Range("K1").FormulaR1C1 = "Financeiro" .Range("L1").FormulaR1C1 = "Diferença" End With ' INSERE AS FORMULAS DE PROCV PARA COD VS CONTA (FINANCEIRO) With Sheets("FINANCEIRO") .Columns("C:C").Insert Shift:=xlToRight .Range("C2:C" & .Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=VLOOKUP(RC[-1],'CONT VS COD'!C[-1]:C,2,0)" .Range("C1").FormulaR1C1 = "Conta" .Cells.EntireColumn.AutoFit End With ' INSERE AS FORMULAS DE PROCV PARA COD VS CONTA (BALANCETE) With Sheets("BALANCETE") .Range("B2:B" & .Range("C" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=VLOOKUP(RC[1],'CONT VS COD'!C[-1]:C,2,0)" ' INSERE FORMULA DE SOMASE PARA CALCULAR AS DIFERENÇAS .Range("K2:K" & .Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = _ "=SUMIF(FINANCEIRO!C[-8],BALANCETE!RC[-8],FINANCEIRO!C[4])" .Range("L2:L" & .Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=RC[-1]-RC[-3]" .Range("A1:L" & Rows.Count).AutoFilter Field:=12, Criteria1:="<>0" .Cells.Copy Sheets("DIFERENÇAS").Cells .Cells.AutoFilter .Cells.EntireColumn.AutoFit End With ' SEPARA OS NÃO ENCONTRADOS PELO PROCV With Sheets("BALANCETE") .Range("A1:L" & Rows.Count).AutoFilter Field:=2, Criteria1:="#N/D" .Cells.Copy Sheets("NÃO ENCONTRADOS").Cells .Rows("2:" & Rows.Count).Delete Shift:=xlUp .Cells.AutoFilter End With Sheets("NÃO ENCONTRADOS").Cells.EntireColumn.AutoFit Sheets("DIFERENÇAS").Cells.EntireColumn.AutoFit Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 

问题是,我把variables设置为TXT文件后,将其保存在代码中? 让它运行缓慢或什么?

谢谢

我的猜测是你的代码很慢,因为vlookup 。 我在计算大约28,000。 而且您正在使用精确的search选项,该search选项执行的是较慢的线性search,而不是更快的二分search。 先尝试对列B上的Worksheet("CONT vs COD")进行sorting,然后进行相似的匹配。

至于增加的大小。 这是一个众所周知的function(错误?)Excel跟踪您已经使用的整个范围。 按Ctrl + End将显示你在哪里Excel认为工作表的结尾。 只需select所有未使用的行,按Delete ,然后立即保存工作簿。 这将删除所有占用大量内存的空白行。