如何使用循环在连续的行中发布

时间:2014-04-16 20:26:49

标签: excel vba excel-vba for-loop

因此,我在下面发布的代码摘录有以下目标:找到1000的所有整数因子。我已经设置了其他所有内容,除非我点击"运行"让它在A列(A2,A3,A4 ......)的每个相应单元格中发布每个因子(1,2,4,5 ......),它会混淆某些内容,将所有因素都发布到A2中,并且我和#39;我只剩下" 1"在A2中,因为这是最近计算的事情。

以下是代码:

Option Explicit

Sub Problem1Sub()

    Dim MyNumber As Single                              'Declare type of variable for number in question
    MyNumber = 1000                                     'Declare value of variable

    Dim Factor As Integer                               'Declare type of variable for the multiple

    For Factor = 1 To MyNumber                              'Specifies loop for all integers between 1 and MyNumber

        Dim TrueFactor As Single                        'Declare variable that is successful even factor of MyNumber
                                                            'This is a single so when TrueFactor values are pasted, if a decimal results,
                                                            'an error is easily identifiable

        Dim TestFactor As Single                        'TestFactor will be used to identify if Factor will result in a whole number
        Dim RowNumber As Single
        RowNumber = 1
        TestFactor = (MyNumber / Factor)

        If Round(TestFactor, 0) = TestFactor Then       'Checks if TestFactor is a whole number
            TrueFactor = TestFactor
            Worksheets("PastedValues").Activate         'Brings up sheet to paste values on
            Range("A1").Select
            ActiveCell.Offset(RowNumber, 0).Select
            ActiveCell.Value = TrueFactor               'Pastes each TrueFactor value into the next cell in column A
        End If

        RowNumber = RowNumber + 1

    Next Factor

End Sub

1 个答案:

答案 0 :(得分:0)

因为RowNumber是在循环中定义的,所以它总是以默认值开始。

编辑: 我改进了代码以使用范围而不是依赖于Select方法:

Option Explicit

Sub Problem1Sub()

    Dim MyNumber As Single                              'Declare type of variable for number in question
    MyNumber = 1000                                     'Declare value of variable

    Dim Factor As Integer                               'Declare type of variable for the multiple
    Dim pastecell As Range
    Set pastecell = Worksheets("PastedValues").Range("a1")

    For Factor = 1 To MyNumber                              'Specifies loop for all integers between 1 and MyNumber

        Dim TrueFactor As Single                        'Declare variable that is successful even factor of MyNumber
                                                            'This is a single so when TrueFactor values are pasted, if a decimal results,
                                                            'an error is easily identifiable
        Dim TestFactor As Single                        'TestFactor will be used to identify if Factor will result in a whole number

        TestFactor = (MyNumber / Factor)

        If Round(TestFactor, 0) = TestFactor Then       'Checks if TestFactor is a whole number
            TrueFactor = TestFactor

            pastecell.Value = TrueFactor               'Pastes each TrueFactor value into the next cell in column A
            Set pastecell = pastecell.Offset(1, 0)
        End If
    Next Factor

End Sub