1

我有一个脚本可以检查工作表中的新商店。如果它找到一个新商店,它应该打开一个表单并提示用户选择一个商店类别,然后单击确定。当用户单击确定时,应选择下拉列表中的值,然后表单关闭并继续循环。

表格称为“shopkat”

这是它的工作原理:

For i = LBound(distshops) To UBound(distshops)
        If Not IsEmpty(distshops(i)) Then
            curcell = getrows
            curshop = distshops(i)
            findout = checkifinsheet(curshop)
            If findout = False Then

                Cells(curcell + 1, 1) = curshop
                'show form
                shopkat.Show vbModal
                'shop current shop
                shopkat.shop.Caption = curshop

                'Get value from combo
                Cells(curcell + 1, 2) = shopkat.shopkatcombo.value

                'if user click ok then continue


            End If
        End If
    Next i

谁能帮忙。非常感谢!

//////////////////////////// 更新 ///////////////////// ////////// 模块1:

Public curcell As Long
Dim ws As Worksheet

表格购物:

Private Sub shopkatok_Click()
    If Not shopkat.shopkatcombo.value = "" Then
        ws.Cells(curcell + 1, 2) = shopkat.shopkatcombo.value
        Unload Me
    End If
End Sub

循环表(商店类别)

 Set ws = ThisWorkbook.Sheets("Shopcategories")

    For i = LBound(distshops) To UBound(distshops)
        If Not IsEmpty(distshops(i)) Then
            curcell = getrows()
            curshop = distshops(i)
            findout = checkifinsheet(curshop)

            If findout = False Then


                shopkat.shop.Caption = curshop

                'show form
                shopkat.Show
                If Not IsEmpty(Cells(curcell + 1, 2).value) Then
                    ws.Cells(curcell + 1, 1) = curshop
                End If
            End If
        End If
    Next i
4

1 回答 1

1

好的,这样做。(未经测试

A ) 插入一个模块并粘贴这些行

Public curcell As Long
Dim ws as Worksheet 

B)接下来在用户窗体的Ok按钮中粘贴此代码

Private Sub CommandButton1_Click()
    ws.Cells(curcell + 1, 2) = shopkat.shopkatcombo.Value
    Unload Me
End Sub

C)最后将您的上述代码修改为此

Sub Sample()
    '
    '~~> Rest of code
    '

    '~~> Change this as a applicable
    Set ws = ThisWorkbook.Sheets("Sheet1")

    For i = LBound(distshops) To UBound(distshops)
        If Not IsEmpty(distshops(i)) Then
            curcell = getrows
            curshop = distshops(i)
            findout = checkifinsheet(curshop)

            If findout = False Then
                ws.Cells(curcell + 1, 1) = curshop

                shopkat.shop.Caption = curshop

                'show form
                shopkat.Show '<~~ No need to mention vbModal. It is default
            End If
        End If
    Next i

    '
    '~~> Rest of code
    '
End Sub
于 2013-08-07T12:59:58.087 回答