【问题标题】:Excel VBA: Run-time error '438' Object doesn't support this property or methodExcel VBA:运行时错误“438”对象不支持此属性或方法
【发布时间】:2014-08-29 16:04:19
【问题描述】:

请帮忙调试: 运行时错误“438”对象不支持此属性或方法

我不确定为什么我的函数 ConvertToStdDateFormat(InputRange As Range) 不接受范围 'ThisRange'

这是我的输入的样子

201301  201401      201301  201401
201302  201402      201302  201402
201303  201403      201303  201403
201304  201404      201304  201404
201305  201405      201305  201405

下面是代码

Sub trythis()
Dim ThisRange As Range
Dim MonthYear_array As Variant
start_date_row = 1
end_date_row = 12

With ActiveSheet
    Set ThisRange = .Range(Cells(start_date_row, 1), Cells(end_date_row, 2))
    MonthYear_array = .Range(Cells(start_date_row, 4), Cells(end_date_row, 5)).Value
End With

Call ConvertToStdDateFormat(ActiveSheet.Range(Cells(start_date_row,1), Cells(end_date_row, 2)))
Call ConvertToStdDateFormat(ActiveSheet.ThisRange)
End Sub


Public Function GetMonthYearFormatted(InputDate)
'InputDate should be in the format "201401" i.e. year(2014)month(01)
    IPString = CStr(InputDate)
    monthval = CInt(Right(IPString, 2))
    yearval = CInt(Left(IPString, 4))
    opDate = DateSerial(yearval, monthval, 1)
    OPFormatDate = Month(opDate) & "-" & Year(opDate)
    GetMonthYearFormatted = OPFormatDate
End Function

Function ConvertToStdDateFormat(InputRange As Range)
    Dim temp_array As Variant
    temp_array = InputRange
    For colsC = 1 To UBound(temp_array, 2)
        For rowsC = 1 To UBound(temp_array, 1)
            temp_array(rowsC, colsC) = GetMonthYearFormatted(temp_array(rowsC, colsC))
        Next rowsC
    Next colsC
    InputRange.Resize(UBound(temp_array, 1), UBound(temp_array, 2)) = temp_array
    ConvertToStdDateFormat = Null
End Function

【问题讨论】:

    标签: excel vba range runtime-error


    【解决方案1】:

    只要换行

    Call ConvertToStdDateFormat(ActiveSheet.ThisRange)
    

    Call ConvertToStdDateFormat(ThisRange)
    

    并且代码会起作用(范围所在的工作表存储在范围对象本身中,可以被ThisRange.Worksheet引用)。

    为了使调试更容易,以Option Explicit 行开始所有模块可能会很有用。这强制所有使用的变量(即Dim x as Integer 行)的显式声明。

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多