2015-07-21 90 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 

參考文獻:

相關問題