使用替代或替换更改日期格式

我正在从一个Excel文件中读取数据,然后将数据写入另一个Excel文件中。


我的问题是,源具有日期格式dd.mm.yyyy,而目的地必须具有格式dd-mm-yyyy。


所以我搜索了SO,找到了这个答案,用一个字符串替换一个字符,建议我使用substitute。


所以我尝试了这段代码:


For r = 1 To 10 

    myOutputSheet.Cells(6+r, 1).Value  = myInputSheet.Cells(r, 1).Value 'Date

    myOutputSheet.Cells(6+r, 1).Value  = substitute(myOutputSheet.Cells(6+r, 1), ".", "-")

Next

它给出了错误:


Microsoft VBScript runtime error: Type mismatch: 'substitute'

如何正确更改.成-?


更新资料


我也尝试过这个:


For r = 1 To 10 

    myOutputSheet.Cells(6+r, 1).Value  = myInputSheet.Cells(r, 1).Value 'Date

    replace(myOutputSheet.Cells(6+r, 1), 2, 1, "-")

    replace(myOutputSheet.Cells(6+r, 1), 4, 1, "-")

Next

但这给出了错误:


Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub

因此,我尝试:


For r = 1 To 10 

    myOutputSheet.Cells(6+r, 1).Value  = myInputSheet.Cells(r, 1).Value 'Date

    replace myOutputSheet.Cells(6+r, 1), 2, 1, "-"

    replace myOutputSheet.Cells(6+r, 1), 4, 1, "-"

Next

但这给出了错误:


Microsoft VBScript runtime error: Type mismatch: '[string: "-"]'


拉莫斯之舞
浏览 654回答 2
2回答

小怪兽爱吃肉

在按照@Ralph聪明的解释完成date-number-string-value-value2问题后,请记住1)如果要使用SubstituteExcel函数,则必须键入WorksheetFunction.Substitute(...)2)ReplaceVBA函数返回一个字符串,以便像使用它一样myOutputSheet.Cells(6 + r, 1).Value = Replace(myInputSheet.Cells(r, 1), ".", "-")
打开App,查看更多内容
随时随地看视频慕课网APP