【问题标题】:Converting Excel formula to VBA for Mid and Find functions将 Excel 公式转换为 Mid 和 Find 函数的 VBA
【发布时间】:2016-10-08 21:01:50
【问题描述】:

我真的很难将下面的 excel 公式转换为 VBA。我假设下面的函数需要在 for 循环中,并且需要下降动态列长度。我已经四处寻找尝试自己编写脚本,但我似乎无法让它在没有错误的情况下运行。

=MID(A1,FIND(":",A1)+1,FIND("(",A1)-FIND(":",A1)-1)

【问题讨论】:

  • 您的意思是在 VBA 中进行这项工作?使用 range("a1") 并将 FIND 替换为 instr Mid(range(a1).value, instr(1,range("a1").value,":").....
  • 你的标题是 VB Script,你的文本是 VBA。你需要什么?
  • VBA,很抱歉造成混乱!

标签: vba excel for-loop excel-formula


【解决方案1】:

您可以使用以下方法动态获取列长度(或最后一行):

dim ws as Worksheet
Set ws = Thisworkbook.Worksheets("mySheetNameHere")
dim lastRow, myLoop, semicolPos, bracketPos
lastRow = ws.Cells(ws.rows.count, "A").End(xlUp).Row ' this gets last row in col A

For myLoop = 1 to lastRow
    ' Find the position of the ";"  and the "(" 
    semicolPos = InStr(ws.Range("A" & myLoop).Value, ":") + 1
    bracketPos = InStr(ws.Range("A" & myLoop).Value, "(")
    newValue = Mid(ws.Range("A" & myLoop).Value, semicolPos, bracketPos - semicolPos)
    ' Put newValue into column B, one cell over from column A
    ws.Range("B" & myLoop).Value = newValue
Next

【讨论】:

  • 嘿戴夫,这可能是一个愚蠢的问题,但是由于该代码解析了 A 列中的单元格,我需要代码的输出位于 B1、B2 等中。我是否使用偏移量上面代码中“Next”后面的函数?
  • 太棒了!谢谢戴夫!我自己肯定无法做到这一点。我真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
相关资源
最近更新 更多