Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

For loop and If statement Excel VBA

I would really appreciate if you could help me out with this problem. I am trying to get vba to provide me all the numbers in increment of 1 from 1 to a value in cell G1 and populate it into column C. Afterwards, I want vba to check each cell in column C starting from C1 to see if the value is greater than a number and to populate into the column next to it( Column D1 onwards )

For example, cell G1 has the number 5.

So, I should see the following in column c , which are the 1, 2,3,4,5 and in column D I should see only the value for cells greater than say 3. So that means only the value 4, and 5 is populated in columnn D.

I would appreciate any help as I am quite new to VBA and am trying to get a hang of it.

Thnx.

question from:https://stackoverflow.com/questions/65870325/for-loop-and-if-statement-excel-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Give this a try:

Sub elyas()
    Dim i As Long, MagicNumber As Long
    Dim k As Long
    
    MagicNumber = 3
    k = 1
    
    For i = 1 To [G1]
        Cells(i, "C").Value = i
        If i > MagicNumber Then
            Cells(k, "D").Value = i
            k = k + 1
        End If
    Next i
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...