Excel VBA高级编程 - 根据关键字自动搜索,自动生成下拉菜单

关注微信公众号:万能的Excel,回复关键词【下拉菜单】获取Excel源文件

功能说明:

因为工作需要,每一次都要从SAP查找物料信息,手动生成物料清单(Boom表),繁琐且容易出错。

使用VBA实现了如下功能:

1、根据关键字,自动检索符合条件的产品信息

2、自动生成下拉菜单

3、选定物料名称,其他产品信息将自动对应输入

1

 

附件代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim whereStr$, sql$, conn, mr%, j%, k%, l%, n%
Dim i As Long, w1 As String
    j = Target.Row
    On Error Resume Next
    k = Application.WorksheetFunction.Match(Sheet6.Cells(Target.Row, 3), Sheet2.Range("D1:D103"), 0)
    l = Application.WorksheetFunction.Match(Sheet6.Cells(Target.Row, 2), Sheet2.Range("C1:C103"), 0)
    n = Application.WorksheetFunction.Match(Sheet6.Cells(Target.Row, 1), Sheet2.Range("b1:b103"), 0)
    If k > 0 And l = 0 Then
            Cells(Target.Row, 2) = Application.WorksheetFunction.Index(Sheet2.Range("C:C"), k)
    ElseIf k > 0 And l > 0 And n = 0 Then
        Cells(Target.Row, 1) = Application.WorksheetFunction.Index(Sheet2.Range("B:B"), k)
        
    ElseIf Target.Count = 1 And Not Intersect(Range("A3:C999"), Target) Is Nothing Then
        whereStr = whereStr & IIf(Cells(j, 1) = "", "", " and [Manufacturer] like '%" & Cells(j, 1) & "%'")
        whereStr = whereStr & IIf(Cells(j, 2) = "", "", " and [ID] like '%" & Cells(j, 2) & "%'")
        whereStr = whereStr & IIf(Cells(j, 3) = "", "", " and [Type] like '%" & Cells(j, 3) & "%'")
        mr = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
        If mr > 2 Then Sheet5.Range("A3:G" & mr).Clear
        If whereStr <> "" Then
            Set conn = CreateObject("ADODB.connection")
            conn.Open "Provider=Microsoft.Ace.oledb.12.0;extended properties='excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
            sql = "select * from [产品库$B6:D] where" & Mid(whereStr, 5)
            [Search!A3].CopyFromRecordset conn.Execute(sql)
            conn.Close
            Set conn = Nothing
        End If
        
    End If


    w1 = ""


    With Sheet6


        ''首先创建下拉列表数据
        n = Sheet5.Range("c1").End(xlDown).Row()


        For i = 3 To n Step 1


            w1 = w1 & IIf(w1 <> "", ",", "")


            w1 = w1 & Trim$(Sheet5.Cells(i, 3))


        Next


        ''添加数据有效性
        


        With .Cells(j, 3).Validation
    
            .Delete
            
                If w1 <> "" And k = 0 Then
    
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=w1
        
                    .InCellDropdown = True
                    
                End If
    
        End With


    End With
    
End Sub

关注微信公众号:万能的Excel,回复关键词【下拉菜单】获取Excel源文件

 

 

 

 

  • 8
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Excel VBA(Visual Basic for Applications)是一种用于在Excel中编写宏和自定义功能的编程语言。通过使用VBA,您可以自动化执行各种任务,包括生成通报。 要自动生成通报,您可以按照以下步骤进行操作: 1. 打开Excel并创建一个新的工作簿。 2. 按下Alt + F11打开VBA编辑器。 3. 在VBA编辑器中,插入一个新的模块。 4. 在模块中编写VBA代码来生成通报。这可能包括从其他工作表或数据源中提取数据,进行计算和分析,并将结果填充到通报模板中。 5. 在VBA代码中使用Excel对象模型来操作工作表、单元格和其他Excel元素。 6. 编写适当的逻辑和条件语句来处理数据和生成通报的不同部分。 7. 运行VBA代码以生成通报。 以下是一个简单的示例代码,用于生成一个包含日期、姓名和内容的通报: ```vba Sub GenerateReport() Dim ws As Worksheet Dim reportSheet As Worksheet Dim lastRow As Long '设置工作表 Set ws = ThisWorkbook.Sheets("数据源") Set reportSheet = ThisWorkbook.Sheets("通报") '找到最后一行 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row '循环遍历数据源并生成通报 For i = 2 To lastRow '获取数据 Dim dateValue As Date Dim nameValue As String Dim contentValue As String dateValue = ws.Cells(i, 1).Value nameValue = ws.Cells(i, 2).Value contentValue = ws.Cells(i, 3).Value '将数据填充到通报模板中 reportSheet.Cells(i, 1).Value = dateValue reportSheet.Cells(i, 2).Value = nameValue reportSheet.Cells(i, 3).Value = contentValue Next i '清除多余的行 reportSheet.Rows(i & ":" & reportSheet.Rows.Count).ClearContents '显示完成消息 MsgBox "通报已生成!" End Sub ``` 您可以根据自己的需求修改和扩展此示例代码,以满足您的具体要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值