0

宏的目的是根据我的“推荐”表上的下拉菜单中的所选案例选择正确的数据。

这是我的代码:

Private Sub cmbNatCoList_Change()

'Define selected NatCo and variables for loop
Dim NatCoList As String
Dim ZeileUntersucht As Integer
Dim ZeileEintragen As Integer

'Line which is looked at in NatCo sheet
ZeileUntersucht = 17
'Line which is written in in Recommendation Sheet
ZeileEintragen = 15

'Selected NatCo
NatCoList = cmbNatCoList.Value

'Set selected NatCo on NatCoRecommendations sheet
'ThisWorkbook.Sheets("NatCoRecommendations").cmbNatCoList.Value = NatCoList

'Set position and size of combobox
cmbNatCoList.Top = 85
cmbNatCoList.Left = 1120
cmbNatCoList.Width = 100
cmbNatCoList.Height = 40

'Write selected NatCo on charts
Range("z1") = NatCoList
Worksheets("Stock Control Chart").Range("C61") = NatCoList
Worksheets("Price Chart").Range("D44") = NatCoList

'Search for recommendations for selected NatCo
 Select Case NatCoList

'========================================
Case Is = "TDG"
Worksheets("Recommendations").Rows("1:224").Hidden = False
    'Loop looking for recommendations in each line, writes BNCode in list if yes 
and continues with next line, if not just continues
For ZeileUntersucht = 20 To 515
    If Worksheets("TDG").Cells(ZeileUntersucht, 238).Value = "yes" Then
        Worksheets("Recommendations").Cells(ZeileEintragen, 3) = Worksheets("TDG").Cells(ZeileUntersucht, 1)
        ZeileEintragen = ZeileEintragen + 1

    End If
Next ZeileUntersucht




'===========================================
Case Is = "TMNL"
Worksheets("Recommendations").Rows("1:263").Hidden = False
    For ZeileUntersucht = 20 To 515
        If Worksheets("TMNL").Cells(ZeileUntersucht, 238).Value = "yes" Then
            Worksheets("Recommendations").Cells(ZeileEintragen, 3) = 
Worksheets("TMNL").Cells(ZeileUntersucht, 1)
            ZeileEintragen = ZeileEintragen + 1

    End If
Next ZeileUntersucht

这是我试图改进的部分代码。

这些案例是案例总共大约 20 个,我在这里只向您展示两个。里面的代码完全一样,唯一会随着所选案例的变化而变化的是它正在查看的工作表。

需要注意的重要事项:始终只为宏选择(使用)一种情况。

如果您需要更多信息,请询问。

4

1 回答 1

0

我注意到,在第一个案例中,您将隐藏设置为 false 的建议中的行数已更改为 1:224,第二个为 1:263,但如果这应该是相同的,那么类似以下内容将做,只需将您的整个 Select Case 替换为以下内容:

Worksheets("Recommendations").Rows("1:224").Hidden = False
    For ZeileUntersucht = 20 To 515
        If Worksheets(NatCoList).Cells(ZeileUntersucht, 238).Value = "yes" Then
            Worksheets("Recommendations").Cells(ZeileEintragen, 3) = Worksheets(NatCoList).Cells(ZeileUntersucht, 1)
            ZeileEintragen = ZeileEintragen + 1
        End If
    Next ZeileUntersucht
于 2017-12-20T11:04:34.463 回答