递增 VBA For 循环通过命令按钮

Incrementing VBA For loop through command button

美好的一天!

我目前有一个命令按钮的小问题,我希望能够执行以下操作:将特定行格式化为特定行高,为特定数量的单元格添加粗边框同一行并计算并将由此产生的行数添加到文件中的初始数字。基本上,该按钮应该允许传播的用户 sheet 添加一个具有特定格式的新行,用户将在其中输入数据并跟踪添加的行数。

我当前的代码保持原样:

Option Explicit

Private Sub NewLineRedButton_Click()

    Dim i As Long
    Dim y As Long

    For y = ThisWorkbook.Worksheets("Flags").Cells(16.3) To y + 1
        ThisWorkbook.Worksheets("Flags").Cells(16, 3) = y + 1
        For i = 20 To i + y Step 1
            ThisWorkbook.Worksheets("Flags").Rows(i).RowHeight = 45
            ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.LineStyle = xlContinuous
            ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.Weight = xlMedium
        Next
    Next

End Sub

目前代码只执行下面两行并停止。我不太确定,为什么...?

像这样写一个for循环

For y = ThisWorkbook.Worksheets("Flags").Cells(16.3) To y + 1

和这样写是一样的

For y = ThisWorkbook.Worksheets("Flags").Cells(16.3) To 1

我猜单元格中的值为零,因此它将执行循环 0 和 1 - 即您看到的两次。

你需要这样的东西

lEndRow = lStartRow + (lRowCount - 1)
For y = lStartRow to  lEndRow

我现在一切顺利。只是无法让它使用正确的边框来完成所有列......我不确定如何在使用单元格(#,#)表示法时调用它们并且我看不到如何使用 Range(' 'Z#,Z#'') 符号,我的 i 变量是一个 Long...

无论如何:这是目前的结果:

Option Explicit

Private Sub NewLineRedButton_Click()

        Dim i As Long
        Dim y As Long
        Dim RowEnd As Long


        RowEnd = ThisWorkbook.Worksheets("Flags").Cells(Rows.Count, 1).End(xlUp).Row

    For y = 19 To RowEnd
        ThisWorkbook.Worksheets("Flags").Cells(16, 3) = y - 17 ' First row which is already on the sheet is on row 19, first row appearing by button clicking is on row 20 and the program is counting the header hence the y - 17 for the value of the number of rows.
        For i = 19 To RowEnd + 1 Step 1
            ThisWorkbook.Worksheets("Flags").Rows(i).RowHeight = 45
            ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.LineStyle = xlContinuous
            ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.Weight = xlMedium

        Next
    Next

End Sub

感谢您的帮助和想法,终于从这里给出的线索中找到了其他资源。