我正在使用使用组合框和按钮的搜索和更改方法,但两个重要的公共变量无缘无故地继续更改为零。
这些方法的目的是搜索设备类型,然后搜索特定设备并更改相关单元格的值。它似乎在第一次按下按钮后工作,但如果组合框没有更改并且按下按钮,重要的变量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 仍然是 false,所以它是说找不到设备。