0

我遵循了一些数组教程,但是我在 VBA 中的代码对于我来说很难将其更改为我的基本知识的数组。

任何人都可以帮忙吗?

这是我的代码:

Sub InternExtern()

Dim source, addrescell, destination As Range
Dim Sourcevalue As String


For Each source In Range("E6", Range("E" & Rows.Count).End(xlUp))
 If source.Value <> "" Then
    For Each addrescell In Range("address_table_names").Rows
             If addrescell.Cells(1).Value <> "" And InStr(source.Offset(0, 23).Value, "Extern") = 0 Then
               SourceName = addrescell.Cells(1).Value
               Sourcevalue = addrescell.Cells(1, 4).Value
                    If InStr(UCase(source), UCase(SourceName)) <> 0 Then
                      If InStr(Sourcevalue, "10.") <> 0 Or InStr(Sourcevalue, "192.168.") <> 0 Or IsInternal(addrescell.Offset(0, 3).Value) Then
                       source.Offset(0, 23) = "Intern"
                       Else: source.Offset(0, 23) = "Extern"
                      End If
                    End If
                      If InStr(source, "-ext-") <> 0 Or InStr(source, "any") <> 0 Or InStr(source, "-EXT-") <> 0 Then
                      source.Offset(0, 23) = "Extern"
                      End If
                      If InStr(source, "any") <> 0 And InStr(source.Offset(0, -1).Value, "FW-Peering") = 0 Then
                      source.Offset(0, 23) = "Intern"
                      End If

            End If
            Next addrescell
End If
Next source

我将列值添加到数组中的目标是让它更快。

提前致谢!

4

2 回答 2

1

我不太明白将某些东西放入数组中会有什么帮助。你得有时间再把它拿出来。

您可以尝试在循环运行时禁用Application.ScreenUpdatingApplication.Calculation在最后将其重新打开。

Application.ScreenUpdating = False
Application.Calculation = xlManual
' your loop
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic

如果您可以解释代码在做什么或您想做什么,我们可能会提供更多帮助。使用@sehe 建议的工作表函数可能是一个更好的解决方案。

更新

只需查看您指向 codereview 的链接,其中一个答案表明创建工作表的数组副本并将修改后的数组重新应用于工作表相当简单快捷。以前没见过,很酷的东西。

虽然数组可能更快,但我认为在您的情况下,它们只会增加很多额外的复杂性,因为您使用的对象方法在您将其转换为数组时不会存在。

假设您将单元格地址添加到数组中(否则您没有工作表的上下文)

Array(1).Offset(0,1) = <not going to happen>

Range(Array(1)).Offset(0,1) = <going to work>

所以你会回到对象来做你的事情,除非你有很多数组,这可能是多余的,也可能是多余的。

由于我不能 100% 确定您的功能的目标是什么,所以这可能根本没有帮助!我认为您的内部 for 循环可能有一些额外的(不需要的)迭代。如果在标记的部分'HERE基本上意味着您已经完成了该迭代,您可以退出循环并转到下一个父循环。

For Each source In Range("E6", Range("E" & Rows.Count).End(xlUp))
    If source.Value <> "" Then
        For Each addrescell In Range("address_table_names").Rows
            If addrescell.Cells(1).Value <> "" Then ' vba doesn't shortcircuit
                If InStr(source.Offset(0, 23).Value, "Extern") = 0 Then
                    SourceName = addrescell.Cells(1).Value
                    Sourcevalue = addrescell.Cells(1, 4).Value
                    If InStr(UCase(source), UCase(SourceName)) <> 0 Then
                        If InStr(Sourcevalue, "10.") <> 0 Or InStr(Sourcevalue, "192.168.") <> 0 Or IsInternal(addrescell.Offset(0, 3).Value) Then
                            source.Offset(0, 23) = "Intern"
                            ' HERE
                            Exit For
                        Else
                            source.Offset(0, 23) = "Extern"
                            ' HERE
                            Exit For
                        End If
                    End If
                    If InStr(source, "-ext-") <> 0 Or InStr(source, "any") <> 0 Or InStr(source, "-EXT-") <> 0 Then
                        source.Offset(0, 23) = "Extern"
                        ' HERE
                        Exit For
                    End If
                    If InStr(source, "any") <> 0 Then ' again, no shortcircuit
                        If InStr(source.Offset(0, -1).Value, "FW-Peering") = 0 Then
                            source.Offset(0, 23) = "Intern"
                            ' HERE
                            Exit For
                        End If
                    End If
                End If
            End If
        Next addrescell
    End If
Next source

有关shortcircuit-ing, 嵌套 if 的信息比使用更快and,而不是光速,但可以节省一次比较。如果您正在进行大量迭代,它可以加起来(无论如何)

于 2013-03-15T12:57:03.257 回答
0

我建议用它制作一个工作表函数:

这样,您只需在计算列中键入公式

 =InternExtern(E6)

并且只有在进行完全重新计算、重新编译 VBA 项目或源值更改时才会重新计算单元格!

于 2013-03-15T12:43:23.257 回答