2013-03-15 29 views
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 

我添加的列值到陣列的目標是,使其更快。

在此先感謝!

+1

爲了使_what_更快,反正呢? – sehe 2013-03-15 12:37:56

+0

您沒有正確聲明所有範圍變量。 'source'和'addrescell'是Variant,基於你設定尺寸的方式。 – 2013-03-15 13:45:45

+0

您的[上一個問題](http://codereview.stackexchange.com/q/23923/2726)已被遷移到CR,並得到[Dick Kusleika](http://codereview.stackexchange.com/a/)的很好回答2726分之23956)。投票結束。 – 2013-03-15 18:38:25

回答

1

我不太明白如何在數組中放入東西會有所幫助。你必須在某個時候再次拿出。

您可能會嘗試禁用Application.ScreenUpdatingApplication.Calculation,而循環運行並在最後將其重新打開。

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

如果你能解釋代碼在做什麼或你想做什麼,我們可能會幫助更多。使用像@sehe這樣的工作表函數可能是更好的解決方案。

更新

只是看着你的鏈接代碼審查和答案的一個顯示,其相當簡單和快速的創建工作表的數組複製與修改後的數組重新應用到工作表。之前沒有看到,很酷的東西。

雖然數組可能會更快,但我認爲對於您的情況,當您將使用不存在的對象的方法轉換爲數組時,它們會增加很多額外的複雜性。

假設你添加單元格地址的陣列(否則你沒有上下文片)

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

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

所以你會回去的對象做你的東西,除非你有很多數組,可能或可能是多餘的。

因爲我不是100%確定你的功能的目標是什麼,這可能根本沒有幫助! 我想你可能會有一些額外的(不需要的)你的循環內部迭代。如果標記爲 '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,有一個嵌套的,如果是比使用and,沒有光速快,但可以節省你一個比較。如果你正在做大量的迭代,它可以加起來(有點小)

+0

嘿,謝謝你的回答,但在我之前的問題上,他們告訴我要製作數組。你剛剛說了什麼,那就是我已經做的。這是我以前的問題:http://codereview.stackexchange.com/questions/23923/improve-vba-code-excel – user2156096 2013-03-15 14:02:29

相關問題