gpt4 book ai didi

Excel VBA::在循环中查找函数

转载 作者:行者123 更新时间:2023-12-04 21:25:41 27 4
gpt4 key购买 nike

我正在尝试遍历几个工作表,其中包含一些必须复制到一个主工作表的源数据,这里称为“PriorityList”。
首先,子程序不工作,我认为错误出在“ 查找 ”方法中。其次,子程序需要很长时间才能运行,我认为这可能是因为“查找”方法搜索整个工作表而不是仅搜索相关范围?

非常感谢您的回答!

帕特里克

Sub PriorityCheck()
'Sub module to actualise the PriorityList

Dim CurrWS As Long, StartWS As Long, EndWS As Long, ScheduleWS As Long
StartWS = Sheets("H_HS").Index
EndWS = Sheets("E_2").Index

Dim SourceCell As Range, Destcell As Range

For CurrWS = StartWS To EndWS

For Each SourceCell In Worksheets(CurrWS).Range("G4:G73")

On Error Resume Next

'Use of the find method
Set Destcell = Worksheets(CurrWS).Cells.Find(What:=SourceCell.Value, After:=Worksheets("PriorityList").Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

'Copying relevant data from source sheet to main sheet
If Destcell <> Nothing Then
Destcell.Offset(0, 2).Value = SourceCell.Offset(0, 5).Value + Destcell.Offset(0, 2).Value
If SourceCell.Offset(0, 3).Value = "x" Then Destcell.Offset(0, 3).Value = "x"
End If
End If

On Error GoTo 0

Next SourceCell

Next CurrWS

End Sub

最佳答案

这里简短的示例如何 使用 'Find' 方法在 priorityList 中查找 source.Value 的第一次出现.

源单元格是“G4:G73”和 范围内的单元格之一优先级列表 在“PriorityList”表上使用范围。希望这可以帮助。

Public Sub PriorityCheck()
Dim source As Range
Dim priorityList As Range
Dim result As Range

Set priorityList = Worksheets("PriorityList").UsedRange

Dim i As Long
For i = Worksheets("H_HS").Index To Worksheets("E_2").Index
For Each source In Worksheets(i).Range("G4:G73")
Set result = priorityList.Find(What:=source.Value)
If (Not result Is Nothing) Then
' do stuff with result here ...
Debug.Print result.Worksheet.Name & ", " & result.Address
End If
Next source
Next i
End Sub

关于Excel VBA::在循环中查找函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com