ElseIf not working.

StvOne

New Member
Joined
Mar 20, 2017
Messages
18
Need help please. I cant seem to make the ElseIf line work correctly. I scan into cell A1 and next row and correct columns are populated. Its when I try and scan again, supposed to find value in column A. If already there then time stamp goes into column F. If column A and F on same row have values in them then a new row is started. This last bit I haven't written yet because I have not corrected the column F portion.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FindString As String                                                                            
Dim Rng As Range
Dim bcr As Range
Dim nr As Long
Dim nc As Long
Dim n As Long
Dim irow As Long
Dim Standard As String
Dim Description As String
Dim Response As Integer
Set ws = Worksheets("Report")
    If Target <> Range("A1") Then
        MsgBox ("You can only scan Barcodes into Sheet3 range A1")
        ActiveCell.ClearContents
        Range("A1").Select
    End If
        If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
        If Target = "" Then Exit Sub
        With Application
        .EnableEvents = False
        .ScreenUpdating = False
    
    'Find cell A1 value in column B and populate sheet 4 report
            FindString = Sheets("StandardTable").Range("A1")
            If Trim(FindString) <> "" Then
            With Sheets("StandardTable").Range("B2:B500")
            Set Rng = .Find(What:=FindString, _
                After:=.Cells(.Cells.Count), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
            End With
            End If
     'Collect Standard Information.
                If Rng Is Nothing Then Response = MsgBox("Do You Want To Add Standard?", vbYesNo, "Add Standard")
                    
                    If Response = vbYes Then
                    'End If
                    Standard = InputBox("Enter Standard ID.", "MPD Number")
                    With Sheets("StandardTable")
                    NextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range("C" & NextRow) = Standard
                End With
                    Description = InputBox("Enter Standard Description.", "Description")
                    With Sheets("StandardTable")
                    NextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range("D" & NextRow) = Description
                End With
                    ID = InputBox("Scan RFID Tag Now.", "Standards RFID Tag")
                    With Sheets("StandardTable")
                    NextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range("B" & NextRow) = ID
                End With
                    MsgBox "Scan Standard RFID Again", vbOKOnly
                    UserForm1.Show
                End If
                
                    If Response = vbNo Then
                    MsgBox "Standard Not Found"
                End If
         '      Code works up to here as expected!!!!!!!!
      
    'Find A1 value from Standards Table in Report column A and populate columns ID, description and Dates to Report
        With ws
        irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 0
        On Error Resume Next
        ws.Cells(irow, 1).Value = Application.Index(Range("C2:C500"), Application.Match(Range("A1").Value, Range("B2:B500"), 0))
        ws.Cells(irow, 2).Value = Application.Index(Range("D2:D500"), Application.Match(Range("A1").Value, Range("B2:B500"), 0))
        On Error GoTo 0
        End With
            n = Application.CountIf(ws.Columns(1), Target.Value)
            If n = 0 Then
            nr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 0
            If nr = 2 And ws.Range("A2") = vbEmpty Then
            nr = 1
        End If
            ws.Range("A" & nr) = ws.Cells(irow, 1).Value
            ws.Range("B" & nr) = ws.Cells(irow, 2).Value
            ws.Range("E" & nr) = Now()
            ws.Range("E" & nr).NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"
            
                ElseIf n > 0 Then
                nr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 0
                If ws.Cells(nr, 5).Value = Target.Value Then
                nc = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1
                ws.Cells(5, nc) = Now()
                ws.Cells(5, nc).NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"
                'End If
                    Else
                    Set bcr = ws.Columns(1).Find(Target.Value, LookAt:=xlWhole)
                    nc = ws.Cells(bcr.Row, Columns.Count).End(xlToLeft).Column + 1
                    ws.Cells(bcr.Row, nc) = Now()
                    ws.Cells(bcr.Row, nc).NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"
                    End If
                End If
            
    End With
            With Sheets("StandardTable").Range("A1")
                .ClearContents
                 UserForm1.Show
            End With
                          
            With Application
                'Sheets("Report").Save
                .EnableEvents = True
                .ScreenUpdating = True
End With
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I cant seem to make the ElseIf line work correctly.

Can you elaborate on what you mean by "work correctly"?

When indented properly, the relevant section of code looks like this:

Code:
n = Application.CountIf(ws.Columns(1), Target.Value)
If n = 0 Then
    nr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 0
    If nr = 2 And ws.Range("A2") = vbEmpty Then
        nr = 1
    End If
    ws.Range("A" & nr) = ws.Cells(irow, 1).Value
    ws.Range("B" & nr) = ws.Cells(irow, 2).Value
    ws.Range("E" & nr) = Now()
    ws.Range("E" & nr).NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"
                  
ElseIf n > 0 Then
    [COLOR=#ff0000][B]nr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 0[/B][/COLOR]
    If ws.Cells(nr, 5).Value = Target.Value Then
    nc = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1
    ws.Cells(5, nc) = Now()
    ws.Cells(5, nc).NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"

Else

I suggest that as a start, you put a break point on the line highlighted in red, and step through your code, checking the workbook as you go. What do you think should be happening? What is actually happening?

Perhaps your code doesn't even get to this point? If so, I wonder whether it has anything to do with this previous block:

Code:
With ws
    irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 0
    On Error Resume Next
    ws.Cells(irow, 1).Value = Application.Index(Range("C2:C500"), Application.Match(Range("A1").Value, Range("B2:B500"), 0))
    ws.Cells(irow, 2).Value = Application.Index(Range("D2:D500"), Application.Match(Range("A1").Value, Range("B2:B500"), 0))
    On Error GoTo 0
End With

Are the unqualified ranges perhaps meant to refer to ws?
 
Upvote 0
Hello, thank you for responding StephenCrump. I replaced Target.Value with ws.Cells(irow, 1).value and column F is now populated with Now command. I am still trying to figure out how to incorporate if column F in the selected row is populated then a new row is created.
 
Upvote 0

Forum statistics

Threads
1,223,975
Messages
6,175,746
Members
452,667
Latest member
vanessavalentino83

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