简体   繁体   中英

Excel VBA user-form update button

Update button works if there is value in cells beside the target cell can not find the problem in this code.

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

End With End Sub

if Target cell is AA2 then there should be value in Z2 or B2 otherwise nothing happens.

Just added ELSE statement but with else its updating value in Column AB not in AA

形式

I did that just on basis of hit n trial and it works

   `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`

You can adapt the following codes :

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

Userform codes (data insert, delete, update, search by column data, listbox with 15 columns, data copy, progressbar) ,you can review from this source .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM