Vba “查找下一步”仅用于第一次迭代
关于我的下一个发现,我有一个问题,很可能是我写错了代码。所以如果我有810号(即f),找到地址,再次找到810号其他位置的地址,依此类推 然后我有前两个工作良好。因此,当findnext第一次按我想要的方式运行时,粘贴到最后一个单元格,但其他单元格不起作用。因此,当我第二次找到next时,它不再复制和粘贴。你知道我怎么修吗Vba “查找下一步”仅用于第一次迭代,vba,excel,Vba,Excel,关于我的下一个发现,我有一个问题,很可能是我写错了代码。所以如果我有810号(即f),找到地址,再次找到810号其他位置的地址,依此类推 然后我有前两个工作良好。因此,当findnext第一次按我想要的方式运行时,粘贴到最后一个单元格,但其他单元格不起作用。因此,当我第二次找到next时,它不再复制和粘贴。你知道我怎么修吗 With RgnScenarioScenario Set f = .Find(What:=f, LookIn:=xlValues, LookAt:=xlWhole)
With RgnScenarioScenario
Set f = .Find(What:=f, LookIn:=xlValues, LookAt:=xlWhole)
WsScenarios.Activate
f.Select
q = f.Address
Set x = Range("A:A").FindNext(f)
x.Select
z = x.Address
Set m = Range("A:A").FindNext(f)
m.Select
n = m.Address
Set k = Range("A:A").FindNext(f)
k.Select
w = k.Address
Set a = Range("A:A").FindNext(f)
a.Select
g = a.Address
If q <> z Then
Range(z).Offset(0, 5).Select
Range(ActiveCell, ActiveCell.Offset(0, ScenarioLastColumn - 6)).Copy
WsOutput.Activate
WsOutput.Cells(lLastRow, Columnf).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Else
End If
If z <> n Then
Range(n).Offset(0, 5).Select
Range(ActiveCell, ActiveCell.Offset(0, ScenarioLastColumn - 6)).Copy
WsOutput.Activate
WsOutput.Cells(lLastRow, Columnf).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Else
End If
If n <> w Then
Range(n).Offset(0, 5).Select
Range(ActiveCell, ActiveCell.Offset(0, ScenarioLastColumn - 5)).Copy
WsOutput.Activate
WsOutput.Cells(lLastRow, Columnf).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Else
End If
If w <> g Then
Range(n).Offset(0, 5).Select
Range(ActiveCell, ActiveCell.Offset(0, ScenarioLastColumn - 5)).Copy
WsOutput.Activate
WsOutput.Cells(lLastRow, Columnf).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Else
End If
End With
Next f
与rgnscenario场景
Set f=.Find(What:=f,LookIn:=xlValues,LookAt:=xlWhole)
WsScenarios.Activate
f、 挑选
q=f.地址
设置x=范围(“A:A”)。FindNext(f)
x、 挑选
z=x.地址
设置m=范围(“A:A”)。FindNext(f)
m、 挑选
n=m.地址
设置k=范围(“A:A”)。FindNext(f)
k、 挑选
w=k.地址
设置a=范围(“a:a”)。FindNext(f)
a、 挑选
g=a.地址
如果qz那么
范围(z)。偏移量(0,5)。选择
范围(ActiveCell,ActiveCell.Offset(0,ScenarioLastColumn-6))。复制
WsOutput.Activate
WsOutput.Cells(lLastRow,Columnf).Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlanks:=False,转置:=True
其他的
如果结束
如果z n那么
范围(n)。偏移量(0,5)。选择
范围(ActiveCell,ActiveCell.Offset(0,ScenarioLastColumn-6))。复制
WsOutput.Activate
WsOutput.Cells(lLastRow,Columnf).Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlanks:=False,转置:=True
其他的
如果结束
如果n w那么
范围(n)。偏移量(0,5)。选择
范围(ActiveCell,ActiveCell.Offset(0,ScenarioLastColumn-5))。复制
WsOutput.Activate
WsOutput.Cells(lLastRow,Columnf).Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlanks:=False,转置:=True
其他的
如果结束
如果w g那么
范围(n)。偏移量(0,5)。选择
范围(ActiveCell,ActiveCell.Offset(0,ScenarioLastColumn-5))。复制
WsOutput.Activate
WsOutput.Cells(lLastRow,Columnf).Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlanks:=False,转置:=True
其他的
如果结束
以
下一个f
我的代码寻找的是场景ID的范围值,在本例中为810。正如您在第一个打印屏幕中看到的,我有3倍810。在下面的示例中,只需识别2。这就是问题所在,它应该尽可能多地确定。我正在为这首曲子而挣扎。我尝试了自动筛选,但没有解决我的问题。这都是关于操作ID的。有3个,所以,我的代码应该会附带3个,这是一个“FindAll”函数,您可以使用它吗
Function FindAll(What, _
Optional SearchWhat As Variant, _
Optional LookIn, _
Optional LookAt, _
Optional SearchOrder, _
Optional SearchDirection As XlSearchDirection = xlNext, _
Optional MatchCase As Boolean = False, _
Optional MatchByte, _
Optional SearchFormat) As Range
'LookIn can be xlValues or xlFormulas, _
LookAt can be xlWhole or xlPart, _
SearchOrder can be xlByRows or xlByColumns, _
SearchDirection can be xlNext, xlPrevious, _
MatchCase, MatchByte, and SearchFormat can be True or False. _
Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""
Dim SrcRange As Range
If IsMissing(SearchWhat) Then
Set SrcRange = ActiveSheet.UsedRange
ElseIf TypeOf SearchWhat Is Range Then
Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
ElseIf TypeOf SearchWhat Is Worksheet Then
Set SrcRange = SearchWhat.UsedRange
Else: Set SrcRange = ActiveSheet.UsedRange
End If
If SrcRange Is Nothing Then Exit Function
'get the first matching cell in the range first
With SrcRange.Areas(SrcRange.Areas.Count)
Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
End With
Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If Not CurrRange Is Nothing Then
Set FindAll = CurrRange
Do
Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If CurrRange Is Nothing Then Exit Do
If Application.Intersect(FindAll, CurrRange) Is Nothing Then
Set FindAll = Application.Union(FindAll, CurrRange)
Else: Exit Do
End If
Loop
End If
End Function
为什么在代码末尾有下一个f?您以前是否有丢失的代码?请查看集合f=。查找(What:=f,LookIn:=xlValues,LookAt:=xlWhole),f是您的值(f=1018),同时它是一个范围?在模块顶部指定
Option Explicit
,然后用特定类型声明每个变量(例如Dim f As range
),然后在代码再次编译后再回到这个问题Next f
似乎表示f
是一个循环计数器(一个整数
?一个长的
?),即f=?到带块的之前的某个地方,但您正在将f
变为带
块的第一行的范围
,即该代码毫无意义。此外。。。请缩进你的代码。如果您不确定如何实现,我的little(v2.x,仍然是beta版)可以帮助您自动完成。适当的缩进使您的代码更容易理解,无论是您还是任何阅读它的人。因此,第二次,您的f是一个范围,您正在“.Find(What:=f)”中查找一个范围。所以它永远不会被找到。这就是为什么你的代码只找到了一次1018。这就是我需要的!非常感谢。我不好,不知道所有的可能性。=)