types使用VBA进行检查

我已经写了一些代码,读取一个电子表格,充满了由工作人员执行的程序,并根据每个活动的持续时间将它们分成“轮class”,以便以前可以为某些步骤做准备。

我正在寻找一些帮助,就好像有人在“duration”标签中input不是一个整数(一个音符或者其他东西)的文本(在这个代码中存储为"X" ),macros提前停止。

我想我可以使用if语句来检查这个,也许是"IsNumeric()"函数,但它不会运行,我知道我没有正确地做。

 Private Sub CommandButton1_Click() 'define variables Dim duration As Integer, n As Long, i As Integer, x As Integer, m As Long Dim toolRange As Range, partRange As Range, perRange As Range, workRange As Range, ppeRange As Range n = 3 'indicates row m = 3 'concatenation counter duration = 0 'duration counter x = 0 'duration placeholder For i = 1 To 100 'Assumed max 50 shifts (This can be changed or solved with more code, but should be set higher than predicted # of shifts) duration = 0 'resets duration for next count While duration < Worksheets("Shifts").Cells(6, "K").Value 'shift length can be altered x = Worksheets("SR060-SR070").Cells(n, "F").Value duration = duration + x 'adds duration until it is over 320 n = n + 1 Wend With Worksheets("SR060-SR070") Set toolRange = .Range(.Cells(m, "H"), .Cells(n, "H")) 'creates tool range End With With Worksheets("SR060-SR070") Set partRange = .Range(.Cells(m, "I"), .Cells(n, "I")) 'creates part range End With With Worksheets("SR060-SR070") Set perRange = .Range(.Cells(m, "E"), .Cells(n, "E")) 'creates per range End With With Worksheets("SR060-SR070") Set workRange = .Range(.Cells(m, "P"), .Cells(n, "P")) 'creates permit range End With With Worksheets("SR060-SR070") Set ppeRange = .Range(.Cells(m, "Q"), .Cells(n, "Q")) 'creates ppe range End With Worksheets("Shifts").Cells(i + 1, 1).Value = i 'creates shift Worksheets("Shifts").Cells(i + 1, 2).Value = Application.Max(perRange) 'creates max per Worksheets("Shifts").Cells(i + 1, 3).Value = duration 'creates duration 'Worksheets("Shifts").Cells(i + 1, 4).Value = ConcatenateAllCellValuesInRange(toolRange) 'inputs tools Worksheets("Shifts").Cells(i + 1, 4).Value = ConcatUniq(toolRange, " ") 'inputs tools 'Worksheets("Shifts").Cells(i + 1, 5).Value = ConcatenateAllCellValuesInRange(partRange) 'inputs parts Worksheets("Shifts").Cells(i + 1, 5).Value = ConcatUniq(partRange, " ") 'inputs parts 'Worksheets("Shifts").Cells(i + 1, 6).Value = ConcatenateAllCellValuesInRange(workRange) 'inputs permits Worksheets("Shifts").Cells(i + 1, 6).Value = ConcatUniq(workRange, " ") 'inputs permits 'Worksheets("Shifts").Cells(i + 1, 7).Value = ConcatenateAllCellValuesInRange(ppeRange) 'inputs ppe Worksheets("Shifts").Cells(i + 1, 7).Value = ConcatUniq(ppeRange, " ") 'inputs ppe m = n 'Allows it to segement down page Next i 'goes to next shift End Sub 'Concatenate function Function ConcatUniq(ByRef rng As Range, _ ByVal myJoin As String) As String Dim r As Range Static dic As Object If dic Is Nothing Then _ Set dic = CreateObject("Scripting.Dictionary") For Each r In rng dic(r.Value) = Empty Next ConcatUniq = Join$(dic.keys, myJoin) dic.RemoveAll End Function 

您可以使用Val函数来获取string的数字部分。 如果值不是数字或空string,Val也将返回0。 你可以在你的While条件下将它和IsNumeric结合起来。

 Dim vVal As Variant Dim nVal As Long vVal = Worksheets("Shifts").Cells(6, "K").Value nVal = Val(vVal) While duration < nVal && IsNumeric(vVal) ... Wend 

参考文献:

  • Val函数 – VBA
  • 是数字函数 – VBA