根据另一个单元格Excel动态更新列表

时间:2019-04-03 17:30:17

标签: excel vba checkbox dropdown

主电子表格有700个条目,每个条目都有相关的类别。用户可以选择自己感兴趣的类别(复选框)。根据选择,摘要表应包含来自主表的条目详细信息。每个条目都有固定的列。每个条目的整个行都将被复制到摘要表中,而不仅仅是一个单元格。

最好可以动态更改此内容(即用户可以更改其选择,摘要表应自动更新)。

任何帮助将不胜感激。使用下面的代码,我将无法追加到现有数据。下面的语句不接受lastRow作为参数。

Sheets("Sheet4").Cells.Copy Destination:=Sheets("Dest").Cells(lastRow, 1) 

代码:这需要大量清洁。我还没有开始工作

Private Sub CommandButton1_Click()
Dim Src As Worksheet, Dest As Worksheet
Dim lastRow As Long, j As Long
Dim CellValue As String
Dim SrcSheets As String
Set Src = ThisWorkbook.Sheets("Sheet3") ' Needs to run through all the sheets. Change the sheet number using a for loop?
Set Dest = ThisWorkbook.Sheets("Dest")

'Clear the entire dest sheet before running the logic
Dest.Cells.ClearContents

'h24 is cell where the total rows are filled
Worksheets("Selection").Range("h24").Value = 0
lastRow = 1 ' this always makes the destination to start from first line, overwriting the existing rows

    If (Worksheets("Selection").Cells(1, 5).Value = True) Then

        Sheets("Sheet3").Cells.Copy Destination:=Sheets("Dest").Cells(1, 1)
        lastRow = Sheets("Sheet3").Cells.SpecialCells(xlLastCell).Row

        Worksheets("Selection").Range("h24").Value = lastRow
    End If

    ' Check if second checkbox is selected
    If (Worksheets("Selection").Cells(2, 5).Value = True) Then
        Sheets("Sheet4").Cells.Copy Destination:=Sheets("Dest").Cells(lastRow, 1)

        lastRow = ThisWorkbook.Sheets("Dest").Cells(Rows.Count, "A").End(xlUp).Row

        Worksheets("Selection").Range("h24").Value = lastRow
    End If
 End Sub

0 个答案:

没有答案