如何更改我的代码以将值获取到数组中

时间:2013-03-15 12:33:22

标签: excel vba excel-vba

我遵循了一些数组教程,但是我在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

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

提前致谢!

2 个答案:

答案 0 :(得分:1)

我不太清楚如何将数组放入数组中会有所帮助。你必须在某个时候再拿出来。

您可以尝试在循环运行时禁用Application.ScreenUpdatingApplication.Calculation并在结束时重新启用它。

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

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

<强>更新

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

虽然Arrays可能更快,但我认为在你的情况下,当你使用在将它转换为数组时不会存在的对象的方法时,它们会增加很多额外的复杂性。 < / p> <击>

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

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

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

答案 1 :(得分:0)

我建议用它来制作工作表功能:

这样,您只需在计算列

中键入公式即可
 =InternExtern(E6)

如果您执行完整的重新计算,则只会重新计算单元格,重新编译VBA项目或源值更改!