我正在使用通过组合框和按钮进行搜索和更改的方法,但是两个重要的公共变量在没有明显原因的情况下继续变为零。
这些方法的目的是搜索设备类型,然后搜索特定设备并更改相关单元格的值。第一次按下按钮后似乎可以使用,但是如果不更改组合框并按下按钮,则重要变量OSSstockNum
和OSSstartingRowNum
会更改为零,因此while循环未运行。
public OSSstockNum As Integer
public OSSstartingRowNum As Integer
Private Sub ComboBox1_Change()
j = 1
OSSfound = False
While j <= Cells(Rows.Count, 2).End(xlUp).Row And OSSfound = False
If Range("B" + CStr(j)).Value = ComboBox1.Value Then
OSSfound = True
Else
j = j + 1
End If
Wend
OSSstockNum = Range("I" + CStr(j)).Value
OSSstartingRowNum = j + 2
End Sub
Private Sub btnFaulty_Click()
Dim intRowNum As Integer
Dim OSSInput As String
Dim CNAddress As String
Dim statusAddress As String
Dim assignAddress As String
Dim condAddress As String
OSSInput = InputBox("What is the Code of the " + ComboBox1.Value + " you
want to change to faulty?")
intRowNum = 0
OSSfound = False
assignAddress = G1
statusAddress = H1
condAddress = I1
While (intRowNum < OSSstockNum And OSSfound = False)
CNAddress = "D" + CStr(OSSstartingRowNum + intRowNum)
If Range(CNAddress).Value = OSSInput Then
OSSfound = True
assignAddress = "G" + CStr(OSSstartingRowNum + intRowNum)
statusAddress = "H" + CStr(OSSstartingRowNum + intRowNum)
condAddress = "I" + CStr(OSSstartingRowNum + intRowNum)
Else
intRowNum = intRowNum + 1
End If
Wend
If OSSfound = True Then
If (Range(statusAddress).Value = "FAULTY") Then
MsgBox ("This device is already faulty")
Else
Range(assignAddress).Value = "FAULTY"
Range(assignAddress).Interior.Color = RGB(255, 199, 206)
Range(assignAddress).Font.Color = RGB(156, 0, 6)
Range(statusAddress).Value = "FAULTY"
Range(statusAddress).Interior.Color = RGB(255, 199, 206)
Range(statusAddress).Font.Color = RGB(156, 0, 6)
Range(condAddress).Value = "FAULTY"
Range(condAddress).Interior.Color = RGB(255, 199, 206)
Range(condAddress).Font.Color = RGB(156, 0, 6)
End If
Else
MsgBox ("Sorry, the device you're looking for couldn't be found")
End If
End Sub
如果找到了输入搜索词,它应该更改所选单元格的值和格式,但是当第二次按下按钮时,将跳过按钮中的while
循环,因此为true仍然为假,因此表示找不到该设备。