如何在我的数组中添加超过200个值?

huangapple go评论49阅读模式
英文:

How to add more than 200+ values in my array?

问题

以下是您提供的VBA代码的中文翻译部分:

Sub CopyDataByCity()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim citiesToFilter() As Variant
Dim city As Variant
Dim headerRow As Range
Dim currentRow As Long

' 设置源工作表
Set sourceSheet = ThisWorkbook.Sheets("Portfolio Summary")

' 设置目标工作表
Set targetSheet = ThisWorkbook.Sheets("RAW DATA")

' 定义要按顺序筛选的城市列表
citiesToFilter = Array("Jacksonville, FL", "Houston-The Woodlands-Sugar Land, TX", "Seattle-Tacoma-Bellevue, WA", "Minneapolis-St. Paul-Bloomington, MN-WI", "San Antonio-New Braunfels, TX", "Portland-Vancouver-Hillsboro, OR-WA", "Philadelphia-Camden-Wilmington, PA-NJ-DE-MD", "Boston-Cambridge-Newton, MA-NH", "Nashville-Davidson--Murfreesboro--Franklin, TN", "Columbus, OH", "Indianapolis-Carmel-Anderson, IN", "Raleigh-Cary, NC", "Cincinnati, OH-KY-IN", "Richmond, VA", "Urban Honolulu, HI", "Greensboro-High Point, NC", "Sacramento-Roseville-Folsom, CA", "Colorado Springs, CO", "Stockton, CA", "San Jose-Sunnyvale-Santa Clara, CA", "Harrisburg-Carlisle, PA", "San Francisco-Oakland-Berkeley, CA", "Louisville/Jefferson County, KY-IN", "Reno, NV", "Lakeland-Winter Haven, FL", "San Diego-Chula Vista-Carlsbad, CA", "Fort Collins, CO", "Salt Lake City, UT", "Boulder, CO", "Kansas City, MO-KS", "Baltimore-Columbia-Towson, MD", "Memphis, TN-MS-AR", "Tallahassee, FL", "North Port-Sarasota-Bradenton, FL", "Tucson, AZ")

' 查找源工作表中的最后一行数据
lastRow = sourceSheet.Cells(Rows.Count, "A").End(xlUp).Row

' 获取标题行
Set headerRow = sourceSheet.Range("6:6")

' 设置目标工作表中的起始行
currentRow = 2 ' 假设标题行在第1行

' 遍历要按顺序筛选的城市
For Each city In citiesToFilter
    ' 遍历源工作表中的行
    For i = 8 To lastRow ' 假设标题行在第7行
        ' 检查列Q中的城市名称是否与当前城市匹配
        If sourceSheet.Cells(i, "Q").Value = city Then
            ' 如果城市名称匹配,复制指定的列并粘贴到目标工作表中
            targetSheet.Range("A" & currentRow).Value = sourceSheet.Cells(i, "C").Value ' 复制列
            targetSheet.Range("B" & currentRow).Value = sourceSheet.Cells(i, "H").Value ' 复制列
            targetSheet.Range("C" & currentRow).Value = sourceSheet.Cells(i, "D").Value ' 复制列
            targetSheet.Range("D" & currentRow).Value = sourceSheet.Cells(i, "S").Value ' 复制列
            targetSheet.Range("E" & currentRow).Value = sourceSheet.Cells(i, "I").Value ' 复制列
            targetSheet.Range("F" & currentRow).Value = sourceSheet.Cells(i, "R").Value ' 复制列
            targetSheet.Range("G" & currentRow).Value = sourceSheet.Cells(i, "W").Value ' 复制列
            targetSheet.Range("H" & currentRow).Value = sourceSheet.Cells(i, "N").Value ' 复制列
            targetSheet.Cells(currentRow, "I").Value = city ' 将城市名称粘贴到目标工作表中的列I
            currentRow = currentRow + 1 ' 增加当前行变量
        End If
        
        ' 检查下一行是否为空或城市是否更改
        If sourceSheet.Cells(i + 1, "A").Value = "" Or sourceSheet.Cells(i + 1, "Q").Value <> city Then
            ' 如果下一行为空或城市更改,跳过它
            i = i + 1
        End If
    Next i
