对于 A 列中的每个值,如何在 BC 列中以增量方式创建 9 个 URL,并将 A 列中的值附加到末尾?

For each value in column A, how can I incrementally create 9 URLS in column BC with the value from column A appended to the end?

提问人:Cracker_Jack_5943 提问时间:7/7/2023 最后编辑:Cracker_Jack_5943 更新时间:7/7/2023 访问量:68

问:

我有以下 Excel 电子表格,其中

  • 第 1 行是标题行;
  • A 列是任意字符串;和
  • B 列是空字符串。
一个 B
1 编号 图像 Src
2 机 管 局
3 血型
4 交流
5 广告

我下面的宏应该获取单元格 A2 ('aa') 中的值,将其附加到 URL ('https://test.com/ aa'),然后附加一个介于 1 到 9 之间的数字,后跟字符串“.webp”。示例输出为“https://test.com/aa-1.webp”。

对于 A 列中的每个字符串,应重复 9 次。此外,每个重复的值都将在 B 列中附带一个连续的 URL(例如,请参阅表)。

问题:

所需输出:

一个 B
1 编号 图像 Src
2 机 管 局 https://test.com/aa-1.webp
3 机 管 局 https://test.com/aa-2.webp
... ... ...
10 机 管 局 https://test.com/aa-9.webp
11 血型 https:// test.com/ab-1.webp
... ... ...

宏生成的内容:

一个 B
1 编号 图像 Src
2 机 管 局
3 血型 https://test.com/aa-1.webp
4 https://test.com/aa-2.webp
... ... ...
11 https://test.com/aa-9.webp
12 https://test.com/ab-1.webp
... ... ...

链接到显示问题的电子表格的屏幕截图

  • 该宏在 B 列中按顺序成功创建了 9 个 URL,但它从单元格 B3 而不是 B2 开始。
  • 宏不必要地将 B2 完全留空。
  • 该宏使 A 列中的 ID 完全保持不变。在我的示例中,您可以看到 B 列中的每个 URL 都应该与 A 列中的相应值配对。

到目前为止,我尝试过:

Sub DuplicateCellsAndGenerateURLs()
    Dim lastRow As Long
    Dim sourceRange As Range
    Dim destinationRange As Range
    Dim i As Long
    Dim j As Long
    
    ' Set the range of the source data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' This line will find the last row
    Set sourceRange = Range("A2:A" & lastRow) ' Define the source range
    
    ' Set the destination range in column A and B
    Set destinationRange = Range("A2:B" & (lastRow * 9) + 1) ' Adjust the destination range
    
    ' Duplicate values in column A
    sourceRange.Copy destinationRange.Columns(1)
    
    ' Generate URLs in column B
    i = 2 ' Start index for URL generation
    For Each cell In sourceRange
        If cell.Value = "" Then Exit For ' Stop if an empty cell is encountered in column A
        For j = 0 To 8
            destinationRange.Cells(i + j, "B").Value = "https://test.com" & cell.Value & "-" & j + 1 & ".webp"
        Next j
        i = i + 9 ' Increment index for next URL generation block
    Next cell
End Sub

TL的;DR:字符串 'aa' 应占据 A2:A10;“https://test.com/aa-#.webp”应占据 B2:B10;对 A 列中的每个值重复上述步骤('ab'、'ac'、'ad '等)。

excel vba csv excel-公式 shopify

评论

0赞 MyICQ 7/7/2023
如果你简化一下,理解你的问题会简单得多:使用示例字符串“aa”和“bb”而不是长数字。使用而不是冗长而复杂的 URL,并指出问题所在。另外,请不要使用图像来显示表格。使用纯文本,解释问题。https://test.com/aa-1.webp
0赞 Cracker_Jack_5943 7/7/2023
@MyICQ - 感谢您的输入!我会更新我的帖子。
0赞 Cracker_Jack_5943 7/7/2023
@MyICQ - 已更新。

答:

0赞 Wizhi 7/7/2023 #1

挑战在于,您要从中获取信息的同时覆盖您的(A 列)。 当您执行循环时将更新,因为它是 .因此,我们丢失了初始数据值。sourceRangesourceRangerange()

解决此问题的一种方法是将临时值存储在另一列中。sourceRange

另一种方法是将值存储在数组中,以防止覆盖“源数据”。如果未显式命令 Array,则不会更新它。

Sub DuplicateCellsAndGenerateURLs2()
    Dim lastRow As Long
    Dim sourceRange As Range
    Dim destinationRange As Range
    Dim lrow_bc As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim myArr As Variant
    
    ' Set the range of the source data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' This line will find the last row
    myArr = Application.Transpose(Range("A2:A" & lastRow)) ' Define the ID's to be store in an array
    
    j = 1 'set the intial array value (which array number loop should start from)
    For Each myCell In myArr 'Loop through the array
        If IsEmpty(myCell) Then Exit For ' Stop if an empty cell is encountered in the array (from column A)
            lrow_bc = Cells(Rows.Count, "BC").End(xlUp).Row 'Get the last row for the URL destination, update it for every ID
            For k = 1 To 9
                Cells(lrow_bc + k, "BC").Value = "https://exampleimagesource.blob.core.windows.net/inventory/" & myArr(j) & "-" & k & ".webp" 'To get Array number, use myArr(j)
            Next k
            Range("A" & lrow_bc + 1 & ":" & "A" & lrow_bc + 9).Value = myArr(j) 'Populate the ID by nine rows each time.
            j = j + 1 'update array iteration number
    Next myCell

End Sub

评论

0赞 Cracker_Jack_5943 7/7/2023
你是圣人。这完全解决了我的问题。非常感谢您的努力和时间 - 这已经难倒了我好几天。
0赞 Wizhi 7/7/2023
很高兴它帮助你:)。弄清楚如何使用 A 列并将新值写入其中有点棘手,但这是有趣的挑战(如果您使用,我建议使用虚拟列)。而且我认为您的第一个版本的问题已经足够好了=)。:)有很多有价值的信息可以解决这个问题。range()