[英]VBA Find Text in Row 1. Copy & paste below rows if greater than zero
i have a code which searches for a text string in row 1. The seraching has no issues .我有一个在第 1 行搜索文本字符串的代码。搜索没有问题。
Problem问题
When the text is found i need the macro to search the column for values greater that zero and if found to copy the whole row and paste into sheet 2. So i have had no success.当找到文本时,我需要宏在列中搜索大于零的值,如果找到,则复制整行并粘贴到工作表 2 中。所以我没有成功。
Please see code below:请看下面的代码:
Private Sub btnUpdateEntry_Click()
Dim StringToFind As String
Dim i As Range
StringToFind = Application.InputBox("Enter string to find", "Find string")
Worksheets("Skills Matrix").Activate
ActiveSheet.Rows(1).Select
Set cell = Selection.Find(What:=StringToFind, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
For Each i In cell
If i.Value > 0 Then
i.Select
ActiveCell.Range("1:1").EntireRow.Select
Selection.Copy
Sheets("Sheet2").Range("A65000").End(xlUp).Offset(1, 0).PasteSpecial
End If
Next i
If cell Is Nothing Then
Worksheets("Data").Activate
MsgBox "String not found"
End If
End Sub
Thank you.谢谢你。
Try this, although I doubt that you need to search the entire column? 尝试此操作,尽管我怀疑您需要搜索整个列? Your loop was only searching one cell.
您的循环仅搜索一个单元格。 This code would need amending if the search string could be found more than once in the first row.
如果可以在第一行中多次找到搜索字符串,则需要修改此代码。
Private Sub btnUpdateEntry_Click()
Dim StringToFind As String
Dim i As Range
Dim cell As Range
StringToFind = Application.InputBox("Enter string to find", "Find string")
With Worksheets("Skills Matrix")
Set cell = .Rows(1).Find(What:=StringToFind, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
For Each i In .Range(cell.Offset(1), .Cells(.Rows.Count, cell.Column).End(xlUp))
If IsNumeric(i.Value) Then
If i.Value > 0 Then
i.EntireRow.Copy
Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
End If
Next i
Else
Worksheets("Data").Activate
MsgBox "String not found"
End If
End With
End Sub
You need to write explicit to search ByRows您需要编写显式搜索 ByRows
Set cell = .Rows(1).Find(What:=StringToFind, LookAt:=xlWhole, SearchOrder:=xlByRows,
MatchCase:=False, SearchFormat:=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.