/ / Vérifiez si la cellule est un nombre dans Excel VBA [dupliquer] - excel, vba, excel-vba

Vérifier si la cellule est un nombre dans Excel VBA [dupliquer] - excel, vba, excel-vba

dans le code ci-dessous, je travaille à faire numériquecalculs sur cellules. Parfois cependant, l’une des cellules (par exemple, "LosLimit") peut contenir une valeur non numérique telle que "---". Comment puis-je vérifier si la cellule est numérique ou non.

S'il s'agit d'un nombre, effectuez un calcul, sinon renvoyez une valeur à partir de "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

Réponses:

3 pour la réponse № 1

Votre réponse était dans la question! Il existe une fonction isNumeric (variable_goes_here) qui renvoie un vrai ou un faux, par exemple.

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

retournera un vrai et vous donnera la boîte de message pour x = 45 mais passera si x = "Not a number"