VBA:找一个日期&工作表1中的TEXT值(2个单独的列)并在工作表2

时间:2017-04-18 12:33:09

标签: arrays vba excel-vba search excel

如果满足2个条件,请搜索Sheet1:

DATE(列A)的文本提示用户InputBox和字母“X”(列V)是常量。如果满足两个条件,则将整行复制+粘贴到Sheet2。

以下是我以前工作中整数搜索的内容:

Private Sub CommandButton5_Click()

Dim strsearch As String, lastline As Integer, tocopy As Integer

'find what date? find other value?
strsearch = CStr(InputBox("enter the string to search for"))
lastline = Range("A65536").End(xlUp).Row
j = 1
For i = 1 To lastline
    For Each c In Range("A" & i & ":V" & i)
        If InStr(c.Text, strsearch) Then
            tocopy = 1
        End If
    Next c
    'Copy the current row
    If tocopy = 1 Then
        Rows(i).Copy Destination:=Sheets("MONTH_END").Rows(j)
        j = j + 2
    End If
tocopy = 0
Next i

End Sub

2 个答案:

答案 0 :(得分:0)

你有基本的亚当。因此,假设第1页上的数据是连续的(即没有空单元格后跟更多数据,但空单元格是循环结束),并假设数据表(表单1)是活动表格,你会看到这个:

StrSearch = CStr(InputBox("Enter the string to search for"))
j = Sheets("MONTH_END").Range("A65536").End(xlUp).Row

For i = 1 to Range("A65536").End(xlUp).Row
     If InStr(ActiveSheet.Cells(i, 1).Value, StrSearch) And ActiveSheet.Cells(i, 22).Value = "A" Then
         ActiveSheet.Rows(i).Copy Destination:=Sheets("MONTH_END").Rows(j)
         j = Sheets("MONTH_END").Range("A65536").End(xlUp).Row         'recalculate J now that a row has been pasted in
     ElseIf IsEmpty(ActiveSheet.Cells(i, 1)) Then
         Exit For         ' The get-out clause of the loop
     End If
Next i

此方法只需要1 For Each,因为您要查找的第二个值始终位于同一列(V,col 22)中,因此无需检查行中的每一列检查值

答案 1 :(得分:0)

这就是我现在正在工作的......浮动但工作..

Private Sub CommandButton5_Click()
Dim i As Long, endRow As Long, pasteRowIndex As Long

'find what date? find other value?
endRow = 6000 ' of course it's best to retrieve the last used row number via a function
pasteRowIndex = 1

Sheets("COMBINED").Range("V3").Value = InputBox("Please Input Date = NOTE: 
Format MM/DD/YYYY")

For i = 1 To endRow 'Loop through COMBINED and search for your criteria

 If Sheets("COMBINED").Cells(i, 1).Value = Range("V3") Or Sheets("COMBINED").Cells(i, 10).Value = "A" Then 'Found

        'Copy the current row
        Rows(i).Select
        Selection.Copy

        'Switch to the sheet where you want to paste it & paste
        Sheets("MONTH_END").Select
        Rows(pasteRowIndex + 1).Select
        ActiveSheet.Paste

        'Next time you find a match, it will be pasted in a new row
        pasteRowIndex = pasteRowIndex + 1


       'Switch back to your table & continue to search for your criteria
        Sheets("COMBINED").Select
   End If
Next i
    'Go Home
        Sheets("MONTH_END").Select
        Application.Goto Range("A1"), True
        ActiveWindow.VisibleRange(1, 1).Select
        Sheets("COMBINED").Select
        Application.Goto Range("A1"), True
        ActiveWindow.VisibleRange(1, 1).Select
End Sub