2017-06-24 112 views
-4

在下面的代碼中,我正在對單元格進行數值計算。有時候,其中一個單元格(比如說「LosLimit」)可能包含一個非數字值,如「---」。如何檢查單元格是否爲數字。檢查單元格是否爲Excel中的數字VBA

如果它是一個數字,然後從「MeasValue」做計算,否則返回值

Sub ReturnMarginal() 

    'UpdatebySUPERtoolsforExcel2016 
    Dim xOut As Worksheet 
    Dim xWb As Workbook 
    Dim xWks As Worksheet 
    Dim InterSectRange As Range 
    Dim lowLimCol As Integer 
    Dim hiLimCol As Integer 
    Dim measCol As Integer 

    Application.ScreenUpdating = False 
    Set xWb = ActiveWorkbook 
    For Each xWks In xWb.Sheets 
     xRow = 1 
     With xWks 
      FindString = "LowLimit" 
      If Not xWks.Rows(1).Find(FindString) Is Nothing Then 
       .Cells(xRow, 16) = "Meas-LO" 
       .Cells(xRow, 17) = "Meas-Hi" 
       .Cells(xRow, 18) = "Min Value" 
       .Cells(xRow, 19) = "Marginal" 
       LastRow = .UsedRange.Rows.Count 
       lowLimCol = Application.WorksheetFunction.Match("LowLimit", xWks.Range("1:1"), 0) 
       hiLimCol = Application.WorksheetFunction.Match("HighLimit", xWks.Range("1:1"), 0) 
       measLimCol = Application.WorksheetFunction.Match("MeasValue", xWks.Range("1:1"), 0) 
       .Range("P2:P" & lastRow).Formula = "=IF(ISNUMBER(" & .Cells(2, lowLimCol).Value2 & ")," & Cells(2, measLimCol).Address(False, False) & "-" & Cells(2, lowLimCol).Address(False, False) & "," & Cells(2, measLimCol).Address(False, False) & ")" 


       .Range("Q2:Q" & LastRow).Formula = "=" & Cells(2, hiLimCol).Address(False, False) & "-" & Cells(2, measLimCol).Address(False, False) 

       .Range("R2").Formula = "=min(P2,Q2)" 
       .Range("R2").AutoFill Destination:=.Range("R2:R" & LastRow) 

       .Range("S2").Formula = "=IF(AND(R2>=-3, R2<=3), ""Marginal"", R2)" 
       .Range("S2").AutoFill Destination:=.Range("S2:S" & LastRow) 

      End If 

     End With 
     Application.ScreenUpdating = True 'turn it back on 
    Next xWks 

End Sub 
+1

谷歌輸入只是標題返回'1,290,000'結果將跳過。 –

+0

@ A.S.H,我很抱歉,但我把代碼預先測試......即。我的思維過程正在進行......那不是很遠!我用我實際嘗試過的代碼更新了代碼,但它只返回「MeasValue」。我是否錯誤地使用'isNumeric'?謝謝 – Joe

+0

您正在檢查單元格的地址,您應該檢查內容。 –

回答

1

你的答案是問題!函數isNumeric(variable_goes_here)將返回true或false,例如

if isNumeric(x) then msgbox("Woop!") 

將返回一個真正的,給你的消息框x = 45但如果x = "Not a number"

相關問題