Next city
End Sub

希望这个翻译对您有所帮助。如果您需要进一步的帮助,请随时提问。

英文:

I'm trying to use one VBA code which filters the city and paste the data accordingly in another sheet with reference to some column names

I'm trying to make a long list in array but it not taking more than 35 values.

The mentioned code

Sub CopyDataByCity()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim citiesToFilter() As Variant
Dim city As Variant
Dim headerRow As Range
Dim currentRow As Long

&#39; Set the source sheet
Set sourceSheet = ThisWorkbook.Sheets(&quot;Portfolio Summary&quot;)

&#39; Set the target sheet
Set targetSheet = ThisWorkbook.Sheets(&quot;RAW DATA&quot;)

&#39; Define the list of cities to filter on in the desired order
citiesToFilter = Array(&quot;Jacksonville, FL&quot;, &quot;Houston-The Woodlands-Sugar Land, TX&quot;, &quot;Seattle-Tacoma-Bellevue, WA&quot;, &quot;Minneapolis-St. Paul-Bloomington, MN-WI&quot;, &quot;San Antonio-New Braunfels, TX&quot;, &quot;Portland-Vancouver-Hillsboro, OR-WA&quot;, &quot;Philadelphia-Camden-Wilmington, PA-NJ-DE-MD&quot;, &quot;Boston-Cambridge-Newton, MA-NH&quot;, &quot;Nashville-Davidson--Murfreesboro--Franklin, TN&quot;, &quot;Columbus, OH&quot;, &quot;Indianapolis-Carmel-Anderson, IN&quot;, &quot;Raleigh-Cary, NC&quot;, &quot;Cincinnati, OH-KY-IN&quot;, &quot;Richmond, VA&quot;, &quot;Urban Honolulu, HI&quot;, &quot;Greensboro-High Point, NC&quot;, &quot;Sacramento-Roseville-Folsom, CA&quot;, &quot;Colorado Springs, CO&quot;, &quot;Stockton, CA&quot;, &quot;San Jose-Sunnyvale-Santa Clara, CA&quot;, &quot;Harrisburg-Carlisle, PA&quot;, &quot;San Francisco-Oakland-Berkeley, CA&quot;, &quot;Louisville/Jefferson County, KY-IN&quot;, &quot;Reno, NV&quot;, &quot;Lakeland-Winter Haven, FL&quot;, &quot;San Diego-Chula Vista-Carlsbad, CA&quot;, &quot;Fort Collins, CO&quot;, &quot;Salt Lake City, UT&quot;, &quot;Boulder, CO&quot;, &quot;Kansas City, MO-KS&quot;, &quot;Baltimore-Columbia-Towson, MD&quot;, &quot;Memphis, TN-MS-AR&quot;, &quot;Tallahassee, FL&quot;, &quot;North Port-Sarasota-Bradenton, FL&quot;, &quot;Tucson, AZ&quot;)

&#39; Find the last row of data in the source sheet
lastRow = sourceSheet.Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row

&#39; Get the header row
Set headerRow = sourceSheet.Range(&quot;6:6&quot;)

&#39; Set the starting row in the target sheet
currentRow = 2 &#39; Assuming the header row is in row 1

