回答我的問題。我有我的標題,我正在導入到Sheet 2.我去了Sheet 3,並有Sheet3!A1 = Sheet2!A1然後Sheet3!B1 = Sheet2!B2等我使用該範圍並創建了一個命名範圍。然後我去了Sheet1!A1,並從Sheet3上創建的命名範圍中分配了下拉菜單。我爲Sheet1!B1和Sheet1!C1做了同樣的工作。然後我添加了下面的代碼。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Target.Cells.CountLarge > 1 Then Exit Sub
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow As Long, nCol As Long
Dim sSrch As String
Dim aCell As Range, rng As Range
Set wsI = ThisWorkbook.Sheets("Sheet2")
Set wsO = ThisWorkbook.Sheets("Sheet1")
Application.EnableEvents = False
If Not Intersect(Target, Range("A1:C1")) Is Nothing Then
sSrch = Cells(1, Target.Column).Value
Set aCell = wsI.Rows(1).Find(What:=sSrch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
nCol = aCell.Column
lRow = wsI.Cells(wsI.Rows.Count, nCol).End(xlUp).Row
Set rng = wsI.Range(wsI.Cells(2, nCol), wsI.Cells(lRow, nCol))
End If
If Not rng Is Nothing Then
Range(Cells(2, Target.Column), Cells(Rows.Count, Target.Column)).ClearContents
rng.Copy Cells(2, Target.Column)
End If
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
現在,當我使用工作表Sheet1!A1降了下來,這從表Sheet 3和VBA編碼的動態列表拉拉在匹配工作表Sheet1!A1選擇在Sheet2上列中的數據。此宏允許將列拖入Sheet1的前3行,具體取決於從下拉菜單中選擇的內容。