gpt4 book ai didi

vba - Excel VBA 使用外部宏创建嵌入宏

转载 作者:行者123 更新时间:2023-12-02 16:44:01 24 4
gpt4 key购买 nike

我有一个宏文件,每周用来编辑和格式化一百个 Excel 文件,然后将其发送出去。我希望向发送的文件添加一些更复杂的功能。

发出的每个文件都需要具有类似于以下内容的代码:

Option Explicit

Sub DropDown4_Change()
With ThisWorkbook.Sheets("ExampleData").Shapes("Drop Down 4").ControlFormat
Select Case .List(.Value)
Case "Value1": SelectValue1
Case "Value2": SelectValue2
Case "Value3": SelectValue3
Case "Value4": SelectValue4
Case "Value5": SelectValue5
Case "Value6": SelectValue6
Case "Value7": SelectValue7
Case "Value8": SelectValue8
End Select
End With
End Sub

Sub SelectValue1()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=2, Criteria1:="<>"
End Sub

Sub SelectValue2()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=3, Criteria1:="<>"
End Sub

Sub SelectValue3()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=4, Criteria1:="<>"
End Sub

Sub SelectValue4()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=5, Criteria1:="<>"
End Sub

Sub SelectValue5()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=6, Criteria1:="<>"
End Sub

Sub SelectValue6()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=7, Criteria1:="<>"
End Sub

Sub SelectValue7()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=8, Criteria1:="<>"
End Sub

Sub SelectValue8()
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=9, Criteria1:="<>"
End Sub

这是基于组合框选择的基本过滤。我的外部宏中需要什么代码才能让它在运行的每个 Excel 文件中写入此代码?这可能吗?

最佳答案

请注意,除非我遗漏了某些内容,否则您可以通过一些小调整大大减少该代码的大小:

Option Explicit

Sub DropDown4_Change()
Dim fieldVal As Long

With ThisWorkbook.Sheets("ExampleData").Shapes("Drop Down 4").ControlFormat
Select Case .List(.Value)
Case "Value1": fieldVal = 2
Case "Value2": fieldVal = 3
Case "Value3": fieldVal = 4
Case "Value4": fieldVal = 5
Case "Value5": fieldVal = 6
Case "Value6": fieldVal = 7
Case "Value7": fieldVal = 8
Case "Value8": fieldVal = 9
End Select
End With
Call SelectValue(fieldVal)
End Sub

Sub SelectValue(myVal As Long)
ActiveSheet.ListObjects("Table4").Range.AutoFilter
ActiveSheet.ListObjects("Table4").Range.AutoFilter Field:=myVal, Criteria1:="<>"
End Sub

关于vba - Excel VBA 使用外部宏创建嵌入宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com