[英]Declaring a set of Variables as a Function of Another Variable
I'm attempting to assign strings based on column entries of which the amount changes (may be 15-40 entries depending on the day), then open URLs by adding the strings to a fixed beginning of the URL. 我试图根据数量发生变化的列条目分配字符串(根据日期可能为15-40个条目),然后通过将字符串添加到URL的固定开头来打开URL。 My issue is the assignment of the variables.
我的问题是变量的分配。 My code is as follows:
我的代码如下:
'LOOP 1:
'Table Entries
Dim RowCount As Integer
Sheets("Table").Select
Sheets("Table").Range("A2").Select
Do While True
ActiveCell.Offset(1, 0).Select
If IsEmpty(ActiveCell.Value) Then
RowCount = ActiveCell.Row
Exit Do
End If
Loop
RowCount = RowCount - 2
'LOOP 2:
'Assign IDs and URLs
Dim ID(1 To RowCount) As Variant
Dim URL(1 To RowCount) As String
Dim i1 As Integer
For i1 = 1 To RowCount
ID(i1) = Sheets("Table").Range("A" + CStr(i + 1)).Value
URL(i1) = "--Redacted--" + CStr(URL(i1))
Next i1
The issue is declaring the ID and URL variables as a function of RowCount. 问题是声明ID和URL变量是RowCount的函数。 I'm sure there is another way to declare these, but my lack of experience is showing.
我敢肯定还有另一种方法来声明这些,但是我缺乏经验表明。
Thanks in advance. 提前致谢。
As you have found: 如您所知:
Sub ThisFails()
RowCount = 9
Dim URL(1 To RowCount) As String
End Sub
does not work, but: 不起作用,但是:
Sub ThisWorks()
Dim URL() As String
RowCount = 9
ReDim URL(1 To RowCount)
End Sub
will work 将工作
Looking at the thing you're trying to accomplish; 查看您要完成的事情; an array with ["--redacted--" + string] you could try:
您可以尝试使用[“ --redacted--” + string]组成的数组:
Sub Try()
Dim URL() As String
With Sheets("Table")
URL = Split("--Redacted--" & Join(Application.Transpose(.Range(.Cells(2, 1), .Cells(1, 1).End(xlDown))), "|--Redacted--"), "|")
End With
End Sub
No need to loop or find an empty cell for your RowCount 无需循环或为RowCount查找空单元格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.