我有一个工作簿,该工作簿有几千行,需要在其中确定每行的第一个和最后一个出现的“ Y”。 (这些字符根据其所属的列与特定的日期相关联)。我必须找到这些字符的开始和停止时间,因为它将给我每个任务的开始和停止日期。到目前为止,我已经尝试过:
Public Sub import_gbs()
Dim wb As Workbook
Dim gbs_wb As Workbook
Dim gbs_bg As Worksheet
Dim gbs_drivers As Worksheet
Dim bg As Worksheet
Dim dict As Scripting.Dictionary
Dim date_array() As String
Dim x As Long
Dim i As Long
Dim switch As Integer
Dim start_position As Integer
Dim stop_position As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.TransitionNavigKeys = False
Set wb = ThisWorkbook
Set bg = wb.Sheets("Current EDGE")
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set gbs_wb = Workbooks.Open("C:\SOME PATH HERE\")
Application.AutomationSecurity = msoAutomationSecurityByUI
Set gbs_bg = gbs_wb.Sheets("CC Input")
Set gbs_drivers = gbs_wb.Sheets("Drivers")
Set dict = New Scripting.Dictionary '''
dict.Add "SU YN Start", gbs_drivers.Cells(9, 5).Value
dict.Add "SU YN Stop", gbs_drivers.Cells(9, 6).Value
dict.Add "EPMP1 YN Start", gbs_drivers.Cells(10, 5).Value
dict.Add "EPMP1 YN Stop", gbs_drivers.Cells(10, 6).Value
dict.Add "EPMP2 YN Start", gbs_drivers.Cells(11, 5).Value
dict.Add "EPMP2 YN Stop", gbs_drivers.Cells(11, 6).Value
dict.Add "RC YN Start", gbs_drivers.Cells(12, 5).Value
dict.Add "RC YN Stop", gbs_drivers.Cells(12, 6).Value
dict.Add "ON YN Start", gbs_drivers.Cells(13, 5).Value
dict.Add "ON YN Stop", gbs_drivers.Cells(13, 6).Value
dict.Add "FU YN Start", gbs_drivers.Cells(14, 5).Value
dict.Add "FU YN Stop", gbs_drivers.Cells(14, 6).Value
dict.Add "DB Lock YN Start", gbs_drivers.Cells(15, 5).Value
dict.Add "DB Lock YN Stop", gbs_drivers.Cells(15, 6).Value
dict.Add "CO/Reporting YN Start", gbs_drivers.Cells(16, 5).Value
dict.Add "CO/Reporting YN Stop", gbs_drivers.Cells(16, 6).Value
i = 2
For x = 3 To 9543
If gbs_bg.Cells(x, 25).Value > 0 Then
start_position = gbs_bg.Range("AI" & x & ":" & "AP" & x).Find(what:="Y", lookat:=xlWhole, searchdirection:=xlNext).Column
stop_position = gbs_bg.Range("AI" & x & ":" & "AP" & x).Find(what:="Y", lookat:=xlWhole, searchdirection:=xlPrevious).Column
Select Case start_position
Case 35
gbs_bg.Cells(x, 65).Value = dict("SU YN Start")
Case 36
gbs_bg.Cells(x, 65).Value = dict("EPMP1 YN Start")
Case 37
gbs_bg.Cells(x, 65).Value = dict("EPMP2 YN Start")
Case 38
gbs_bg.Cells(x, 65).Value = dict("RC YN Start")
Case 39
gbs_bg.Cells(x, 65).Value = dict("ON YN Start")
Case 40
gbs_bg.Cells(x, 65).Value = dict("FU YN Start")
Case 41
gbs_bg.Cells(x, 65).Value = dict("DB Lock YN Start")
Case 42
gbs_bg.Cells(x, 65).Value = dict("CO/Reporting YN Start")
End Select
Select Case stop_position
Case 35
gbs_bg.Cells(x, 66).Value = dict("SU YN Stop")
Case 36
gbs_bg.Cells(x, 66).Value = dict("EPMP1 YN Stop")
Case 37
gbs_bg.Cells(x, 66).Value = dict("EPMP2 YN Stop")
Case 38
gbs_bg.Cells(x, 66).Value = dict("RC YN Stop")
Case 39
gbs_bg.Cells(x, 66).Value = dict("ON YN Stop")
Case 40
gbs_bg.Cells(x, 66).Value = dict("FU YN Stop")
Case 41
gbs_bg.Cells(x, 66).Value = dict("DB Lock YN Stop")
Case 42
gbs_bg.Cells(x, 66).Value = dict("CO/Reporting YN Stop")
End Select
i = i + 1
End If
Next x
因此,大多数情况下,这对我有用。在少数几行中确实有一些奇怪的实例,在第35列中存在一个“ Y”,但是查找未命中它并在该行中选取了下一个“ Y”实例。在7,000多个行中,这种情况仅发生了几次,并且始终在第35列中出现(但大多数情况下,如果在该行的该列中找到“ Y”,则通常会在该列中找到)。显然,这弄乱了我这些行的日期。我不知道那会是什么。我尝试通过检查缺少“ Y”的单元格的长度和值来尝试调试。但是,该结果将返回1,并且值为“ Y”。我试图在搜索值和公式之间进行切换,但这也不起作用。
因此,我需要找到一种新的方式来查找事件。我在想,因为我知道vba确实可以正确识别每个单元格的值的长度,所以我可以以此为基础。但是我不确定如何开始。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:0)
最简单的方法是创建一个小的for循环,以遍历8列数据(从AI到AP)并找到第一个和最后一个出现的“ Y”,并检查这是否非常耗时。但是,通过手动计算和屏幕更新,它应该不会太长。 在下面,您可以找到准备测试的简单子程序。
Option Explicit
Sub test()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim t#: t = Timer
Dim wbs As Worksheet
Dim x As Long
Dim col As Integer
Dim startposition As Integer
Dim endposition As Integer
Set wbs = ActiveSheet
With wbs
For x = 1 To 7000
startposition = 0
For col = 35 To 42
If .Range(.Cells(x, col), .Cells(x, col)).Value = "Y" And startposition = 0 Then
startposition = col
ElseIf .Range(.Cells(x, col), .Cells(x, col)).Value = "Y" And startposition <> 0 Then
endposition = col
End If
Next col
Next x
End With
Debug.Print Timer - t
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
我在空白纸上的分数是0.57s。您可以分析该代码,甚至可以实现该循环。对于x循环,仅是对7000行的模拟。