gpt4 book ai didi

Excel VBA : Create hierarchical list in form of "I-1, I-2, II-1, II-2" dynamically based on input

转载 作者:行者123 更新时间:2023-12-05 01:49:33 26 4
gpt4 key购买 nike

我想创建一个“I”列格式的动态编号列表,它将由 B1 和 B2 中的值生成:example picture

罗马数字和阿拉伯数字应该是动态的,基于 B1 和 B2 中的单元格值。我已经尝试过 VBA,但我无法计算出 for 循环来生成正确的结果。另外我不确定如何实现罗马数字。到目前为止,这是我的代码:

Private Sub CommandButton1_Click()
Dim Roman_Number As Integer
Dim Arabic_Number As Integer

Roman_Number = Range("B1").Value
Arabic_Number = Range("B2").Value

For i = 0 To (Roman_Number - 1)
Cells(i + 1, 5).Value = "I" & "-" & (i + 1)
Next i

End Sub

非常感谢您!

最佳答案

有一个生成罗马数字的工作表函数,您将需要嵌套循环,因此代码应如下所示:

Option Explicit
Sub Button1_Click()
Dim Roman_Number As Integer
Dim Arabic_Number As Integer
Dim row As Integer
Dim i As Integer
Dim j As Integer

Roman_Number = Range("B1").Value
Arabic_Number = Range("B2").Value

row = 1

For i = 1 To Roman_Number
For j = 1 To Arabic_Number
Cells(row, 5).Value = WorksheetFunction.Roman(i) & "-" & j
row = row + 1
Next j
Next i
End Sub

只要数字不是太大,整数就可以。

enter image description here

关于Excel VBA : Create hierarchical list in form of "I-1, I-2, II-1, II-2" dynamically based on input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74069722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com