&#39; Loop through the cities to filter on in the desired order
For Each city In citiesToFilter
    &#39; Loop through the rows in the source sheet
    For i = 8 To lastRow &#39; Assuming the header row is in row 7
        &#39; Check if the city name in column Q matches the current city
        If sourceSheet.Cells(i, &quot;Q&quot;).Value = city Then
            &#39; If the city name matches, copy the specified columns and paste into the target sheet
            targetSheet.Range(&quot;A&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;C&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;B&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;H&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;C&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;D&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;D&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;S&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;E&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;I&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;F&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;R&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;G&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;W&quot;).Value &#39; Copy column
            targetSheet.Range(&quot;H&quot; &amp; currentRow).Value = sourceSheet.Cells(i, &quot;N&quot;).Value &#39; Copy column
            targetSheet.Cells(currentRow, &quot;I&quot;).Value = city &#39; Paste the city name to column I in the target sheet
            currentRow = currentRow + 1 &#39; Increment the current row variable
        End If
        
        &#39; Check if the next row is blank or if the city changes
        If sourceSheet.Cells(i + 1, &quot;A&quot;).Value = &quot;&quot; Or sourceSheet.Cells(i + 1, &quot;Q&quot;).Value &lt;&gt; city Then
            &#39; If the next row is blank or if the city changes, skip over it
            i = i + 1
        End If
    Next i
Next city
End Sub

答案1

得分: 9

a) 数组的元素数量没有35个的限制。您达到了VBA编辑器代码行的最大长度限制(1024个字符)。

b) 您可以通过在行末使用续行符号并在下一行继续编写来避免长度限制:

citiesToFilter = Array("Jacksonville, FL", "Houston-The Woodlands-Sugar Land, TX", _
                    "Seattle-Tacoma-Bellevue, WA", (...))

然而,您还有一个可以组合的行数限制,最多为25行,所以也可能会达到限制。

c) 您应该使用一个函数来获取城市列表,以便将程序逻辑和筛选值的创建分开。请记住,此列表可能会更改,或者某个名称可能有错拼,您将很难在代码中找到正确的位置。也许可以这样做:

Function getCityList() As String()
    Dim cityList(1 To 200) As String
    cityList(1) = "Jacksonville, FL"
    cityList(2) = "Houston-The Woodlands-Sugar Land, TX"
    cityList(3) = "Seattle-Tacoma-Bellevue, WA"
    (...)
    getCityList = cityList
End Function

d) 不要在VBA代码中硬编码列表,最好将列表放在工作表中。如果您不希望用户看到该列表,只需将其隐藏。以下代码假定您有一个名为"CityList"的工作表,其中有一个命名范围"CityList"。如果您可以接受列表是一个二维数组(您需要更改For Each city In citiesToFilter语句):

Function getCityList() As Variant
    Dim cityList
    cityList = ThisWorkbook.Sheets("CityList").Range("CityList").Value
    getCityList = cityList
End Function

如果您更喜欢有一个一维数组,只需将其转换:

Function getCityList() As String()
    Dim sheetValues As Variant, i As Long
    sheetValues = ThisWorkbook.Sheets("CityList").Range("CityList").Value
    ReDim cityList(1 To UBound(sheetValues, 1)) As String
    For i = 1 To UBound(sheetValues, 1)
        cityList(i) = sheetValues(i, 1)
    Next
    getCityList = cityList
End Function
英文:

a) There is no limit of 35 elements for an array. You are reaching the maximum length of a code line of the VBA editor (1024 characters).

b) You can avoid that length limit by using line continue character at the end of the line and continue on the next line:

citiesToFilter = Array(&quot;Jacksonville, FL&quot;, &quot;Houston-The Woodlands-Sugar Land, TX&quot;, _
                &quot;Seattle-Tacoma-Bellevue, WA&quot;, (...)

However, there is also a limit of 25 lines that you can combine with that, so you might reach a limit there also.

c) You should use a function to get the list of cities so that you separate the program logic and the creation of the filter values. Have in mind that this list might change or you have a typo in one of the names and you will have a hard time to find the correct place in your code. Maybe something like that:

Function getCityList() As String()
    Dim cityList(1 to 200) as String
    cityList(1) = &quot;Jacksonville, FL&quot;
    cityList(2) = &quot;Houston-The Woodlands-Sugar Land, TX&quot;
    cityList(3) = &quot;Seattle-Tacoma-Bellevue, WA&quot;
    (...)
    getCityList = cityList
End Function

