简体   繁体   English

Excel VBA将字符串转换为范围

[英]excel vba convert string to range

I am trying to run a macro on 3 different ranges, one after another. 我试图在3个不同的范围上运行宏,一个接一个。 Once the range is selected, the code works just fine (where variables F and L are defined). 一旦选择了范围,代码就可以正常工作(其中定义了变量F和L)。 I would like to set r1-r3 as Ranges I need and then use a string variable to concatenate the range numbers together. 我想将r1-r3设置为所需的范围,然后使用字符串变量将范围数字连接在一起。 This code works, but doesn't provide the starting and ending row number in the range selected. 该代码有效,但不提供所选范围内的开始和结束行号。 This is vital because it tells the " TableCalc " macro when to start and stop the code. 这很重要,因为它告诉“ TableCalc ”宏何时启动和停止代码。 I would then like to move on to the next range. 然后,我想继续下一个范围。 Thanks for your help. 谢谢你的帮助。

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

This is not the answer (as part of your code and what you are trying to achieve is unclear yet), but it is a "cleaner" and more efficient way to code what you have in your original post. 这不是答案(作为代码的一部分以及您要实现的目标尚不清楚),但这是一种对原始帖子中的内容进行编码的“更干净”,更有效的方法。

Option Explicit

Dim WS              As Worksheet

Your original Sub shorten: 您原始的Sub缩短:

Sub TestRangeBC()

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

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

End Sub

This Sub gets the Name of the Named Range (you set in your workbook) as a String, and sets the Range accordingly. 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

What are you trying to test in the loop below, what are the criteria of staying in the loop ? 您想在下面的循环中测试什么,停留在循环中的标准是什么?

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

it's really hard to tell what you may want to do 很难说出您想做什么

but may be what follows can help you clarifying and (hopefully) doing it! 但以下可能会帮助您澄清和(希望)做到这一点!

first off, you can't "combine" variable names 首先,您不能“组合”变量名

So I'd go with an array of named ranges names (ie String array) to be filled by means of a specific sub: 因此,我将使用一个命名范围名称数组(即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

so that you can clean up your "main" sub code and keep only more relevant code there: 这样您就可以清理“主要”子代码,并仅在其中保留更多相关的代码:

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

the "main" sub loops through the named ranges array directly collected from GetRanges() and calls DoIt() to actually process the current one: “ main”子遍历直接从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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM