我正在尝试编写一个宏来循环遍历电子表格,并在每一行上将三个值(来自不同列)连接成一个值(整数),并在同一行的另一列中产生。然后,根据该列中不同值的数字从该数字开始递增。每次迭代(包括第一个)都应该复制该行的第一列值,以便每个增量在新创建的输出中反映相同的信息。
这是输入数据的一个示例:
B2,C2和D2都应该连接在一起,产生数字5555551000,并且应该放在单元格I2中。应将A2复制到H2,作为I2的位置标识符。然后I2应该基于F2的值(在这种情况下,10次)递增,并且那些结果应该在I2处从I3开始迭代,其中A2的值在每个输出的单元格的左边。 ,在相应的H列中。
以下是同一张纸上输出的示例:
输出完成以扩展第一个数字范围后,循环继续到下一行并重复该过程。
我通过设置起点,然后使用row.Count进行for循环,并在其中执行Do While循环并根据DID整数打破循环,对此进行了尝试。我没有取得多大成功,也许这与我使用ActiveCell尝试在迭代过程中移动的方式有关。
任何建议将不胜感激。我还不允许嵌入图片,因此外部链接到示例。
答案 0 :(得分:0)
就像其他人已经指出的那样,当你发布代码时,你会更容易理解和解决你想要做的事情,但是我对它进行了一次尝试并提出了以下内容 - 快速说明,这个子需要放在2张不同的纸张上(源纸张,然后是生成的输出纸张)。如果你真的需要它在同一张纸上,那我猜你可以在旧数据的基础上输出数据。
Public Sub sampleSub()
Dim sourceWS As Worksheet
Dim sourceData() As Variable
Dim outputRange As Range
Dim outputArr() As Variable
Dim readCounter As Long
Dim writeCounter As Long
Dim iterationCounter As Long
Set sourceWS = ThisWorkbook.Sheets(1)
sourceData = sourceWS.Range("A2:F4").Value2 'Read in source data
'Redim outout array with enough rows to be able to store each iteration (column F => 10 + 120 + 1000...)
ReDim outputArr(1 To Application.WorksheetFunction.Sum(sourceWS.Range("F2:F4")), 1 To 9)
For readCounter = 1 To UBound(sourceData, 1) 'Loop through each row of source data
For iterationCounter = 0 To sourceData(readCounter, 6) - 1 'Iterates to # in source data column F
writeCounter = writeCounter + 1
outputArr(writeCounter, 1) = sourceData(readCounter, 1) 'Write source data column A to output column A
outputArr(writeCounter, 2) = sourceData(readCounter, 2) 'Write source data column B to output column B
outputArr(writeCounter, 3) = sourceData(readCounter, 3) 'Write source data column C to output column C
outputArr(writeCounter, 4) = sourceData(readCounter, 4) 'Write source data column D to output column D
outputArr(writeCounter, 5) = sourceData(readCounter, 5) 'Write source data column E to output column E
outputArr(writeCounter, 6) = sourceData(readCounter, 6) 'Write source data column F to output column F
outputArr(writeCounter, 7) = sourceData(readCounter, 7) 'Write source data column G to output column G
outputArr(writeCounter, 8) = sourceData(readCounter, 1) 'Write source data column A to output column H
'Concatonate source data columns B, C & D and then adds iteration counter
outputArr(writeCounter, 9) = CLng(sourceData(readCounter, 2) & sourceData(readCounter, 3) & sourceData(readCounter, 4)) + iterationCounter
Next
Next
'User selects target range and then data is outputted
Set outputRange = Application.InputBox("Select target for output:", Type:=8)
outputRange.Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End Sub
希望这有帮助, TheSilkCode