英文:
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?
问题
我有以下的Excel电子表格,其中:
- 第1行是标题行;
- 列A包含任意字符串;
- 列B是空字符串。
我的宏应该取单元格A2中的值('aa'),将其附加到一个URL('https://test.com/ aa'),然后附加一个从1到9的数字,后面跟着字符串'.webp'。一个示例输出将是'https://test.com/aa-1.webp'。
对于列A中的每个字符串,应该复制9次。此外,每个复制的值都将伴随着一个顺序的URL在列B中(请参阅表格示例)。
问题:
期望的输出:
- 列A中的每个字符串占据A2:A10;
- 'https://test.com/aa-#.webp'占据B2:B10;
- 对于列A中的每个值('ab','ac','ad'等),重复此过程。
宏实际产生的输出:
- 宏成功在列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
' 设置列A中源数据的范围
lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' 此行将找到最后一行
Set sourceRange = Range("A2:A" & lastRow) ' 定义源范围
' 设置列A和B中的目标范围
Set destinationRange = Range("A2:B" & (lastRow * 9) + 1) ' 调整目标范围
' 复制列A中的值
sourceRange.Copy destinationRange.Columns(1)
' 在列B中生成URL
i = 2 ' URL生成的起始索引
For Each cell In sourceRange
If cell.Value = "" Then Exit For ' 如果在列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 ' 增加下一个URL生成块的索引
Next cell
End Sub
简而言之:字符串'aa'应占据A2:A10;'https://test.com/aa-#.webp'应占据B2:B10;对于列A中的每个值('ab','ac','ad'等),重复此过程。
英文:
I have the following Excel spreadsheet where
- Row 1 is the header row;
- Column A is any string; and
- Column B is an empty string.
A | B | |
---|---|---|
1 | ID | Image Src |
2 | aa | |
3 | ab | |
4 | ac | |
5 | ad |
My macro below is supposed to take the value in cell A2 ('aa'), append it to a URL ('https://test.com/aa'), and then append a number ranging from 1 to 9 followed by the string '.webp'. An example output would be 'https://test.com/aa-1.webp'.
For every string in column A, it should be duplicated 9 times. In addition, each duplicated value will be accompanied by a sequential URL in column B (see table for example).
The Problem:
The desired output:
A | B | |
---|---|---|
1 | ID | Image Src |
2 | aa | https://test.com/aa-1.webp |
3 | aa | https://test.com/aa-2.webp |
... | ... | ... |
10 | aa | https://test.com/aa-9.webp |
11 | ab | https:// test.com/ab-1.webp |
... | ... | ... |
What the macro instead produces:
A | B | |
---|---|---|
1 | ID | Image Src |
2 | aa | |
3 | ab | 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 | |
... | ... | ... |
Link to screenshot of spreadsheet showing the problem
- The macro successfully creates 9 URLs sequentially in column B, but it starts in cell B3 instead of B2.
- The macro needlessly leaves B2 entirely blank.
- The macro leaves the IDs in column A entirely untouched. In my example, you can see that each URL in column B is supposed to be paired with its corresponding value from column A.
What I've attempted so far:
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: string 'aa' should occupy A2:A10; 'https://test.com/aa-#.webp' should occupy B2:B10; repeat for each value in column A ('ab', 'ac', 'ad', etc).
答案1
得分: 0
挑战在于你正在同时覆盖你的sourceRange
(列A),而与此同时你想从中获取信息。sourceRange
在循环时将会更新,因为它是一个range()
。因此,我们失去了最初的数据值。
解决这个问题的一种方法是将sourceRange
的值临时存储在另一列中。
另一种方法是将你的值存储在一个数组中,以防止覆盖"源数据"。如果你不明确命令它,数组不会更新。
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
' 设置列A中源数据的范围
lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' 这一行将找到最后一行
myArr = Application.Transpose(Range("A2:A" & lastRow)) ' 定义要存储在数组中的ID
j = 1 ' 设置初始数组值(循环应从哪个数组号开始)
For Each myCell In myArr ' 遍历数组
If IsEmpty(myCell) Then Exit For ' 如果在数组(来自列A)中遇到空单元格,则停止
lrow_bc = Cells(Rows.Count, "BC").End(xlUp).Row ' 获取URL目标的最后一行,为每个ID更新它
For k = 1 To 9
Cells(lrow_bc + k, "BC").Value = "https://exampleimagesource.blob.core.windows.net/inventory/" & myArr(j) & "-" & k & ".webp" ' 获取数组编号,使用myArr(j)
Next k
Range("A" & lrow_bc + 1 & ":" & "A" & lrow_bc + 9).Value = myArr(j) ' 每次填充九行的ID
j = j + 1 ' 更新数组迭代号
Next myCell
End Sub
请注意,这段VBA代码的目的是复制和生成URL,但在处理sourceRange
时要小心,以避免数据丢失。
英文:
The challenge is that you are overwriting your sourceRange
(column A) at the same time as you want to get information from it. sourceRange
will be updated when you do your loop, since it's a range()
. Therefore we loose the initial data values.
One way to solve this is to store sourceRange
values temporary, in another column.
Another is to store your values in a array so you prevent overwriting the "source data". Array is not updated if you don't explicitly command it to.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论