英文:
I would like to write an Excel VBA for the repeated itmes for several columns
问题
以下是代码部分的翻译:
Sub repeat_items()
'----变量
Dim lrow_d As Long
Dim lrow_s As Long
'-----工作表
Dim s_sht As Worksheet
'-----定义工作表名称
Set s_sht = Worksheets("Repeat")
'--最后一行
lrow_s = s_sht.Cells(Rows.Count, 1).End(xlUp).Row
'---创建重复项
s_sht.Range("D2:D1000000").Clear
For i = 2 To lrow_s '从上到下计算第2列A的数量
lrow_d = s_sht.Cells(Rows.Count, 4).End(xlUp).Row
s_sht.Range("A" & i).Copy Destination:=s_sht.Range("D" & lrow_d + 1 & ":" & "D" & lrow_d + s_sht.Range("B" & i))
' +1是底部下一行,D是列D的位置,B是从列B计算的次数
Next i
End Sub
希望这个翻译对您有帮助。如果您有任何其他问题,请随时提出。
英文:
I would like to know how to write the excel VBA for this repeated items as shown in the photo?
The left is the data I have, and the right is the target answer I want to achieve.
I tried to find the code for that but it is in vain, could anyone please help? thanks.
Sub repeat_items()
'----variables
Dim lrow_d As Long
Dim lrow_s As Long
'-----Sheets
Dim s_sht As Worksheet
'-----Define Sheet names
Set s_sht = Worksheets("Repeat")
'--LastRow
lrow_s = s_sht.Cells(Rows.Count, 1).End(xlUp).Row
'---Creating repeating
s_sht.Range("D2:D1000000").Clear
For i = 2 To lrow_s 'count no. 2 of column A from up to down
lrow_d = s_sht.Cells(Rows.Count, 4).End(xlUp).Row
s_sht.Range("A" & i).copy Destination:=s_sht.Range("D" & lrow_d + 1 & ":" & "D" & lrow_d + s_sht.Range("B" & i))
'+1 is bottom next row 'D is the column D location 'B is calculating number of times from column B
Next i
End Sub
答案1
得分: 0
基于您提供的屏幕截图,以下内容应该适用于您。请确保根据需要更新工作表名称,并根据需要调整目标位置。(如果要将结果放在不同的工作表上,需要添加一个新的工作表变量)。
Sub repeat_items()
'声明和设置变量
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim rData As Range: Set rData = ws.Range("A1").CurrentRegion
Dim rDest As Range: Set rDest = ws.Range("H2")
If rData.Rows.Count = 1 Then Exit Sub '没有数据
Dim aData As Variant: aData = rData.Value
'准备结果数组
Dim aResults() As Variant, iResult As Long
ReDim aResults(1 To WorksheetFunction.Sum(rData), 1 To 1)
'准备循环变量
Dim i As Long, j As Long, k As Long, sResult As String
'循环遍历每一行
For i = 2 To UBound(aData, 1)
sResult = vbNullString '将结果字符串重置为空
'循环遍历每一列
For j = 1 To UBound(aData, 2)
'检查数据中的值是否为数字
If Not IsNumeric(aData(i, j)) Then
'不是数字,使用"_"将文本添加到结果字符串
sResult = sResult & aData(i, j) & "_"
Else
'这是一个数字,循环该数字次数
For k = 1 To aData(i, j)
'每次都将此列标题添加到结果字符串并追加到结果数组
iResult = iResult + 1
aResults(iResult, 1) = sResult & aData(1, j)
Next k
End If
Next j
Next i
rDest.Resize(rDest.CurrentRegion.Rows.Count).ClearContents
rDest.Resize(UBound(aResults, 1)).Value = aResults '输出结果
End Sub
英文:
Based off your provided screenshot, something like this should work for you. Be sure to update worksheet name as necessary, and adjust the destination if you want it elsewhere. (Would need to add a new worksheet variable if you want the destination on a different sheet).
Sub repeat_items()
'Declare and set variables
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim rData As Range: Set rData = ws.Range("A1").CurrentRegion
Dim rDest As Range: Set rDest = ws.Range("H2")
If rData.Rows.Count = 1 Then Exit Sub 'No data
Dim aData As Variant: aData = rData.Value
'Prepare results array
Dim aResults() As Variant, iResult As Long
ReDim aResults(1 To WorksheetFunction.Sum(rData), 1 To 1)
'Prepare loop variables
Dim i As Long, j As Long, k As Long, sResult As String
'Loop through each row
For i = 2 To UBound(aData, 1)
sResult = vbNullString 'Reset the result string to null
'Loop through each column
For j = 1 To UBound(aData, 2)
'Check if the value in the data is a number or not
If Not IsNumeric(aData(i, j)) Then
'Not a number, add the text to the result string with an "_"
sResult = sResult & aData(i, j) & "_"
Else
'This is a number, loop that number of times
For k = 1 To aData(i, j)
'Each time, add this column header to the result string and append it to the results array
iResult = iResult + 1
aResults(iResult, 1) = sResult & aData(1, j)
Next k
End If
Next j
Next i
rDest.Resize(rDest.CurrentRegion.Rows.Count).ClearContents
rDest.Resize(UBound(aResults, 1)).Value = aResults 'Output results
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论