Excel VBA用户表单更新按钮

时间:2016-06-01 20:28:22

标签: excel vba excel-vba

如果目标单元格旁边的单元格中有值,则

更新按钮有效 在此代码中找不到问题。

Private Sub CommandButton3_Click()
Dim irow As Long, _
wS As Worksheet, _
NextRow As Long, _
cF As Range
Set wS = Worksheets("stock")
With wS
    With .Range("A:A")

        Set cF = .Find(What:=Me.ComboBox2.value, _
                After:=.Cells(1, 1), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)
    End With

 If Not cF Is Nothing Then
    If cF.Offset(0, 1) <> vbNullString Then
        Set cF = cF.End(xlToRight).Offset(0, 25)
        cF.value = Me.TextBox2.value + .Cells(cF.row, "AA").value
    End If
 Else
   .Cells(cF.row, "AA").value = Me.TextBox2.value + .Cells(cF.row,   "AA").value
 End If

结束  结束子

如果目标细胞是AA2,则Z2或B2应该有值,否则没有任何反应。

刚刚添加了ELSE语句,但是其中的更新值在列AB中不在AA中

form

2 个答案:

答案 0 :(得分:0)

我只是根据命中试验做到了这一点

   `Private Sub CommandButton3_Click()
    Dim irow As Long, _
    wS As Worksheet, _
    NextRow As Long, _
   cF As Range
   Set wS = Worksheets("stock")
   With wS
   With .Range("A:A")

    Set cF = .Find(What:=Me.ComboBox2.value, _
            After:=.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)

 .Cells(cF.row, "AA").value = Me.TextBox2.value + .Cells(cF.row,   "AA").value
 End With
 End Sub`

答案 1 :(得分:0)

您可以修改以下代码:

Private Sub CommandButton2_Click() 'Change Button
Dim sonsat As Long

If ListBox1.ListIndex = -1 Then
MsgBox "Choose an item", vbExclamation, ""
Exit Sub
End If
lastrow = Sheets("Data").Cells(Rows.count, 1).End(xlUp).Row
Sheets("Data").Range("A2:A" & lastrow).Find(What:=ListBox1.Text, LookIn:=xlValues, LookAt:=xlWhole).Activate
sonsat = ActiveCell.Row
Cells(sonsat, 1) = TextBox1.Text
Cells(sonsat, 2) = TextBox2.Text
Cells(sonsat, 3) = TextBox3.Text
Cells(sonsat, 4) = TextBox4.Text
Cells(sonsat, 5) = TextBox5.Text
Cells(sonsat, 6) = TextBox6.Text
Cells(sonsat, 7) = TextBox7.Text
Cells(sonsat, 8) = TextBox8.Text
Cells(sonsat, 9) = TextBox9.Text
Cells(sonsat, 10) = TextBox10.Text
Cells(sonsat, 11) = TextBox11.Text
Cells(sonsat, 12) = TextBox12.Text
Cells(sonsat, 13) = TextBox16
Cells(sonsat, 14) = TextBox17
Cells(sonsat, 15) = TextBox18

Call Main 'Progress Bar
MsgBox "Item has been updated", vbApplicationModal, ""
ListBox1.List = Sheets("Data").Range("A2:O" & Sheets("Data").Cells(Rows.count, 1).End(xlUp).Row).Value
End Sub

用户代码(数据插入,删除,更新,按列数据搜索,具有15列的列表框,数据复制,进度条),您可以从此source中进行查看。