首页 > 解决方案 > else if statement for check in check out sheet vba not working

问题描述

I have an excel sheet in which lies a data entry form and a check in check out list. I have a macro set up to search for the entry, and if it doesnt exist, make a new entry with a checkout time, if it does exist, it checks it back in with a time. My issue lies when I try to check out an entry that already exists. The code just updates the check in time. Ive added an elseif statement into the code but it doesnt seem to do anything. If anyone could help me figure out why itd be much appreciated.

Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
 
barcode = Worksheets("Sheet1").Cells(2, 2)

    Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If rng = True Or ActiveCell.Offset(0, 2).Value = True Then
        rownumber = rng.Row
        Worksheets("Sheet1").Cells(rownumber, 1).Select
        ActiveCell.Offset(0, 2).Clear
        ActiveCell.Offset(0, 1).Select
        ActiveCell.Value = Date & "  " & Time
        ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
        Worksheets("Sheet1").Cells(2, 2) = ""
        Cells(2, 2).Select
    ElseIf rng Is Nothing Then
        ActiveSheet.Columns("a:a").Find("").Select
        ActiveCell.Value = barcode
        ActiveCell.Offset(0, 1).Select
        ActiveCell.Value = Date & "  " & Time
        ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
        Worksheets("Sheet1").Cells(2, 2) = ""
        ActiveCell.Offset(0, 3).Select
    ElseIf rng = True Then
        rownumber = rng.Row
        Worksheets("Sheet1").Cells(rownumber, 1).Select
        ActiveCell.Offset(0, 2).Select
        ActiveCell.Value = Date & "  " & Time
        ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
        Worksheets("Sheet1").Cells(2, 2) = ""
        rng.Offset(, 1).Clear
        ActiveCell.Offset(0, 2).Value = "TOOL CRIB"
        Cells(2, 2).Select
    End If
End Sub

标签: excelvba

解决方案


推荐阅读