扩展 Excel VBA 宏代码以显示数据表中的所有必需信息

问题描述 投票:0回答:1

感谢您的点击并花费您的时间。我会尽力使其简短明了。

以下代码是由一位已离开公司的前员工编写的。我没有VBA编码经验,但我想了解它以更改此代码以扩展宏的用途。

该宏比较通过扫描条形码(商品标签)输入的输入字符串(商品编号)。条形码始终在同一位置包含所需的商品编号 - 为此设置了“限制器”。输入项目编号后,宏将在 Excel 工作表中进行搜索以查找相同的项目编号,该工作表从 SAP B1 查询中提取数据。一个项目编号可以出现在数据中的多行中,并且具有与同一项目相关的不同信息(值)。但目前,我只返回一行,因此只有部分信息,而其他行被忽略。

这是代码:

Public Sub UserForm_Initialize()

limiter_a = "^#01^"
limiter_b = "^#02^"

End Sub


Private Sub suchbutton_Click()

Dim fullstring, searchstring As String
Dim partfound, locationfound, partqtyinfound As String
Dim partnotein, partspecialin, lastbin As String
Dim columnpart, columnlocation, lineqtydetail As String
Dim ergebnis As String
Dim j As Long
Dim lenght_a, lenght_b As Long

columnpart = "L"
columnlocation = "C"
partqtyinfound = "M"
partnotein = "O"
partspecialin = "P"
lastbin = "B"
lineqtydetail = "N"

lenght_a = Len(limiter_a)
lenght_b = Len(limiter_b)

fullstring = SucheTeilenummer.userinput.Value

openPos = InStr(fullstring, limiter_a)
closePos = InStr(fullstring, limiter_b)


If openPos > "0" And closePos > "0" Then
    searchstring = Mid(fullstring, openPos + lenght_a, closePos - openPos - lenght_b)
Else
    ergebnis = "Keine Limiter gefunden"
End If


If ergebnis = "Keine Limiter gefunden" Then

    SucheTeilenummer.partfound = ergebnis
    SucheTeilenummer.locationfound = ""
    SucheTeilenummer.partqtyinfound = ""
    Suche.Teilenummer.partnotein = ""
    Suche.Teilenummer.partspecialin = ""
    Suche.Teilenummer.lastbin = ""
    Suche.Teilenummer.lineqtydetail = ""

Else
'# I suspect the following part is the location where I need to expand the coding..

    letzteZeileTeilenummer = ActiveSheet.Cells(ActiveSheet.Rows.Count, columnpart).End(xlUp).Row
    For j = letzteZeileTeilenummer To 2 Step -1
        
        If Range(columnpart & j).Value = searchstring Then
            SucheTeilenummer.partfound = ActiveSheet.Range(columnpart & j).Value
            SucheTeilenummer.locationfound = ActiveSheet.Range(columnlocation & j).Value
            SucheTeilenummer.partqtyinfound = ActiveSheet.Range(partqtyinfound & j).Value
            SucheTeilenummer.partnotein = ActiveSheet.Range(partnotein & j).Value
            SucheTeilenummer.partspecialin = ActiveSheet.Range(partspecialin & j).Value
            SucheTeilenummer.lastbin = ActiveSheet.Range(lastbin & j).Value
            SucheTeilenummer.lineqtydetail = ActiveSheet.Range(lineqtydetail & j).Value
            
            ergebnis = "Wert gefunden"
            
            Exit For
            
        Else
            ergebnis = "Wert nicht gefunden"
        End If
        
    Next j

End If

        If ergebnis = "Wert nicht gefunden" Then
            SucheTeilenummer.partfound = searchstring
            SucheTeilenummer.locationfound = ""
            SucheTeilenummer.partqtyinfound = ""
            SucheTeilenummer.partnotein = ""
            SucheTeilenummer.partspecialin = ""
            SucheTeilenummer.lastbin = ""
            SucheTeilenummer.lineqtydetail = ""
            
        End If

SucheTeilenummer.userinput.Value = ""
fullstring = ""
ergebnis = ""
SucheTeilenummer.userinput.SetFocus


End Sub

Private Sub userinput_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
    Call suchbutton_Click
    KeyCode = 0
End If

End Sub


Private Sub userinput_change()
If InStr(SucheTeilenummer.userinput.Value, limiter_b) Then
    Call suchbutton_Click
End If

End Sub

我附加的第一张图片显示了输出。

[enter image description here]

商品数量总计为 6 个。但是,由于不同数量的商品特性不同,该商品有 2 行数据。示例:1 个项目设置为 300 Bar 范围,而 5 个项目设置为 325 Bar。这是当这些差异发生时我试图充分考虑和展示的数据。不同的值可以从数字条范围变化,或者只是需要联系相应项目的不同销售员工姓名。一项可以是 2 行,也可以是最多 12 行,相关信息有所不同。

[enter image description here]

我附加的第二张图片是我想要实现的目标。我需要底部的文本框 1. 显示所有相关信息(以某种方式)总结,2. 我需要它改变颜色以引起员工的注意 - 每当有不同的信息时。

我希望能够正确地解释它。

抱歉所有这些文字。

谢谢。

excel vba search macros sapb1
1个回答
0
投票

我在这里猜测一下。
我们需要删除 Exit For 以确保它继续搜索。然后确保数量详细信息与下一个数据合并。

Else
'# I suspect the following part is the location where I need to expand the coding..

    letzteZeileTeilenummer = ActiveSheet.Cells(ActiveSheet.Rows.Count, columnpart).End(xlUp).Row
    For j = letzteZeileTeilenummer To 2 Step -1
        
        If Range(columnpart & j).Value = searchstring Then
            SucheTeilenummer.partfound = ActiveSheet.Range(columnpart & j).Value
            SucheTeilenummer.locationfound = ActiveSheet.Range(columnlocation & j).Value
            SucheTeilenummer.partqtyinfound = ActiveSheet.Range(partqtyinfound & j).Value
            SucheTeilenummer.partnotein = ActiveSheet.Range(partnotein & j).Value
            SucheTeilenummer.partspecialin = ActiveSheet.Range(partspecialin & j).Value
            SucheTeilenummer.lastbin = ActiveSheet.Range(lastbin & j).Value
            SucheTeilenummer.lineqtydetail = SucheTeilenummer.lineqtydetail & VbNewLine & ActiveSheet.Range(lineqtydetail & j).Value & " - " & ActiveSheet.Range(partnotein & j).Value
            ' I'm guessing above that partnotein is the +++ 325 BAR +++, if not replace with where that data is.
            
            ergebnis = "Wert gefunden"
            
            'Exit For
            
        Else
            ergebnis = "Wert nicht gefunden"
        End If
        
    Next j

End If
© www.soinside.com 2019 - 2024. All rights reserved.