Поиск даты и копирования и вставка всей строки: VBA - PullRequest
0 голосов
/ 13 марта 2019

Я новичок в VBA, и я не уверен, как я могу выполнить следующую задачу.

Я хочу попросить пользователя указать диапазон дат, а затем взять этот диапазон дат и выполнить поиск в столбце BB «Исходных данных» для любой даты в этом диапазоне. Если дата находится в этом диапазоне, я хочу взять всю строку и скопировать ее, а затем вставить ее в другой лист с именем «Dest Sheet»

Любая помощь приветствуется! Я пробовал много разных способов сделать это, и ничего, что я сделал, не работает. Вот что у меня сейчас есть

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

1 Ответ

0 голосов
/ 13 марта 2019

Я думаю, что приведенный ниже код может быть тем, что вы ищете.

Однако, какова цель вашей переменной "j"? Кроме того, значения в столбце BB не отформатированы как даты? Я не был уверен, что цель строки, которая выполняет 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...