简体   繁体   English

搜索日期并复制并粘贴整行:VBA

[英]Search for Date and copy and Paste Whole Row: VBA

I am new to VBA and I am not sure how I can accomplish the following task. 我是VBA的新手,我不确定如何才能完成以下任务。

I want to ask the user for a date range and then take that date range and search through column BB of “Source Sheet” for any date in that range. 我想问用户一个日期范围,然后选择该日期范围,并在“源工作表”的BB列中搜索该范围内的任何日期。 If a date is in that range I want to take the entire row and copy it and then paste it into a different sheet called “Dest Sheet” 如果日期在该范围内,我要整行复制,然后将其粘贴到另一个名为“目标表”的表中

Any help is appreciated! 任何帮助表示赞赏! I have tried many different ways of doing this and nothing I have done is working. 我尝试了许多不同的方法来执行此操作,但我没有做任何工作。 Here is what I currently have 这是我目前拥有的

Dim N As Integer
Dim i As Integer
Dim StartDate As Date
Dim EndDate As Date

N = Cells(Rows.Count, "E").End(xlUp).Row    'determines the last row with data in it
                                              'uses column E because E should never be Null if not after last active row

Dim j As Integer 'declares j for indexing of the rows on the target spreadsheet
j = 2

Dim fRow As Long                                    'is the role of j in an attempt to copy and paste
fRow = Sheets("Dest Sheet").UsedRange.Rows.Count         'defines the variable

For i = 2 To N          'indexes 2 to N to begin with row after the title row to the last active ro
Cells(i, "BB").Select
    If Cells(i, "BB").Value <> "" Then

    Columns("BB").Select
    Selection.NumberFormat = "mm/dd/yyyy"

    Range("BB2").End(xlDown).Select

    StartDate = Application.InputBox("Enter the start date")
    EndDate = Application.InputBox("Enter the end date")

    'in row i execute the if statement

        If ("BB" >= StartDate & "BB" <= EndDate) Then

         Sheets("Source Sheet").Cells(i, 1).EntireRow.Copy Destination:=Sheets("Dest Sheet").Cells(fRow, 1).Offset(1, 0)
         fRow = fRow + 1

        End If

 End If  'declares end of the if statements

Next i

End Sub

I think the below code might be what you are looking for. 我认为以下代码可能是您想要的。

However, what is the purpose of your variable "j"? 但是,变量“ j”的目的是什么? Also, are the values in column BB not formatted as dates? 另外,BB列中的值是否未格式化为日期? I wasn't sure the purpose of the line that executes Selection.NumberFormat = "mm/dd/yyyy" 我不确定执行Selection.NumberFormat =“ mm / dd / yyyy”的行的用途

Dim wb As Workbook
Dim wsSrc As Worksheet
Dim wsDest As Worksheet
Dim n As Long
Dim i As Long
Dim fRow As Long
Dim startDate As Date
Dim endDate As Date

Set wb = ActiveWorkbook
Set wsSrc = wb.Sheets("Source Sheet")
Set wsDest = wb.Sheets("Dest Sheet")

n = wsSrc.Range("E:E").Find(what:="*", searchdirection:=xlPrevious).Row
startDate = Application.InputBox("Enter the start date")
endDate = Application.InputBox("Enter the end date")

For i = 2 To n

    If wsSrc.Range("BB" & i).Value >= startDate And wsSrc.Range("BB" & i).Value <= endDate Then

        fRow = wsDest.Range("A:A").Find(what:="").Row
        wsSrc.Range("BB" & i).EntireRow.Copy wsDest.Cells(fRow, 1)

    End If

Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM