我正在尝试获取一个单元格,检查它是否具有小数位,然后删除它们,然后根据数字中包含多少个字符,在单元格中放置特定的格式,len函数返回null,而instr函数起作用,但是当传递给变量将返回null。谢谢任何可以帮助的人。在第一个if函数的末尾,我将三个不起作用的变量的结果打印到立即窗口进行验证,请使用Debug.Print命令转到查看菜单并激活立即窗口以进行观看。
Function cnvtDta()
ActiveSheet.Select
Data1 = Range("data").Value
Dim rslt As String
rslt = Data1
Set myrng = Range("data")
Dim wot, sowot
'Find decimal place in cell
dot = myrng.Find(".", myrng)
If dot = True Then
'if decimal place strip remainders and decimal point
Dim pos, res
pos = InStr(1, rslt, ".")
res = Left(rslt, pos)
sowot = Len(res)
End If
Debug.Print res
Debug.Print sowot
Debug.Print pos
'Return specific formats to cell
'thank you kindly to anyone who can spare the time to genuinely help
End Function
所以基本上,您的问题有几个部分。
Function DoesCellContainDecimals(inputRange As Range) As Boolean
Dim tolerance As Double
tolerance = 0.0001
If Not IsNumeric(inputRange.Value2) Then
'invalid argument
CellValueLenghtWithoutDecimal = False
Exit Function
End If
If (Abs(Fix(inputRange.Value2) - inputRange.Value2) < tolerance) Then
'value does not have meaningful decimals
CellValueLenghtWithoutDecimal = False
Else
'value has meaningful decimals
CellValueLenghtWithoutDecimal = True
End If
End Function
Int(6.5) '6
Fix(6.5) '6
Int(-6.5) '-7
Fix(-6.5) '-6
Format(6500000,"# ### ###") '6 500 000
Range("A1").NumberFormat = "# ### ##0" 'same effect as above but only when displaying in that cell.