I have a big sheet worth of data and want to copy and paste all rows containing "HVT"
to another sheet. I am new to VBA and I think the code I have searches the entire cell instead of the contents of the cell. For example if the cell said only "HVT"
it would work, but the cells have multiple strings in them. An example of something that could be in a cell would be something like "mechanical system damper HVT purchased"
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 11).Value = "HVT" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
Excel if statements accept wildcards, so you just need to know if you want to match the whole word "HVT" or not:
Match whole word:
If Worksheets("Sheet1").Cells(i, 11).Value Like "* HVT *" Or Worksheets("Sheet1").Cells(i, 11).Value Like "HVT *" Or Worksheets("Sheet1").Cells(i, 11).Value Like "* HVT" Then
Or match any instance of HVT ("xHVTx" returns true
):
If Worksheets("Sheet1").Cells(i, 11).Value Like "*HVT*" Then
Will work