问题
I'm stucked with using Find and set as variable. I cannot get the result I need. In first sheet I have a column Test with values x or (x). If the value is x I need to copy the value from column EN. If the value is (x) do not copy.
the code copies values from column "EN" no matter x or (x)
I must have probably an error in using Set stfound
Dim ENcolumn
Dim xcolumn
Dim secrow
Dim lastrow
Dim totrow
Worksheets("List1").Activate
Worksheets("List1").Range("A1:C1").Find(What:="EN", MatchCase:=True,
lookAT:=xlWhole).Activate
ENcolumn = ActiveCell.Column 'find and create variable
Worksheets("List1").Range("A1:C1").Find(What:="test", MatchCase:=True,
lookAT:=xlWhole).Activate
xcolumn = ActiveCell.Column 'find and create variable
currow = ActiveCell.Row + 1 ''make one low rower than current row (first
value)
lastrow = Worksheets("List1").Cells(Rows.Count, xcolumn).End(xlUp).Row
For totrow = currow To lastrow
Set stfound = Cells.Find(What:="x", After:=Cells(totrow, xcolumn),
MatchCase:=True, lookAT:=xlWhole)
If Not stfound Is Nothing Then 'if value is found then do this
Worksheets("List1").Cells(totrow, ENcolumn).Copy 'copy values
Worksheets("List2").Activate
b = Worksheets("list2").Cells(Rows.Count, ENcolumn).End(xlUp).Row
Worksheets("list2").Cells(b + 1, 2).Select 'select first empty cell in
second column
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Value = "receivercode"
ActiveCell.Offset(0, 2).Value = "01.01.2019"
Worksheets("list1").Activate
End If
Next
Application.CutCopyMode = False 'stop if false
ThisWorkbook.Worksheets("List1").Cells(1, 1).Select
MsgBox ("done")`
Now I get all the values from column "EN" copied to sheet2 to column2.
I need only those values in column EN that has x value in column 1
回答1:
You need to repeat search until you find all. Note that search is circular so you need to remember the first match. (The good news is that it can be any cell so you do not need to start searching from the very first cell.) Here is a frame for searching the entire sheet for a specific value:
Dim s1st As String
Dim rFnd as Range
Set rFnd = Nothing
With ActiveSheet.UsedRange
Set rFnd = .Cells.Find(What:="x", LookIn:=xlValues, lookat:=xlWhole, _
SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=True)
If Not rFnd Is Nothing Then
s1st = rFnd.Address
Do
' do here what you need to do with your found cell.
' rFnd points to the found cell with the value "x"
' e.g.
rFnd.Copy ' single cell
b = Worksheets("list2").Cells(Rows.Count, ENcolumn).End(xlUp).Row
Worksheets("list2").Paste Destination:=Worksheets("list2").Cells(b + 1, 2)
Set rFnd = .FindNext(rFnd)
Loop While Not rFnd Is Nothing And rFnd.Address <> s1st
End If
End With
NB: you may keep track of the actual destination cell instead of finding the last one in every loop. So you find the first destination cell (.End(...)...
) once in the initialisation phase and then simply increment the row counter within the loop. Though you will notice any increase in speed only over thousands of rows.
来源:https://stackoverflow.com/questions/55984796/find-and-set-as-variable-and-if