2013-08-07 54 views
1

我有一個腳本,用於檢查工作表中的新商店。如果它找到一家新店,它應該打開一個表單並提示用戶選擇一個店鋪類別,然後點擊確定。當用戶點擊確定時,應該選擇下拉菜單中的值,然後窗體關閉並繼續循環。VBA - 在循環中打開表格,然後等待OK單擊

形式是卡列斯 「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 

毛圈片(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 
+0

我不明白你的問題。當您以模式打開表單時,代碼的下一行將不會執行,直到表單被卸載。 –

+0

我想我現在明白你想要什麼了......一刻 –

+0

我已經更新了代碼。希望它更容易理解 – Tino

回答

1

好做到這一點。 (UNTESTED

)插入一個模塊並粘貼這些線

Public curcell As Long 
Dim ws as Worksheet 

)接着在用戶窗體的Ok按鈕粘貼此代碼

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

Ç )最後修改你的上面的代碼到這個

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 
+0

感謝您的回答。它不起作用:-)我必須打電話給模塊嗎?它給我一個錯誤,說該對象丟失 – Tino

+0

我已經更新了代碼。查看原文。 – Tino