2013-07-17 169 views
-2

Im Monching,有沒有人可以幫助我解決我的excel vbs代碼,因爲我是這項工作的新手。我需要這個條目是依賴於2 cbo的。Excel 2010中的VBA代碼

當我把我的數據放在用戶表單中時,數據覆蓋了其他數據同樣的單元格 我需要在用戶表單中輸入的金額取決於cboMembers和cboMonth,如果我選擇一個成員和月份,我需要的條目是特定於成員和月份的單元格。

Private Sub cboMembers_Change() 

End Sub 

Private Sub cboMonth_Change() 

End Sub 

Private Sub cmdCancel_Click() 
' Clear the form 
    For Each ctl In Me.Controls 
     If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then 
      ctl.Value = "" 
     ElseIf TypeName(ctl) = "CheckBox" Then 
      ctl.Value = False 
     End If 
    Next ctl 
End Sub 

Private Sub cmdClose_Click() 
    Unload Me 

End Sub 

Private Sub cmdEnter_Click() 
Dim iRow As Long 
Dim ws As Worksheet 
If OptionButton1 Then Set ws = Worksheets("Share") 
If OptionButton2 Then Set ws = Worksheets("Salary") 
If OptionButton3 Then Set ws = Worksheets("Emer") 
If OptionButton4 Then Set ws = Worksheets("Bday") 
If OptionButton5 Then Set ws = Worksheets("Educ") 

On Error Resume Next 

iRow = ws.Cells.Find("*", Range("B2"), xlFormulas, , xlByRows, xlPrevious).Row 
On Error GoTo 0 
iRow = iRow + 0 

' Check user input 
    If Me.cboMembers.Value = "" Then 
     MsgBox "Please choose a Members.", vbExclamation, "UserForm1" 
     Me.cboMembers.SetFocus 
     Exit Sub 
    End If 
    If Me.cboMonth.Value = "" Then 
     MsgBox "Please choose a Month.", vbExclamation, "UserForm1" 
     Me.cboMonth.SetFocus 
     Exit Sub 
    End If 
    If Me.txtAmountPaid.Value = "" Then 
     MsgBox "Please enter an Amount.", vbExclamation, "UserForm1" 
     Me.txtAmountPaid.SetFocus 
     Exit Sub 
    End If 
    If Not IsNumeric(Me.txtAmountPaid.Value) Then 
     MsgBox "The Amount box must contain a number.", vbExclamation, "UserForm1" 
     Me.txtAmountPaid.SetFocus 
     Exit Sub 
    End If 

'copy the data to the database 
    RowCount = Worksheets("Share").Range("B2").CurrentRegion.Rows.Count 
     With Worksheets("Share").Range("B2") 
     .Offset(RowCount, 0).Value = Me.cboMembers.Value 
     .Offset(RowCount, 0).Value = Me.cboMonth.Value 
     .Offset(RowCount, 1).Value = Me.txtAmountPaid.Value 
     End With 

'clear the data 
Me.cboMembers.Value = "" 
Me.cboMonth.Value = "" 
Me.txtAmountPaid.Value = "" 
Me.OptionButton1.Value = "" 
Me.OptionButton2.Value = "" 
Me.OptionButton3.Value = "" 
Me.OptionButton4.Value = "" 
Me.OptionButton5.Value = "" 
End Sub 

Private Sub UserForm_Initialize() 
Dim cmonth As Range 
Dim ws As Worksheet 
Set ws = Worksheets("LookUpEntry") 

For Each cmonth In ws.Range("Month") 
    With Me.cboMonth 
     .AddItem cmonth.Value 
    End With 
Next cmonth 

End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
If CloseMode = vbFormControlMenu Then 
Cancel = True 
MsgBox "Please Use the Close Button!" 
End If 
End Sub 

這裏是我的useform,成員和本月在那裏的形式。當我選擇成員和相應的一個月裏,我想在S賠付金額寫入值太平洋細胞。如果我選擇股份,則支付的金額的價值會自動輸入到股份表中。如果我在A4中選擇會員並在F1中選擇月份,則付款金額的值將在F4中。我需要這個代碼。

我的形式

A2至A234是我的成員

B1到M1是我的月

+1

不清楚你在問什麼。 請澄清您的具體問題或添加其他詳細信息,以突出顯示您的需要。正如目前所寫,很難確切地說出你在問什麼。 – 2013-07-17 08:22:02

+1

你可以[編輯]你的問題,首先讓標題更有意義(你只有標籤信息,根本不屬於這個標籤,並且它沒有提到你問的問題),然後解釋你的問題到底是什麼?發佈一大塊代碼(順便說一下,'VBA',而不是'VBS')是不夠的。我不知道這裏真正的問題是什麼。謝謝。 –

回答

0

我認爲這個問題可能在於這部分代碼:

With Worksheets("Share").Range("B2") 
    .Offset(RowCount, 0).Value = Me.cboMembers.Value 
    .Offset(RowCount, 0).Value = Me.cboMonth.Value 
    .Offset(RowCount, 1).Value = Me.txtAmountPaid.Value 
End With 

你正在將cboMembers中的值複製到單元格(rowCount + 2, 2)中。然後,您將cboMonth的值複製到同一個單元格中 - 覆蓋之前放在那裏的cboMembers的值。

這不是100%清楚你要怎麼做,但這些代碼可以更好地工作:

With Worksheets("Share").Range("B2") 
    .Offset(RowCount, 0).Value = Me.cboMembers.Value 
    .Offset(RowCount, 1).Value = Me.cboMonth.Value 
    .Offset(RowCount, 2).Value = Me.txtAmountPaid.Value 
End With