在下面的代码中,我将范围转换为字符串:
dCostCenter = Right(dCostCenter, Len(dCostCenter) - 1)
但是,我需要在行中再次使用dCostCenter
作为范围:
Set DetailsTargetCell = forecastMain.Cells(dCostCenter.Row, dMonth.Column)
否则我收到“需要对象”错误。
但是可以将字符串转换为范围吗?或许还有另一种方法可以解决这个问题?
Public Function SetDetailsTarget(monthNumber As Integer, CCNumber As String)
Call SetWorkbooks
Call SetRanges
For Each dMonth In DetailsMonths
For Each dCostCenter In DetailsCostCenters
If Left(dCostCenter, 1) = "0" And Not dCostCenter Like "*/*" Then
dCostCenter = Right(dCostCenter, Len(dCostCenter) - 1)'range to string
End If
If monthNumber <> "0" Then
If dMonth = MonthName(monthNumber) And dCostCenter = CCNumber Then
Set DetailsTargetCell = forecastMain.Cells(dCostCenter.Row, dMonth.Column)' I need string to range here
End If
End If
Next dCostCenter
Next dMonth
答案 0 :(得分:1)
添加另一个变量,例如dCostCenterString。然后在将范围转换为字符串时使用此选项,保留原始范围可供使用。 也没有声明你的变量,例如
dim dCostCenterString as string
您正在使用变体,这会降低代码速度并产生错误,因为vba必须猜测变量是什么。
答案 1 :(得分:0)