在 Excel 中使用两个下拉列表作为开始时间和结束时间复制行范围

在 Excel 中使用两个下拉列表作为开始时间和结束时间复制行范围

我有一个在定义的时间范围内从 SQL 获取的数据列表:

从 SQL 获取数据透视表

在该图像中,我还已经制作了名为Start time和的下拉列表End time

问题是:我如何制作一个宏,其输出是从下拉列表中复制整行Start time及其范围?End time

我也已经尝试使用聊天 GPT,但结果是空白的,没有错误消息,我尝试了各种配置,但没有运气,顺便说一句,我对宏 VBA 还不熟悉。

以下是来自 chatGPT 的代码(我已经对其进行了修改):

Sub CopyRowsBasedOnDropdowns()
Dim SourceSheet As Worksheet
Dim TargetSheet As Worksheet
Dim LastRow As Long
Dim CopyRange As Range
Dim Dropdown1 As String
Dim Dropdown2 As String
Dim Cell As Range

' Set the source and target sheets
Set SourceSheet = ThisWorkbook.Sheets("PivotSF") ' Change "SourceSheet" to your source sheet name
Set TargetSheet = ThisWorkbook.Sheets("Filteredtiming") ' Change "TargetSheet" to your target sheet name

' Get the selected values from the dropdowns
Dropdown1 = ThisWorkbook.Sheets("PivotSF").Range("I3").Value ' Change "DropdownSheet" and "A1" to your dropdown cell
Dropdown2 = ThisWorkbook.Sheets("PivotSF").Range("K3").Value ' Change "DropdownSheet" and "A2" to your second dropdown cell

' Define the range to copy
Set CopyRange = SourceSheet.Range("A2:F" & SourceSheet.Cells(Rows.Count, "A").End(xlUp).Row) ' Change "A" and the range as needed

' Clear previous data in the target sheet
TargetSheet.Cells.Clear

' Loop through the source sheet and copy rows that match the dropdown criteria
For Each Cell In CopyRange
    If Cell.Offset(0, 1).Value = Dropdown1 And Cell.Offset(0, 2).Value = Dropdown2 Then ' Change the offset and criteria as needed
        LastRow = TargetSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        Cell.EntireRow.Copy TargetSheet.Cells(LastRow, 1)
    End If
Next Cell

子目录结束

相关内容