![](/img/trans.png)
[英]Search for a particular date in sheet 1 with FROM and TO dates in 2 columns and then paste data from a column in same row in sheet 1 into sheet 2
[英]VBA: Find a DATE & TEXT values in Sheet 1 (2 separate columns) and paste entire row in Sheet 2
如果滿足兩個條件,則通過Sheet1搜索:
文本提示用戶InputBox
用於DATE(列A)和字母“ 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
亞當,您已經掌握了基礎知識。 因此,假設工作表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,第22列),因此無需遍歷行中的每一列來檢查該值。
這就是我現在正在工作的東西。blo腫但有效
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.