excel - 宏调用将针对工作簿中所有打开的工作表运行的函数

标签 excel vba

'我正在尝试运行一个宏,它将针对每个工作簿中的每个单元格运行一个自定义公共(public)函数(模块标题是 ClearHTML 或 StripHTML()。我通过一个名为 Remove_HTML 或 ClearHTMLShort() 的模块调用它。现在它只是指向一个事件工作表并专注于 D 列。我的目标是运行一个宏并让它遍历工作簿中每个工作表的所有填充单元格。抱歉,我对此有点陌生。

最好使用自定义函数,然后使用宏来调用单个列的函数。''

Public Function StripHTML(cell As Range) As String
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
Dim sInput As String
Dim sOut As String
sInput = cell.Text

    sInput = Replace(sInput, "\x0D\x0A", Chr(10))
    sInput = Replace(sInput, "\x00", Chr(10))

    sInput = Replace(sInput, "</P>", Chr(10) & Chr(10))
    sInput = Replace(sInput, "<BR>", Chr(10))
    sInput = Replace(sInput, "<li>", "-")

    sInput = Replace(sInput, "&#39;", "'")
    sInput = Replace(sInput, "&ndash;", "–")
    sInput = Replace(sInput, "&mdash;", "—")

    sInput = Replace(sInput, "", "`")

With RegEx
   .Global = True
   .IgnoreCase = True
   .MultiLine = True
   .Pattern = "<[^>]+>" 'Regular Expression for HTML Tags.

End With
    sOut = RegEx.Replace(sInput, "")
    StripHTML = sOut
    Set RegEx = Nothing

End Function

调用函数的代码:
Sub ClearHTMLshort() 
Dim ws As Worksheet 
For Each ws In ThisWorkbook.Worksheets 
  Dim c As Range 
   For Each c In ActiveSheet.UsedRange.Columns("D").Cells 
     c.Value = StripHTML(c) 
   Next c 
Next ws 
End Sub

最佳答案

不妨这样回答:

你做的一切都是正确的,然后用一个错误的陈述把它搞砸了。当您使用 For Each Foo in Bar 循环某些内容时, Foo将是您要使用的对象。

改变:
ActiveSheet.UsedRange.Columns("D").Cells
至:
ws.UsedRange.Columns("D").Cells

关于excel - 宏调用将针对工作簿中所有打开的工作表运行的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364304/

相关文章:

excel - UDF从access数据库检索数据

vba - 设置具有变量名的数组 VBA

shell - 使用 shell32.dll 声明函数 Excel Vba

vba - 什么时候在 VBA 中使用 Workbooks.Close?

arrays - Excel VBA函数: How to pass range,转换为数组,反转,返回数组

html - 如何使用 VBA 缩进 HTML?

excel - 使用 IsNumeric 删除数据行

excel - Excel 2007 中的热图

excel - 如何将一个单元格中的部分字符串替换为另一个单元格中的字符串?

vba - Excel VBA代码导致崩溃-未知原因