excel vba将字符串转换为范围

时间:2016-09-16 16:31:57

标签: excel vba excel-vba macros

我试图在一个接一个的3个不同范围上运行一个宏。选择范围后,代码就可以正常工作(定义变量F和L)。我想将r1-r3设置为我需要的范围,然后使用字符串变量将范围数字连接在一起。此代码有效,但不提供所选范围内的起始和结束行号。这是至关重要的,因为它告诉“ TableCalc ”宏何时启动和停止代码。然后我想继续下一个范围。谢谢你的帮助。

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

2 个答案:

答案 0 :(得分:0)

这不是答案(作为代码的一部分,你想要实现的目标还不清楚),但它是一个更清洁的&#34;以及更有效的方式来编写原始帖子中的内容。

Option Explicit

Dim WS              As Worksheet

原来的Sub缩短了:

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

答案 1 :(得分:0)

很难说出你想做什么

但可能是以下内容可以帮助您澄清并(希望)这样做!

首先,你可以&#34;结合&#34;变量名称

所以我要使用一系列命名范围名称(即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

这样你就可以清理你的&#34;主要&#34;子代码,只保留更相关的代码:

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
&#34;主要&#34; 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