在VBA中如下代码:
Dim RowCounter As Long
RowCounter = Worksheets(1).UsedRange.Rows.Count()Dim RowCycle As Long
Dim ActiveCell As Range
Dim AgeRange As Range
Dim SexRange As Range
Dim Age As Integer
Dim YearCount As IntegerFor RowCycle = 1 To RowCounter
Set ActiveCell = Worksheets(1).Range("AI" & (RowCycle + 1))
Set AgeRange = Worksheets(1).Range("E" & (RowCycle + 1))
Set SexRange = Worksheets(1).Range("D" & (RowCycle + 1))
Age = CInt(Format(AgeRange, "yyyymmdd") / 10000) //这条语句出现类型不匹配
If (SexRange.Text = "男" & Age <= 1947) Then YearCount = Age - 1932
If (SexRange.Text = "女" & Age <= 1952) Then YearCount = Age - 1932
If (YearCount >= 0) Then ActiveCell.Value = CLng(YearCount * 1300) + CLng(15000) Else ActiveCell.Value = 15000Next RowCycle如上面代码,在调试中可以执行,但是在上面Age那一行出现“类型不匹配”的提示,请问是为什么呢?应该怎么修改?
谢谢大家~!
Dim RowCounter As Long
RowCounter = Worksheets(1).UsedRange.Rows.Count()Dim RowCycle As Long
Dim ActiveCell As Range
Dim AgeRange As Range
Dim SexRange As Range
Dim Age As Integer
Dim YearCount As IntegerFor RowCycle = 1 To RowCounter
Set ActiveCell = Worksheets(1).Range("AI" & (RowCycle + 1))
Set AgeRange = Worksheets(1).Range("E" & (RowCycle + 1))
Set SexRange = Worksheets(1).Range("D" & (RowCycle + 1))
Age = CInt(Format(AgeRange, "yyyymmdd") / 10000) //这条语句出现类型不匹配
If (SexRange.Text = "男" & Age <= 1947) Then YearCount = Age - 1932
If (SexRange.Text = "女" & Age <= 1952) Then YearCount = Age - 1932
If (YearCount >= 0) Then ActiveCell.Value = CLng(YearCount * 1300) + CLng(15000) Else ActiveCell.Value = 15000Next RowCycle如上面代码,在调试中可以执行,但是在上面Age那一行出现“类型不匹配”的提示,请问是为什么呢?应该怎么修改?
谢谢大家~!
Age = Format(AgeRange, "yyyymmdd") / 10000
也出现同样问题,不知道是什么原因引起的。
Format(AgeRange, "yyyymmdd") 上面.如果AgeRange是一个不为空的数据的话.(而且必须是日期型的.比如:08/01/02等)那没有什么问题.如果不能满足我上面提到的要求,就会出现"类型不匹配"的错误.
我修改了AgeRange列的类型后,还是出现了同样的问题,还有就是在之前定义X的时候应该是什么类型呢(或者说可以不定义X直接使用)?
if isDate(ageRange) then
Age = year(AgeRange)
else
'ageRange数据错误,不是日期型数据
end if
通过year(AgeRange)这种方式,没有错误了,但是前面的“类型错误”还是不知道错在什么地方。非常感谢fvflove..