运行'Text to column'宏时'Subscript out of range'错误代码



>我正在尝试将一系列日期从文本转换为"日期"格式,这些格式位于"母版"表的B列中。

我创建了一个宏,但我想从任何工作表运行它,而不是在主工作表上。但是,错误代码"下标超出范围"不断出现,突出显示 用工作表("sh2")。到目前为止,我的代码是:

Sub DateFormatUpdate()
    DateFormatUpdate Macro
    Updates the format of the dates in the master data sheet from 'General' to 'Date'
    Dim sh2 As Worksheet
    Set sh2 = ActiveWorkbook.Sheets("Master")
    With Sheets("sh2")
    Range("B2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("B2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True
    Range("C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("C2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True
    Range("R9").Select
    End With
End Sub

我对 VBA 很陌生,所以如果这实际上是一个简单的问题,我深表歉意!谢谢你的帮助!

而不是

With Sheets("sh2")

尝试使用

With sh2

因为您已将 sh2 声明为工作表变量。

相关内容

最新更新