d) Instead of hard code the list in your VBA code, I guess it's a much better idea to have the list in a sheet. If you don't want the user to see that list, just hide it. The following code assumes that you have a sheet "CityList" with a named range "CityList"). If you can live with the fact that the list is a 2-dimensional array (you will need to change the For Each city In citiesToFilter statement)

Function getCityList() As Variant       
    Dim cityList 
    cityList= ThisWorkbook.Sheets(&quot;CityList&quot;).Range(&quot;CityList&quot;).Value
    getCityList = cityList 
End Function

If you prefer to have a one-dimensional array, just convert it:

Function getCityList() As String()        
    Dim sheetValues As Variant, i As Long
    sheetValues = ThisWorkbook.Sheets(&quot;CityList&quot;).Range(&quot;CityList&quot;).Value
    ReDim cityList(1 To UBound(sheetValues, 1)) As String
    For i = 1 To UBound(sheetValues, 1)
        cityList(i) = sheetValues(i, 1)
    Next
    getCityList = cityList
End Function

答案2

得分: 0

根据您的要求,以下是代码部分的中文翻译:

如果我理解您正确...

在sourceSheet中,从第8行开始(第7行是标题),从列A到Z的数据。从第8行开始,直到citiesToFilter中提到的城市名的列Q中的数据。

在targetSheet中,除了A1:H1中的标题(8列)之外,没有数据。

以下的代码是根据上述说明运行的。

预期结果是,在sourceSheet列Q中找到的每个城市单元格的每一行(其条件来自于对citiesToFilter中的每个元素的循环),将找到的城市行的值依次放入targetSheet列A到H,而行是具有数据的最后一行 +1。

Sub test()
    Dim sourceSheet As Worksheet, targetSheet As Worksheet
    Dim rgS As Range, rgT As Range, rgR As Range
    Dim citiesToFilter, el, arrCol, col

    Set sourceSheet = ThisWorkbook.Sheets("Portfolio Summary")
    Set rgS = sourceSheet.Range("Q8", sourceSheet.Range("Q" & Rows.Count).End(xlUp))
    Set targetSheet = ThisWorkbook.Sheets("RAW DATA")
    Set rgT = targetSheet.Range("A2")

    citiesToFilter = Array("Jacksonville, FL", "Houston-The Woodlands-Sugar Land, TX", "Seattle-Tacoma-Bellevue, WA", "Minneapolis-St. Paul-Bloomington, MN-WI", "San Antonio-New Braunfels, TX", "Portland-Vancouver-Hillsboro, OR-WA", "Philadelphia-Camden-Wilmington, PA-NJ-DE-MD", "Boston-Cambridge-Newton, MA-NH", "Nashville-Davidson--Murfreesboro--Franklin, TN", "Columbus, OH", "Indianapolis-Carmel-Anderson, IN", "Raleigh-Cary, NC", "Cincinnati, OH-KY-IN", "Richmond, VA", "Urban Honolulu, HI", "Greensboro-High Point, NC", "Sacramento-Roseville-Folsom, CA", "Colorado Springs, CO", "Stockton, CA", "San Jose-Sunnyvale-Santa Clara, CA", "Harrisburg-Carlisle, PA", "San Francisco-Oakland-Berkeley, CA", "Louisville/Jefferson County, KY-IN", "Reno, NV", "Lakeland-Winter Haven, FL", "San Diego-Chula Vista-Carlsbad, CA", "Fort Collins, CO", "Salt Lake City, UT", "Boulder, CO", "Kansas City, MO-KS", "Baltimore-Columbia-Towson, MD", "Memphis, TN-MS-AR", "Tallahassee, FL", "North Port-Sarasota-Bradenton, FL", "Tucson, AZ")
    arrCol = Array("C", "H", "D", "S", "I", "R", "W", "N")

    For Each el In citiesToFilter
        With rgS
            If .Find(el, lookat:=xlWhole) Is Nothing Then GoTo nxtEL:
            .Replace el, True, xlWhole, , False, , False, False
            Set rgR = .SpecialCells(xlConstants, xlLogical)
            .Replace True, el, xlWhole, , False, , False, False
        End With

        For Each cell In rgR
            For Each col In arrCol
                rgT.Value = sourceSheet.Range(col & cell.Row).Value
                Set rgT = rgT.Offset(0, 1)
            Next
        Set rgT = rgT.Offset(1, 0).End(xlToLeft)
        Next
    nxtEL:
    Next el

