【问题标题】:Getting type mismatch error in replace function in vba在 vba 中的替换函数中获取类型不匹配错误
【发布时间】:2014-05-08 04:07:34
【问题描述】:

我的 vba 代码是将当前时间和日期替换为 4_17_2014 8_09_00 PM 格式

但是在 VBA 代码下运行时出现类型不匹配错误

Function Current_Date_Time_Stamp()

Dim CurrTime As Date

CurrTime = Now
MsgBox Now
CurrTime = Replace(CurrTime, "-", "_")
MsgBox CurrTime
CurrTime = Replace(CurrTime, ":", "_")
Current_Date_Time_Stamp = CurrTime

End Function

任何人都可以帮助我为什么我会出错

【问题讨论】:

  • 你在哪一行得到错误?
  • Replace 对字符串进行操作,你给它一个日期。如果您想要日期值的特定表示,请尝试查看 Format()。
  • Tim Williams,我在第 5 行出现错误

标签: excel vba


【解决方案1】:

正如@Tim Williams 在 cmets 中提到的,Replace 仅适用于 string 变量,但 CurrTimedate(实际上,日期变量不存储 格式日期,但仅限日期本身。显示日期时的格式取决于您单元格的区域设置和数字格式)。

但是你可以使用这样的单行代码函数:

Function Current_Date_Time_Stamp()
    Current_Date_Time_Stamp = Format(Now, "mm_dd_yyyy h_mm_ss AM/PM;@")
End Function

【讨论】:

  • 感谢您的回复,对我有很大帮助
猜你喜欢
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 2021-04-26
相关资源
最近更新 更多