发布网友 发布时间:2022-04-27 01:12
共2个回答
热心网友 时间:2022-06-22 01:19
1、使用Application.EnableEvents = False禁用事件,避免赋值时触发Change事件。
2、把代码复制到应用的sheet。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim D_row As Long, j As Long, i As Long
Columns(3).ClearContents
D_row = Range("A" & Cells.Rows.Count).End(xlUp).Row
Debug.Print D_row
For i = 1 To D_row
If Cells(i, 1) <> "" Then Cells(1, 3).Offset(j).Resize(Cells(i, 1).Value, 1) = Cells(i, 2)
j = j + Cells(i, 1).Value
Next i
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
3、下图是运行效果图
热心网友 时间:2022-06-22 01:19
通过VBA代码测试OK,下面是详细代码。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i, j, aCount, aPosition As Integer
aPosition = 0
For i = 1 To 1000
If Sheet6.Cells(i, 1) <> "" Then
aCount = Sheet6.Cells(i, 1)
For j = 1 To aCount
aPosition = aPosition + 1
Sheet6.Cells(aPosition, 3) = Sheet6.Cells(i, 2)
Next
End If
Next
End Sub