End Sub

rgS是sourceSheet中城市数据的范围,从Q8开始,到任何行结束。
rgT是要填充的targetSheet的起始单元格。
arrCol是期望的源列数组,要放入目标表格中。

有3个循环,
A. 对于citiesToFilter中的每个元素的循环
B. 对于rgS中找到的每个单元格的循环
C. 对于arrCol中的每个列名的循环

A的第一次迭代示例...
"Jacksonville, FL"在sourceSheet的Q10、Q15、Q20中找到。
所以这些单元格(Q10、Q15、Q20)存储在rgR变量中。

现在下一个循环是B,循环到rgR中的每个单元格。
然后进入循环C,循环到arrCol中的每个列,并相应地放置结果。

B的第一个迭代示例,循环的单元格是Q10,其行为10。

因此,在循环C的第一次迭代中,sourceSheet.Range(col & cell.Row)是单元格C10,它将C10的值放入rgT ---》sourceSheet单元格A2的C的值。

在循环C的第二次迭代中,sourceSheet.Range(col & cell.Row)是单元格H10,它将H10的值放入下一个rgT。

以此类推,直到循环C的第8次迭代完成。

接下来,B的下一个迭代,循环的单元格是Q15,其行为15。

因此,在循环C的第一次迭代中,sourceSheet.Range(col & cell.Row)是单元格C15,它将C15的值放入下一个rgT......

依此类推,直到循环B的所有单元格完成。

然后,它继续到citiesToFilter中的下一个元素。希望这有助于理解代码的功能。

英文:

If I understand you correctly...<br>

The data in sourceSheet is from column A to Z starting from row 8 (where row 7 is the header). The data in column Q starting from row 8 to whatever is the city name mentioned in citiesToFilter.

In targetSheet, there is no data, except the header in A1:H1 (8 columns).

The code below is run from the explanation above.

Expected result, to each row of the found city cell in sourceSheet column Q (which criteria is coming from the loop to each element in citiesToFilter), put the value of "C", "H", "D", "S", "I", "R", "W", "N" found row city to targetSheet column A to H respectively which row is the lastrow with data + 1.

Sub test()
Dim sourceSheet As Worksheet, targetSheet As Worksheet
Dim rgS As Range, rgT As Range, rgR As Range
Dim citiesToFilter, el, arrCol, col

Set sourceSheet = ThisWorkbook.Sheets(&quot;Portfolio Summary&quot;)
Set rgS = sourceSheet.Range(&quot;Q8&quot;, sourceSheet.Range(&quot;Q&quot; &amp; Rows.Count).End(xlUp))
Set targetSheet = ThisWorkbook.Sheets(&quot;RAW DATA&quot;)
Set rgT = targetSheet.Range(&quot;A2&quot;)

