2017-10-14 13 views
0

私はマクロを初めて使っていますが、どのように動作するか、小さなVBAコードを書くことができるかの基本的な考え方があります。USERFORM VBクエリの中にあらかじめ定義された値を入れてください

私はサンプルのuserformを作成しましたが、私は自分のUSERFORMの下で利用可能なドロップダウンオプションのすべての値を保持するために、別のルックアップシートを維持したくないように、

私が使用したコードを見つけてください。

Private Sub cmdAdd_Click() 
Dim lRow As Long 
Dim lPart As Long 
Dim ws As Worksheet 
Set ws = Worksheets("PartsData") 

'find first empty row in database 
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ 
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 

lPart = Me.cboPart.ListIndex 

'check for a part number 
If Trim(Me.cboPart.Value) = "" Then 
    Me.cboPart.SetFocus 
    MsgBox "Please enter a part number" 
    Exit Sub 
End If 

'copy the data to the database 
'use protect and unprotect lines, 
'  with your password 
'  if worksheet is protected 
With ws 
' .Unprotect Password:="password" 
    .Cells(lRow, 1).Value = Me.cboPart.Value 
    .Cells(lRow, 2).Value = Me.cboPart.List(lPart, 1) 
    .Cells(lRow, 3).Value = Me.cboLocation.Value 
    .Cells(lRow, 4).Value = Me.txtDate.Value 
    .Cells(lRow, 5).Value = Me.txtQty.Value 
' .Protect Password:="password" 
End With 

'clear the data 
Me.cboPart.Value = "" 
Me.cboLocation.Value = "" 
Me.txtDate.Value = Format(Date, "Medium Date") 
Me.txtQty.Value = 1 
Me.cboPart.SetFocus 

End Sub 

Private Sub cmdClose_Click() 
    Unload Me 
End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 

    If CloseMode = vbFormControlMenu Then 
    Cancel = True 
    MsgBox "Please use the Close Form button!" 
    End If 
End Sub 

Private Sub UserForm_Initialize() 
Dim cPart As Range 
Dim cLoc As Range 
Dim ws As Worksheet 
Set ws = Worksheets("LookupLists") 

For Each cPart In ws.Range("PartIDList") 
    With Me.cboPart 
    .AddItem cPart.Value 
    .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value 
    End With 
Next cPart 

**For Each cLoc In ws.Range("LocationList") 
    With Me.cboLocation 
    .AddItem cLoc.Value 
    End With 
Next cLoc** 

Me.txtDate.Value = Format(Date, "Medium Date") 
Me.txtQty.Value = 1 
Me.cboPart.SetFocus 

End Sub 

I learn how to set this userfrom from a web site and you can use this link to download the sample excel file (posted on that website)

親切にこの上で私を助けて。

+0

あなたは既にこの行で制御値を設定しています。 'Me.cboPart.Value =" "' ....同じ種類のコードを使って空白でない値を設定してください。 – jsotola

答えて

1

があなたの代わりに書面で、UserForm_Initializeサブでは、いくつかの定義済みの場所を追加したいとしましょう、事前に

おかげで...

For Each cLoc In ws.Range("LocationList") 
    With Me.cboLocation 
    .AddItem cLoc.Value 
    End With 
Next cLoc 

...

With Me.cboLocation 
    .AddItem "Location 1" 
    .AddItem "Location 2" 
    .AddItem "Location 3" 
    'Keep going as many as you like 
End With 
に変更します
+0

Rosettaに感謝します。あなたがもう一つ助けてくれれば、これらの値を使って私が持っているExcelファイルを保存することは可能ですか?あなたがこれで私を助けることができればそれは素晴らしいことでしょう。 –

+0

コンボボックスの選択された場所をExcelスプレッドシートの範囲に入れるには、A1セルというのはRange( "A1")= me.cboLocation.value' – Rosetta

+0

です。ありがとう、しかし、私たちがユーザーフォームを使ってキャプチャした値、たとえば私が作成したUserFomrから 'cboLocation'の下の値を選択し、ボタンを追加します。ユーザーがコードをSUB Try1()であるMODULE 1をクリックすると、Dim location location = cbolocation MsgBoxロケーションEND SUB'。私はこれを試しましたが、msgboxの下でnull値を得ています。 –

関連する問題