如何在 VBA 中插入新行后添加下拉列表

如何在 VBA 中插入新行后添加下拉列表

我尝试在添加新行后插入一个包含两个项目的下拉列表。以下代码无法正常工作。

     Dim varUserInput As Variant
     varUserInput = InputBox("Enter Row Number where you want to add a row:", 
    "What Row?")
    If varUserInput = "" Then Exit Sub
    RowNum = varUserInput
    Rows(RowNum + 1).Insert Shift:=xlDown
    With Sheet1.RowNum.listBox1
     .AddItem "Paris"
     .AddItem "New York"
    End With

答案1

尝试这个:

Sub Macro1()

RowNum = InputBox("Enter Row Number where you want to add a row:", "What Row?")
If RowNum = "" Then Exit Sub

Range("A1").Offset(RowNum, 0).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="London,Sydney"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With
End Sub

注意:我使用“开发人员”选项卡上的“录制宏”工具录制了大部分内容,然后进行了编辑以使用部分代码。当您不确定插入某些内容(例如此下拉验证列表)所需的确切代码时,这是一种有用的方法。

编辑:同时添加两个列表:

Sub Macro1()
'ask user for row to insert data
RowNum = InputBox("Enter Row Number where you want to add a row:", "What Row?")
If RowNum = "" Then Exit Sub

'insert dropdowns in column A
Range("A1").Offset(RowNum, 0).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="London,Sydney"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

'inset second drop down in column E
Range("E1").Offset(RowNum, 0).Select '<-- change reference to E
'Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove '<-- line removed as don't need to insert twice

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="New York,Jakarta"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

End Sub

并且有许多相同的列表:

Sub Macro1()
Dim RowNum As Integer
Dim Lists As Integer

'ask user for row to insert data
RowNum = InputBox("Enter Row Number where you want to add a row:", "What Row?")

'insert row
Range("A1").Offset(RowNum, 0).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

'ask how many drop down lists to make
Lists = InputBox("Enter number of drop down lists to make in this row:", "Number?")

i = 0

Do While i < Lists And i < 1000
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="Hong Kong,Rome,Wellington,Cairo"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With

    'move across one cell
    ActiveCell.Offset(0, 1).Range("A1").Select

    i = i + 1
Loop


End Sub

相关内容