我有大量文件(超过500,000行),我需要查找“M”列是否等于“”,如果它等于“”,我需要在“N”列中写下某些内容
我使用=到“”因为IsEmpty()
在这些文件中不起作用(不确定原因)。
这是我目前的代码:(我删除了不必要的内容)
Sub sbVBA_COMMENTS_ExcelSheets()
'CONSTANTS SERVICENTRE FILES (PROD FOUR)
Const SC_STRLINE As Integer = 4 'FIRST PART IS ALWAYS AT LINE 4
Const SC_COLNUM As String = "B" 'PART NUMBERS WILL ALWAYS BE IN THE B COLUM FOR EVERY EXCEL FILES
Const SC_COLMKT As String = "K" 'NEW MARKETING CODE WILL ALWAYS BE IN THE K COLUM FOR EVERY EXCEL FILES
Const SC_COLDCT As String = "M" 'NEW DISCOUNT CODE WILL ALWAYS BE IN THE M COLUM FOR EVERY EXCEL FILES
Const SC_COLPRB As String = "N" 'NEW COMMENTS AND PROBLEM CODE WILL ALWAYS BE IN THE M COLUM FOR EVERY EXCEL FILES
'VARIABLES
Dim RowCount As Long
Dim ct As Long
'SET VARIABLES
RowCount = 0
ct = SC_STRLINE
Sheets(4).Select
'ADD COMMENTS
With Sheets(4)
RowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
While ct <= RowCount
If Sheets(4).Range(SC_COLDCT & ct).Value = "" Then
Sheets(4).Range(SC_COLPRB & ct).Value = "EMPTY"
End If
ct = ct + 1
Wend
'FILTER BY COMMENTS
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Add Key:= _
Range(SC_COLPRB & (SC_STRLINE - 1)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("PROD FOUR").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
这是有效的...不知怎的......但它是超级慢的,当我运行宏时,我的Excel几乎有70%的时间死掉。
有时,这个宏会导致一个问题,它会因某种原因在任何地方写下EMPTY ......
基本上,我正在寻找更快更清洁的方法来做同样的事情。
有没有办法在做同样的事情时获得性能?
请注意,我不是编码员,所以如果你能以我能理解的方式回答我,那将不胜感激。
答案 0 :(得分:2)
尝试使用以下代码。这不会处理范围,而是将范围转换为有希望更快的数组。
Sub sbVBA_COMMENTS_ExcelSheets()
'CONSTANTS SERVICENTRE FILES (PROD FOUR)
Const SC_STRLINE As Integer = 4 'FIRST PART IS ALWAYS AT LINE 4
Const SC_COLNUM As String = "B" 'PART NUMBERS WILL ALWAYS BE IN THE B COLUM FOR EVERY EXCEL FILES
Const SC_COLMKT As String = "K" 'NEW MARKETING CODE WILL ALWAYS BE IN THE K COLUM FOR EVERY EXCEL FILES
Const SC_COLDCT As String = "M" 'NEW DISCOUNT CODE WILL ALWAYS BE IN THE M COLUM FOR EVERY EXCEL FILES
Const SC_COLPRB As String = "N" 'NEW COMMENTS AND PROBLEM CODE WILL ALWAYS BE IN THE M COLUM FOR EVERY EXCEL FILES
'VARIABLES
Dim RowCount As Long
Dim varray As Variant
Dim i As Long
'SET VARIABLES
RowCount = 0
ct = SC_STRLINE
'disable unnecessary hindrances
Application.ScreenUpdating = False
Application.Calculation = xlManual
With ThisWorkbook.Sheets(4)
'find last row
RowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
varray = Range(SC_COLDCT & SC_STRLINE & ":" & SC_COLDCT & RowCount).Value
For i = UBound(varray, 1) To LBound(varray, 1) Step -1
If varray(i, 1) = "" Then 'if nothing
.Range(SC_COLPRB & i + 4).Value = "EMPTY"
End If
Next
'FILTER BY COMMENTS
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.Add Key:= _
Range(SC_COLPRB & (SC_STRLINE - 1)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
End With
With ActiveWorkbook.Worksheets("PROD FOUR").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
答案 1 :(得分:0)
下面的简单代码可以快速完成您想要的任务。您也可以使用&#34;&#34;而不是vbNullString。
Dim i As Long
Dim lR As Long
lR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lR
If Cells(i, "M").Value = vbNullString Then
Cells(i, "N").Value = "Empty"
End If
Next i