2016-09-16 86 views
0

我試圖在3個不同的範圍上一個接一個地運行一個宏。一旦選擇範圍,代碼就可以正常工作(定義變量F和L)。我想將r1-r3設置爲我需要的範圍,然後使用字符串變量將範圍數字連接在一起。此代碼有效,但不會在所選範圍中提供起始和結束行號。這是至關重要的,因爲它告訴「TableCalc」宏何時啓動和停止代碼。然後我想繼續下一個範圍。謝謝你的幫助。excel vba將字符串轉換爲範圍

Sub TestRangeBC() 

WS.Select 

Dim r1 As Range 
Dim r2 As Range 
Dim r3 As Range 
Dim rngx As String 
Dim num As Integer 
Dim rng As Range 

Set r1 = WS.Range("ONE") 
Set r2 = WS.Range("TWO") 
Set r3 = WS.Range("THREE") 

For num = 1 To 3 
    rngx = "r" & num 
    Set rng = Range(rngx) 

    Dim F As Integer 
    Dim L As Integer 

    F = rng.Row + 1 
    L = rng.Row + rng.Rows.Count - 2 
    Cells(F, 8).Select 

    Do While Cells(F, 8) <> "" And ActiveCell.Row <= L 

     'INSERT SITUATIONAL MACRO 
     Call TableCalc 
     WS.Select 
     ActiveCell.Offset(1, 0).Select 
    Loop 
Next num 

End Sub 
+0

你說的「它告訴TableCalc宏時啓動和停止的代碼」是什麼意思?我沒有看到使用任何參數或全局變量。 – Comintern

+0

[不要使用'.Select'](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)在你的「情景宏」部分。這可能有幫助。另外,你的命名範圍是單排嗎?還是多行?如果你用'F8'瀏覽宏,什麼是'F'?另外,您需要指定您希望獲取'Cells(F,8)'的表。 – BruceWayne

+0

@Tony Abadie是'TableCalc'代碼與這個'Sub'在同一個模塊中?爲什麼在'Do While'循環中'Cells(F,8)<>「」'不會進入? –

回答

0

這不是答案(如你的代碼的一部分,你想實現的是目前還不清楚是什麼),但它是一個「乾淨」,更高效的代碼你在你的原職的方式。

Option Explicit 

Dim WS    As Worksheet 

您的原始分縮短:

Sub TestRangeBC() 

' chanhe WS to your Sheet name 
Set WS = Sheets("Sheet1") 

Call ActiveRange("ONE") 
Call ActiveRange("TWO") 
Call ActiveRange("THREE") 

End Sub 

Sub得到命名範圍(你在工作簿設置)作爲一個字符串的名稱,並設置相應的範圍內。

Sub ActiveRange(RangeName As String) 

Dim Rng     As Range 
Dim F     As Integer 
Dim L     As Integer 
Dim lRow    As Long 

With WS 
    Set Rng = .Range(RangeName) 

    ' just for debug purpose >> to ensure the right Range was passed and set 
    Debug.Print Rng.Address 

    F = Rng.Row + 1 
    L = Rng.Row + Rng.Rows.Count - 2 

    lRow = F 

    ' what you are trying to achieve in this loop is beyond me 
    Do While .Cells(F, 8) <> "" And .Cells(lRow, 8).Row <= L 
     Debug.Print .Cells(lRow, 8).Address 
     'INSERT SITUATIONAL MACRO 
     ' Call TableCalc 
     ' not sure you need to select WS sheet again 
     WS.Select 
     lRow = lRow + 1 
    Loop 
End With 

End Sub 

你想在下面的循環中測試什麼,留在循環中的標準是什麼?

Do While Cells(F, 8) <> "" And ActiveCell.Row <= L 
0

它真的很難告訴你可能想要做

什麼,但可能接下來可以幫你澄清和(希望)這樣做!

首先,你不能「結合」變量名

所以我用命名的數組去範圍的名稱(即String陣列)通過特定的子來填補:

Function GetRanges() As String() 
    Dim ranges(1 To 3) As String 

    ranges(1) = "ONE" 
    ranges(2) = "TWO" 
    ranges(3) = "THREE" 
    GetRanges = ranges 
End Function 

,這樣可以清理你的「主」子代碼,只保留有更多的相關代碼:

Sub TestRangeBC() 
    Dim r As Variant 
    Dim ws As Worksheet 

    Set ws = Worksheets("Ranges") '<--| change "Ranges" to your actual worksheet name 

    For Each r In GetRanges() '<--| loop through all ranges names 
     DoIt ws, CStr(r) '<--| call the range name processing routine passing worksheet and its named range name 
    Next r 
End Sub 

「主」子循環直接通過收集到的命名範圍陣列從GetRanges()並呼籲DoIt()實際處理當前一個:

Sub DoIt(ws As Worksheet, rangeName As String) 
    Dim cell As Range 
    Dim iRow As Long 

    With ws.Range(rangeName) '<--| reference the passed name passed worksheet named range 
     For iRow = .Rows(2).Row To .Rows(.Rows.Count - 2).Row '<--| loop through its "inner" rows (i.e. off 1st and last rows) 
      Set cell = ws.Cells(iRow, 8) '<--| get current row corresponding cell in column "F" 
      If cell.value = "" Then Exit For '<--| exit at first blank column "F" corresponding cell 
      TableCalc cell '<-- call TableCalc passing the 'valid' cell as its parameter 
     Next iRow 
    End With 
End Sub 
+0

@TonyAbadie,你通過了嗎? – user3598756

相關問題