【发布时间】:2023-01-31 19:22:39
【问题描述】:
我有一个非常大的 Excel 文件,我从中根据特定条件将完整的行(不是复制而是剪切)传输到另一个电子表格。搜索的条件不仅是名称(字符串),还可以是以例如开头的数字。 45*。我创建的代码适用于较小的文件,但对于较大的文件来说,它花费的时间太长,有时甚至会崩溃。 我想用更多功能扩展代码:
- 删除除主表之外的所有现有表。
- 搜索多个条件(例如“政府”、“中端市场”、“45“,“企业”),可以出现在“S”列中,并为在“S”列中找到的每个标准创建一个新表,并将完整行传输到新表中。新表的名称应该是名称定义的标准。
- 通过状态或进度条显示进度。
这是我目前使用的代码:
Sub VTest() Dim LastRow As Long Dim CurrentRow As Long Dim SourceSheetName As String SourceSheetName = "InstallBase" ' <--- Set this to name of the Source sheet Application.ScreenUpdating = False ' Turn ScreenUpdating off to prevent screen flicker Sheets.Add after:=Sheets(SourceSheetName) ' Add a new sheet after the Source sheet ActiveSheet.Name = "Midmarket" ' Assign a name to newly created sheet Sheets(SourceSheetName).Range("A1:AC1").Copy Sheets("Midmarket").Range("A1:AC1") ' Copy Header rows from Source sheet to the new sheet LastRow = Sheets(SourceSheetName).Range("A" & Rows.Count).End(xlUp).Row ' Determine Last used row in column A For CurrentRow = LastRow To 2 Step -1 ' Start at LastRow and work backwards, row by row, until beginning of data If Sheets(SourceSheetName).Range("S" & CurrentRow).Value Like "Midmarket" Then ' If we encounter a 'Yes' in column S then copy the row to new sheet Sheets(SourceSheetName).Rows(CurrentRow).Copy Sheets("Midmarket").Range("A" & Rows.Count).End(xlUp).Offset(1) Sheets(SourceSheetName).Rows(CurrentRow).Delete ' Delete the row from the Source sheet that contained 'Yes' in column S End If Next ' Continue checking previous row Application.ScreenUpdating = True ' Turn ScreenUpdating back on End Sub
【问题讨论】:
-
工作表可以在 Column S 上排序吗?
-
您是否尝试过在没有状态栏“监控”进度的情况下运行代码?它很可能是您代码中的一个严重“瓶颈”。此外,如果您只需要值而不是值、格式和公式,则可以大大提高性能。当然,最关键的是按照CDP1802的指示对数据进行排序。
-
我以前没有使用过进度条。这只是一个想法,看看程序在更大文件方面的进展情况。 @CDP1802,我刚刚在一个较小的文件上测试了你的代码。它很好用。我将在星期一在更大的文件(超过 65 万行)上再次测试它。感谢您的快速支持!