Textbox value updates sheet without changing the other cells

Jayliam

New Member
Joined
May 12, 2015
Messages
31
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Need some guidance. I am using a textbox to update adjacent cells based on the value in column A. Everything works except if there is data in the other cells it deletes it. How can I update specific textboxes without deleting the other data? Below is the code I have so far.

Option Explicit

Private Sub UPDATE_NCP_Click()
Dim m As Variant
Dim iRow As Long
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range
Set ws = Worksheets("DATALOG")

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row

If Len((Me.TextBox1.Value)) > 0 Then

m = Application.Match(Val(Me.TextBox1.Value), ws.Range("A:A"), False)
If Not IsError(m) Then
m = CLng(m)

With ws

.Cells(iRow, 17).Value = Me.TextBox2.Value
.Cells(iRow, 19).Value = Me.TextBox3.Value
.Cells(iRow, 20).Value = Me.TextBox4.Value
.Cells(iRow, 24).Value = Me.TextBox5.Value
.Cells(iRow, 25).Value = Me.TextBox6.Value
.Cells(iRow, 26).Value = Me.TextBox7.Value
.Cells(iRow, 27).Value = Me.TextBox8.Value
.Cells(iRow, 30).Value = Me.TextBox9.Value
.Cells(iRow, 23).Value = Me.TextBox10.Value
.Cells(iRow, 29).Value = Me.TextBox11.Value
.Cells(iRow, 32).Value = Me.TextBox12.Value
.Cells(iRow, 18).Value = Me.ComboBox1.Value

End With

Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
Me.TextBox8.Value = ""
Me.TextBox9.Value = ""
Me.TextBox10.Value = ""
Me.TextBox11.Value = ""
Me.TextBox12.Value = ""
Me.ComboBox1.Value = ""

Else
MsgBox "NCP NUMBER: " & Me.TextBox1 & Chr(10) & "Record Not Found", 48, "Not Found"
Me.TextBox1.SetFocus
End If

End If

End Sub

Private Sub ComboBox1_DropButtonClick()
With Me.ComboBox1
.List = Worksheets("Failure_Modes").Range("A2", Worksheets("Failure_Modes").Cells(Rows.Count, "A").End(xlUp)).Value
.ListRows = Application.WorksheetFunction.Min(6, .ListCount)
.DropDown
End With
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,
try this update to the code & see if does what you want

VBA Code:
Private Sub UPDATE_NCP_Click()
    Dim m           As Variant, Search As Variant
    Dim i           As Long, c As Long
    Dim ws          As Worksheet
   
    Search = Me.TextBox1.Value
    If Len(Search) = 0 Then Exit Sub
    If IsNumeric(Search) Then Search = Val(Search)
   
    Set ws = ThisWorkbook.Worksheets("DATALOG")
   
    m = Application.Match(Search, ws.Range("A:A"), False)
    If Not IsError(m) Then
        m = CLng(m)
       
        For i = 1 To 12
            c = Choose(i, 17, 19, 20, 24, 25, 26, 27, 30, 23, 29, 32, 18)
            With Me.Controls(IIf(i = 12, "ComboBox1", "TextBox" & i + 1))
              If Len(.Value) > 0 Then ws.Cells(CLng(m), c).Value = .Value
                .Value = ""
            End With
        Next i
       
        MsgBox "NCP NUMBER: " & Search & Chr(10) & "Record Updated", 64, "Updated"
       
    Else

        MsgBox "NCP NUMBER: " & Search & Chr(10) & "Record Not Found", 48, "Not Found"

    End If
     Me.TextBox1.SetFocus
End Sub

Dave
 
Upvote 0
Hi,
try this update to the code & see if does what you want

VBA Code:
Private Sub UPDATE_NCP_Click()
    Dim m           As Variant, Search As Variant
    Dim i           As Long, c As Long
    Dim ws          As Worksheet
  
    Search = Me.TextBox1.Value
    If Len(Search) = 0 Then Exit Sub
    If IsNumeric(Search) Then Search = Val(Search)
  
    Set ws = ThisWorkbook.Worksheets("DATALOG")
  
    m = Application.Match(Search, ws.Range("A:A"), False)
    If Not IsError(m) Then
        m = CLng(m)
      
        For i = 1 To 12
            c = Choose(i, 17, 19, 20, 24, 25, 26, 27, 30, 23, 29, 32, 18)
            With Me.Controls(IIf(i = 12, "ComboBox1", "TextBox" & i + 1))
              If Len(.Value) > 0 Then ws.Cells(CLng(m), c).Value = .Value
                .Value = ""
            End With
        Next i
      
        MsgBox "NCP NUMBER: " & Search & Chr(10) & "Record Updated", 64, "Updated"
      
    Else

        MsgBox "NCP NUMBER: " & Search & Chr(10) & "Record Not Found", 48, "Not Found"

    End If
     Me.TextBox1.SetFocus
End Sub

Dave
Dave,

It worked perfectly. Thank you for your assistance.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top