【发布时间】:2016-04-07 01:28:53
【问题描述】:
大家好,我是 VBA Excel 的新手,请多多包涵。这是我到目前为止的代码。这是一个具有三个输入的组合框 1.Part# 2. Month (Icol) 3. Amount。我希望我的组合框查看 A 列并查看该部分是否位于如果它不是我想将该部分添加到行的底部。一切正常,除非在 A 列上没有添加新部分。该部分被添加到行的底部,但值部分没有,这必须与用户选择的月份相对应。新部分,“当前月份 +1=N, 200。所以添加新部分并将 200 添加到 N 列。
私有子 cmdAdd_Click()
Dim iRow As Long
Dim lastRow As Long
Dim iCol As String
Dim c As Range
Dim ws As Worksheet
Dim value As Long
Dim NewPart As Boolean
Set ws = Worksheets("sheet2")
Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If c Is Nothing Then
'find first empty row in database
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
iRow = lastRow + 1
NewPart = True
Else
'find row where the part is
iRow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
NewPart = False
End If
'check for a part number
If Trim(Me.PartTextBox.value) = "" Then
Me.PartTextBox.SetFocus
MsgBox "Please Enter A Part Number"
Exit Sub
End If
If Trim(Me.MonthComboBox.value) = "" Then
Me.MonthComboBox.SetFocus
MsgBox "Please Enter A Month"
Exit Sub
End If
If Trim(Me.AddTextBox.value) = "" Then
Me.AddTextBox.SetFocus
MsgBox "Please Enter A Value To Add Or Substract"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
Select Case MonthComboBox.value
Case "Current Month"
iCol = "C"
Case "Current Month +1"
iCol = "N"
Case "Current Month +2"
iCol = "O"
Case "Current Month +3"
iCol = "P"
Case "Current Month +4"
iCol = "Q"
End Select
value = Cells(iRow, iCol).value
If NewPart = True Then
ws.Cells(iRow, "A").value = Me.PartTextBox.value
ws.Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value)
End If
With ws
' .Unprotect Password:="password"
.Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value)
' .Protect Password:="password"
End With
'clear the data
Me.PartTextBox.value = ""
Me.MonthComboBox.value = ""
Me.AddTextBox.value = ""
Me.PartTextBox.SetFocus
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
'Empty NameTextBox
PartTextBox.value = ""
'Empty PhoneTextBox
AddTextBox.value = ""
'Empty DinnerComboBox
'Fill DinnerComboBox
With MonthComboBox
.AddItem "Current Month"
.AddItem "Current Month +1"
.AddItem "Current Month +2"
.AddItem "Current Month +3"
.AddItem "Current Month +4"
End With
End Sub
【问题讨论】: