2015-07-21 53 views
1

我已经编写了一些代码,用于读取由工作人员执行的程序,并根据每个活动的持续时间将其分为“班次”,以便为某些步骤做准备之前制作。使用VBA进行类型检查

我寻找一些帮助,因为如果有人输入的文字不是一个整数(记或东西)进入“持续时间”选项卡,(被存储为"X"在这个代码)宏观提前停止。

我想我可以用一个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 
+0

请出示你已经尝试了什么。 – Kapol

回答

0

您可以使用Val函数来获取字符串的数字部分。如果值不是数字或空字符串,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 

参考文献:

相关问题