VBA code not finding value in Textbox1.

Jayliam

New Member
Joined
May 12, 2015
Messages
31
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
The code I am using is not searching for the value in textbox1. It goes to the last found value and entering the data.

Can someone please review my code and assist?

VBA Code:
Private Sub CommandButton1_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")
Dim n As Long
'n = ws.Range("A" & Application.Rows.Count).End(xlUp).Row
'ws.Range("A" & n + 1).Value = Val(Me.TextBox1.Value)

    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, 42).Value = Me.TextBox2.Value
                 .Cells(iRow, 45).Value = Me.TextBox3.Value
                 .Cells(iRow, 48).Value = Me.TextBox4.Value
                 .Cells(iRow, 51).Value = Me.TextBox5.Value
                 .Cells(iRow, 43).Value = Me.TextBox6.Value
                 .Cells(iRow, 46).Value = Me.TextBox7.Value
                 .Cells(iRow, 49).Value = Me.TextBox8.Value
                 .Cells(iRow, 52).Value = Me.TextBox9.Value
                 .Cells(iRow, 44).Value = Me.TextBox11.Value
                 .Cells(iRow, 47).Value = Me.TextBox12.Value
                 .Cells(iRow, 50).Value = Me.TextBox13.Value
                 .Cells(iRow, 53).Value = Me.TextBox14.Value
                 .Cells(iRow, 18).Value = Me.TextBox15.Value
                 .Cells(iRow, 19).Value = Me.TextBox16.Value
                 .Cells(iRow, 20).Value = Me.TextBox17.Value
                 .Cells(iRow, 24).Value = Me.TextBox18.Value
                 .Cells(iRow, 27).Value = Me.TextBox19.Value
                 .Cells(iRow, 30).Value = Me.TextBox20.Value
                 .Cells(iRow, 25).Value = Me.TextBox21.Value
                 .Cells(iRow, 28).Value = Me.TextBox22.Value
                 .Cells(iRow, 31).Value = Me.TextBox23.Value
                 .Cells(iRow, 26).Value = Me.TextBox24.Value
                 .Cells(iRow, 29).Value = Me.TextBox25.Value
                 .Cells(iRow, 32).Value = Me.TextBox26.Value
                 .Cells(iRow, 21).Value = Me.TextBox27.Value
                 .Cells(iRow, 22).Value = Me.TextBox28.Value
                 .Cells(iRow, 23).Value = Me.TextBox29.Value
                 .Cells(iRow, 33).Value = Me.TextBox30.Value
                 .Cells(iRow, 36).Value = Me.TextBox31.Value
                 .Cells(iRow, 39).Value = Me.TextBox32.Value
                 .Cells(iRow, 34).Value = Me.TextBox33.Value
                 .Cells(iRow, 37).Value = Me.TextBox34.Value
                 .Cells(iRow, 40).Value = Me.TextBox35.Value
                 .Cells(iRow, 35).Value = Me.TextBox36.Value
                 .Cells(iRow, 38).Value = Me.TextBox37.Value
                 .Cells(iRow, 41).Value = Me.TextBox38.Value
                 .Cells(iRow, 54).Value = Me.TextBox39.Value
                 .Cells(iRow, 57).Value = Me.TextBox40.Value
                 .Cells(iRow, 58).Value = Me.TextBox41.Value
                 .Cells(iRow, 71).Value = Me.TextBox42.Value
                 .Cells(iRow, 72).Value = Me.TextBox43.Value
                 .Cells(iRow, 73).Value = Me.TextBox44.Value
                 .Cells(iRow, 74).Value = Me.TextBox45.Value
                 .Cells(iRow, 75).Value = Me.TextBox46.Value
                 .Cells(iRow, 76).Value = Me.TextBox47.Value
                 .Cells(iRow, 77).Value = Me.TextBox48.Value
                 .Cells(iRow, 69).Value = Me.TextBox49.Value
                 .Cells(iRow, 70).Value = Me.TextBox50.Value
                 .Cells(iRow, 55).Value = Me.TextBox51.Value
                 .Cells(iRow, 56).Value = Me.TextBox52.Value
                 .Cells(iRow, 78).Value = Me.TextBox53.Value
                 .Cells(iRow, 79).Value = Me.TextBox54.Value
                 .Cells(iRow, 80).Value = Me.TextBox55.Value
                 .Cells(iRow, 81).Value = Me.TextBox56.Value
                 .Cells(iRow, 82).Value = Me.TextBox57.Value
                 .Cells(iRow, 83).Value = Me.TextBox58.Value
                 .Cells(iRow, 84).Value = Me.TextBox59.Value
                 .Cells(iRow, 85).Value = Me.TextBox60.Value
                 .Cells(iRow, 86).Value = Me.TextBox61.Value
                 .Cells(iRow, 87).Value = Me.TextBox62.Value
                 .Cells(iRow, 88).Value = Me.TextBox63.Value
                 .Cells(iRow, 89).Value = Me.TextBox64.Value
                 .Cells(iRow, 90).Value = Me.TextBox65.Value
                 .Cells(iRow, 91).Value = Me.TextBox66.Value
                 .Cells(iRow, 92).Value = Me.TextBox67.Value
                 .Cells(iRow, 93).Value = Me.TextBox68.Value
                 .Cells(iRow, 94).Value = Me.TextBox69.Value
                 .Cells(iRow, 59).Value = Me.TextBox70.Value
                 .Cells(iRow, 61).Value = Me.TextBox71.Value
                 .Cells(iRow, 62).Value = Me.TextBox72.Value
                 .Cells(iRow, 63).Value = Me.TextBox73.Value
                 .Cells(iRow, 64).Value = Me.TextBox74.Value
                 .Cells(iRow, 60).Value = Me.TextBox75.Value
                 .Cells(iRow, 65).Value = Me.TextBox76.Value
                 .Cells(iRow, 66).Value = Me.TextBox77.Value
                 .Cells(iRow, 67).Value = Me.TextBox78.Value
                 .Cells(iRow, 68).Value = Me.TextBox79.Value
                 .Cells(iRow, 15).Value = Me.TextBox80.Value
                 .Cells(iRow, 16).Value = Me.TextBox81.Value
                
                
            End With
 
            Else
                 MsgBox "SALE ORDER: " & Me.TextBox1 & Chr(10) & "Record Not Found", 48, "Not Found"
                 Me.TextBox1.SetFocus
        End If

End If
    
End Sub
Private Sub CommandButton2_Click()
  Unload Me
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

try changing this line

VBA Code:
m = CLng(m)

to this

VBA Code:
iRow = CLng(m)

and see if this does what you want

Dave
 
Upvote 0
Solution
welcome - appreciate the feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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