在VBA中定义InStr

安东尼

在他们更改了我的日期/时间格式之前,有一行代码一直有效。我需要从中提取日期,小时和分钟:

StringOfText:StringOfText1开始运行,结果:XXXXXXXX 2020-01-21_11h49m38s

这是我正在使用的,但是在运行excel宏时出现错误不匹配:

Start_Time = _
  DateValue(Left(Right(Left(Cells(i, x).Value, (InStr(Cells(i, x).Value, "m*s") + 2)), 20), 10)) + _
  TimeValue(Left(Right(Left(Cells(i, x).Value, (InStr(Cells(i, x).Value, "m*s") + 2)), 9), 2) & _
  ":" & Left(Right(Left(Cells(i, x).Value, (InStr(Cells(i, x).Value, "m*s") + 2)), 6), 2))

我知道我在那里需要一个“赞”,但是当我添加它时,我会得到更多的错误。代码在“ m * s”具有“ min”(之前使用的行的末尾)之前有效。i和x在此代码段上方已预定义。

任何想法或帮助将不胜感激。

尊尼
      dim s as string
    
'this is the cell value in your loop
      s = Cells(i, x).Value
        
      Dim i As Long
    
'locate as a reference point the underscore as a known, reliable anchor
      i = InStr(1, s, "_", vbTextCompare)

'Once we know where the data is we can remove the formatting from the string
' that prevents us from using VBA CDate - "_", "h", "m", "s"

' use mid string with no third parameter to get all of the remaining string from the start point
' use len("yyyy-mm-dd") rather than just 10 as a magic number for clarity
      s= Mid(s, i - len("yyyy-mm-dd"))

' make necessary replacements to change "2020-01-01_11h20m34s" to
' 2020-01-01 11:20:34" which can be parsed by CDate
      s = Replace(s, "_", " ")
      s = Replace(s, "h", ":")
      s = Replace(s, "m", ":")
      s = Replace(s, "s", "")
      start_time=CDate(s)
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

由VBA中的变量定义的函数名称

来自分类Dev

类定义如何在VBA中工作

来自分类Dev

使用InStr()vba在字符串中搜索3“

来自分类Dev

在VBA中使用InStr进行多字符串搜索

来自分类Dev

VBA InStr始终返回0

来自分类Dev

Instr()中的溢出错误

来自分类Dev

用户定义功能中的vba用户定义运算符

来自分类Dev

VBA在变量中定义属性

来自分类Dev

“如果不是instr”不起作用,我认为应该在vba中

来自分类Dev

Excel VBA脚本中的对象定义错误

来自分类Dev

VBA:将多个值传递给Instr

来自分类Dev

VBA向新查询定义中添加参数

来自分类Dev

VBA定义数组中的范围

来自分类Dev

在VBA中定义单元格

来自分类Dev

VBA表单中的自定义数据验证

来自分类Dev

Spark SQL中的Oracle INSTR等效项

来自分类Dev

使用VBA代码访问CATIA中的用户定义属性

来自分类Dev

Bigquery中REGEXP_INSTR的替代

来自分类Dev

VBA Excel InStr(myString)不返回结果

来自分类Dev

使用InStr()vba在字符串中搜索3“

来自分类Dev

如何在tsql中执行instr

来自分类Dev

VBA在变量中定义属性

来自分类Dev

访问Instr Vba

来自分类Dev

阵列VBA中的用户定义对象

来自分类Dev

在VBA中定义动态范围

来自分类Dev

如果使用InStr,则为VBA

来自分类Dev

VSTR中的InSTR或查找功能

来自分类Dev

是否可以在 VBA 中定义用户定义类型的常量?

来自分类Dev

VBA InStr 函数在循环中限制匹配解决方法