excel - 从一个单元格创建多行
问题描述
大家好,如何使用下面的代码来实现如上图所示的截图?因此,如果它尝试拆分单元格,我希望它能够创建新行。
Sub SplitCellValue()
Dim str As String
Dim ArrStr() As String
'Fill variables: str is the value of the active cell, ArrStr splits this value at the comma
str = ActiveCell.Value
ArrStr = Split(str, ", ")
'Loop through each ArrStr to populate each cell below the activecell
For i = 0 To UBound(ArrStr)
ActiveCell.Offset(i, 0).Value = ArrStr(i)
Next i
End Sub
解决方案
在现有循环中添加一个附加For...Next
循环以添加行。包含并If
使用布尔值声明来确定是否添加了行(因此是否需要添加)。
这只会为活动单元格的列插入一个新行。
就像是:
'Loop through each ArrStr to populate each cell below the activecell
Dim i As Long
Dim y As Long
Dim RowsAdded As Boolean
RowsAdded = False
For i = 0 To UBound(ArrStr)
ActiveCell.Offset(i, 0).Value = ArrStr(i)
If RowsAdded = False Then
For y = 1 To UBound(ArrStr)
ActiveCell.Offset(1, 0).EntireRow.Insert xlDown
Next y
RowsAdded = True
End If
Next i
以下是代码执行前后的图片:
前:
后:
推荐阅读
- vue.js - 从 NPM 链接解析别名
- jquery - Timepicker 删除/隐藏默认(30 分钟)时间间隔
- php - Laravel:从视图中添加字段(用户:Mass Assignable Attribute)
- security - 清理系统日志中的密码
- php - 如何在laravel中使用关系检索数据
- jenkins - 如何在归档工件之前触发管道工作流的作业成功
- laravel - 向现有用户 Laravel 5.8 发送电子邮件通知
- css - 了解css中的相对位置
- python - 解决 urlopen 和 request.get 的间歇性滞后时间
- c++ - 重复调用具有非类型模板参数的函数