【问题标题】:Create a list with values from multiple Excel sheets使用来自多个 Excel 工作表的值创建一个列表
【发布时间】:2020-01-07 16:26:24
【问题描述】:

我正在编写将通过工作表“voorblad”中的下拉菜单运行的 VBA 代码。

对于下拉菜单中的每个值,我想复制值“Voorblad”.range“K9”和“Calculation”.range“G35”并将其粘贴到名为“LIST Sheet”的工作表中。

VBA 旨在创建所有值的列表。

Sub CreateList()

Dim Answer As VbMsgBoxResult

Answer = MsgBox("Validation message!", vbYesNoCancel, "CreateList")

If Answer = vbYes Then

    Application.ScreenUpdating = False

    With Sheets("Voorblad").Range("K9").Validation
        For Each rCell In Range(.Formula1)
            .Parent.Value = rCell.Value

            Sheets("Voorblad").Select
            Range("K9").Select
            ActiveCell.Copy
            Sheets("LIST Sheet").Select
            Range("B2").Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Sheets("LIST Sheet").Select
            ActiveCell.Offset(1, 0).Select

            Sheets("Calculation").Select
            Range("G35").Select
            ActiveCell.Copy
            Sheets("LIST Sheet").Select
            Range("G2").Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Sheets("LIST Sheet").Select
            ActiveCell.Offset(1, 0).Select

        Next rCell
        .Parent.Value = ""

    End With

    Application.ScreenUpdating = True

    MsgBox "Export geslaagd! Het PDF is opgeslagen in jouw Documenten"
End If

End Sub

VBA 代码未创建列表。每次宏运行代码时,它都会重新选择 Range("B2") 和 Range("G2") 处的预选单元格。我希望它将值向下粘贴一行。最终这必须创建一个列表。

【问题讨论】:

  • 首先,尽可能avoid select/activate。例如,Sheets("Calculation").Range("G35").Copy insted 选择工作表、单元格,然后复制单元格
  • ^ 在此之上,尝试找到last used row

标签: excel vba


【解决方案1】:

要正确创建列表,您需要确保移动到目标工作表(列表表)上的下一个空单元格。尝试类似

Sheets("LIST Sheet").Range("G2").End(XLdown).Offset(1,0).PasteSpecial Paste:=xlPastValues

这应该将值放在下一个空白单元格中。与 G 列相同

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多