If only run first line and skip the others

Capa7md

New Member
Joined
Aug 21, 2023
Messages
11
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi i want ti edit my data by using if and when i run the code it just go to first line and skip the others

Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim NewRow As Long
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For NewRow = 3 To LastRow
If Sheet1.Cells(NewRow, 1) = CInt(Me.TextBox6) Then
Sheet1.Cells(NewRow, 2) = Me.TextBox1
Sheet1.Cells(NewRow, 3) = Me.TextBox2
End If
Next NewRow



End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Just a guess

VBA Code:
Private Sub CommandButton1_Click()
    Dim LastRow As Long
    Dim NewRow As Long
    
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For NewRow = 3 To LastRow
    If Sheet1.Cells(NewRow, 1) = CInt(Me.TextBox6) Then
        Sheet1.Cells(NewRow, 2) = Me.TextBox1
        Sheet1.Cells(NewRow, 3) = Me.TextBox2
        Exit For
    End If
Next NewRow

End Sub
 
Upvote 0
Just a guess

VBA Code:
Private Sub CommandButton1_Click()
    Dim LastRow As Long
    Dim NewRow As Long
   
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For NewRow = 3 To LastRow
    If Sheet1.Cells(NewRow, 1) = CInt(Me.TextBox6) Then
        Sheet1.Cells(NewRow, 2) = Me.TextBox1
        Sheet1.Cells(NewRow, 3) = Me.TextBox2
        Exit For
    End If
Next NewRow

End Sub
Yes this is my code but my problem is the code only run the first line in if and skep the second line if the condition was true
 
Upvote 0
Sorry, I misunderstood what you're asking.

what is the value in cells(NewRow,1) and what is the value in TextBox6 ?
 
Upvote 0
Sorry, I misunderstood what you're asking.

what is the value in cells(NewRow,1) and what is the value in TextBox6 ?
The value in cells(NewRow,1) is number by using row() and text 6 also serial number using row() . The reason of the code to find the row to edit data in it i camper the vlaue in text6 which came from listbox by clicking and gave the serial number
 
Upvote 0
Set a break point on the If line and the code will stop there when you run it.
Now execute each line one at a time with the F8 key and verify that Excel is seeing the two values as equal and is continuing on to the other lines
 
Upvote 0
Set a break point on the If line and the code will stop there when you run it.
Now execute each line one at a time with the F8 key and verify that Excel is seeing the two values as equal and is continuing on to the other lines
I actually did that and vba code just do the first line of if and skip all sub
 
Upvote 0
Sure sounds to me that the IF statement is FALSE.
But maybe I don't know which line you are referring to as the first line.
Is the IF statement not being executed?
 
Upvote 0
VBA Code:
If Sheet1.Cells(NewRow, 1) = CInt(Me.TextBox6)
Both will have to be the data type Long or you may get False.
 
Upvote 0
NewRow is declared as Long and TextBox6 is coerced to Integer.
Not exactly the same data types but works on my Excel 2010.
I may be mistaken but think Excel converts Integer to Long internally since Excel 2007.
Probably should have changed CInt to CLng regardless.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,972
Members
452,540
Latest member
haasro02

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