VBA - 在循环内打开表单然后等待OK单击

时间:2013-08-07 12:49:58

标签: vba excel-vba excel

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

表格是calles“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

表格shopkat:

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

Loop Sheet(Shopcategories)

 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

1 个答案:

答案 0 :(得分:1)

好的,这样做。 (的 UNTESTED

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

Public curcell As Long
Dim ws as Worksheet 

B )在Userform' 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