且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将变量连接到要设置为VBA范围的字符串

更新时间:2023-02-06 18:02:03

虽然这样创建范围一般都是皱眉的,但是这样做的方式是使用SET像@Gary McGill在评论中指出)。以下是一个如何做的例子:

Although creating ranges like this is frowned upon in general, the way to do it is with the word SET (like @Gary McGill stated in the comments). Here is an example of how to do this:

Sub test()

Dim alphabet As String
Dim totHrdrLngth As Long
Dim belowRowCount As Long
Dim rowCount As Long
Dim inRange As Range

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
totHrdrLngth = 5
belowRowCount = 10
rowCount = 5

' Gives us A5:E10
Set inRange = Range("A" & rowCount & ":" & range2 & _
                    Mid$(alphabet, totHrdrLngth, 1) & belowRowCount)

End Sub

您正在当前范围内运行此宏,因此不需要指定ActiveSheet.Range。我希望这有助于您达到您尝试实现的目标。

You are running this macro in the current range, so there should be no need to specify ActiveSheet.Range. I hope this helps get you toward what you are trying to achieve.