citiesToFilter = Array(&quot;Jacksonville, FL&quot;, &quot;Houston-The Woodlands-Sugar Land, TX&quot;, &quot;Seattle-Tacoma-Bellevue, WA&quot;, &quot;Minneapolis-St. Paul-Bloomington, MN-WI&quot;, &quot;San Antonio-New Braunfels, TX&quot;, &quot;Portland-Vancouver-Hillsboro, OR-WA&quot;, &quot;Philadelphia-Camden-Wilmington, PA-NJ-DE-MD&quot;, &quot;Boston-Cambridge-Newton, MA-NH&quot;, &quot;Nashville-Davidson--Murfreesboro--Franklin, TN&quot;, &quot;Columbus, OH&quot;, &quot;Indianapolis-Carmel-Anderson, IN&quot;, &quot;Raleigh-Cary, NC&quot;, &quot;Cincinnati, OH-KY-IN&quot;, &quot;Richmond, VA&quot;, &quot;Urban Honolulu, HI&quot;, &quot;Greensboro-High Point, NC&quot;, &quot;Sacramento-Roseville-Folsom, CA&quot;, &quot;Colorado Springs, CO&quot;, &quot;Stockton, CA&quot;, &quot;San Jose-Sunnyvale-Santa Clara, CA&quot;, &quot;Harrisburg-Carlisle, PA&quot;, &quot;San Francisco-Oakland-Berkeley, CA&quot;, &quot;Louisville/Jefferson County, KY-IN&quot;, &quot;Reno, NV&quot;, &quot;Lakeland-Winter Haven, FL&quot;, &quot;San Diego-Chula Vista-Carlsbad, CA&quot;, &quot;Fort Collins, CO&quot;, &quot;Salt Lake City, UT&quot;, &quot;Boulder, CO&quot;, &quot;Kansas City, MO-KS&quot;, &quot;Baltimore-Columbia-Towson, MD&quot;, &quot;Memphis, TN-MS-AR&quot;, &quot;Tallahassee, FL&quot;, &quot;North Port-Sarasota-Bradenton, FL&quot;, &quot;Tucson, AZ&quot;)
arrCol = Array(&quot;C&quot;, &quot;H&quot;, &quot;D&quot;, &quot;S&quot;, &quot;I&quot;, &quot;R&quot;, &quot;W&quot;, &quot;N&quot;)

For Each el In citiesToFilter
    With rgS
        If .Find(el, lookat:=xlWhole) Is Nothing Then GoTo nxtEL:
        .Replace el, True, xlWhole, , False, , False, False
        Set rgR = .SpecialCells(xlConstants, xlLogical)
        .Replace True, el, xlWhole, , False, , False, False
    End With

    For Each cell In rgR
        For Each col In arrCol
            rgT.Value = sourceSheet.Range(col &amp; cell.Row).Value
            Set rgT = rgT.Offset(0, 1)
        Next
    Set rgT = rgT.Offset(1, 0).End(xlToLeft)
    Next
nxtEL:
Next el

End Sub

rgS is the range of data of city in sourceSheet, starting from Q8 to whatever row.<br>
rgT is the starting cell to fill in targetSheet.<br>
arrCol is the array of the expected column source to be put into target Sheet.<br>

There are 3 loops, <br>
A. loop for each element in citiesToFilter<br>
B. loop for each found cell in rgS<br>
C. loop for each column name in arrCol

Example of the first iteration of A....<br>
"Jacksonville, FL" is found in Q10, Q15, Q20 of sourceSheet.<br>
So those cells (Q10, Q15, Q20) are in rgR variable.

Now the next loop is B, to loop to each cell in rgR <br>
Then go to loop C, to loop to each col in arrCol and put the result accordingly.<br>

Example of the first iteration of B, the looped cell is cell Q10, which row is 10 <br>

So in the first iteration of loop C, sourceSheet.Range(col &amp; cell.Row) is cell C10 , and it put C10 value into rgT ---> sourceSheet cell A2 first iteration of loop C.

In the second iteration of loop C, sourceSheet.Range(col &amp; cell.Row) is cell H10, and it put H10 value into the next rgT.

And so on until the 8th iteration of loop C finish.

The next iteration of loop B, the looped cell is cell Q15, which row is 15.<br>

So in the first iteration of loop C, sourceSheet.Range(col &amp; cell.Row) is cell C15 , and it put C15 value into the next rgT...

In the second iteration of loop C, sourceSheet.Range(col &amp; cell.Row) is cell H15, and it put H15 value into the next rgT.

And so on until the 8th iteration of loop C finish.<br>
And so on until all the cell of loop B finish.<br>
And then it goes to the 2nd element in citiesToFilter.<br>

Sorry, it's difficult for me to explain.

huangapple
  • 本文由 发表于 2023年4月17日 16:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定