我试图在使用Variant和Long数据类型的数组输出中保留前导零。当我尝试将D变量切换为String时,出现类型不匹配错误。另一个问题是前导零的数量需要灵活,因为值的长度范围为4到9位。我正在粘贴下面的整个子图,以及输入值和结果的屏幕截图。
提前感谢您的任何建议!
Sub ExpandRanges()
Dim ArrayData As Variant
Dim ArrayOut As Variant
Dim X As Long
Dim D As Long
Dim Index As Long
ActiveSheet.Range("A1:B3").NumberFormat = "@"
ArrayData = Range("A1:B3")
ReDim ArrayOut(1 To Evaluate("Sum(B1:B3-A1:A3+1)"), 1 To 1)
For X = 1 To UBound(ArrayData)
For D = ArrayData(X, 1) To ArrayData(X, 2)
Index = Index + 1
ArrayOut(Index, 1) = D
Next
Next
With ActiveSheet.Range("D1").Resize(UBound(ArrayOut), 1)
.Cells = ArrayOut
End With
End Sub
答案 0 :(得分:0)
你的NumberFormat
应该是" 0000000000"对于保留前导零的10位数字符串。