For Loops

luci_gg

New Member
Joined
Oct 27, 2011
Messages
13
Hi,

I have written a very simple code to understand how the for loops work in VBA. I would like to add the letters dd in column 3 if column 2 = F2000.
This is what I have:

Sub dd()
Dim i As Long
Dim j As Long
For i = 1 To i = 3
For j = 1 To j = 2
If Cells(i, 2) = F2000 Then
Cells(i, 3) = dd
End If
Next j
Next i

End Sub

Why is this not working?? I don't get any error messages from excel but nothing happens.

Thanks for your help!!!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
dd is a variable name with no value assigned. Try

Code:
Sub dd()
Dim i As Long
Dim j As Long
For i = 1 To i = 3
    For j = 1 To j = 2
        If Cells(i, 2) = F2000 Then
            Cells(i, 3).Value = "dd"
        End If
    Next j
Next i

End Sub
 
Upvote 0
Sorry, it should be

Rich (BB code):
Sub dd()
Dim i As Long
Dim j As Long
For i = 1 To i = 3
    For j = 1 To j = 2
        If Cells(i, 2) = "F2000" Then
            Cells(i, 3).Value = "dd"
        End If
    Next j
Next i

End Sub
 
Upvote 0
Tested and working

Code:
Sub dd()
Dim i As Long
Dim j As Long
For i = 1 To 3
    For j = 1 To 2
        If Cells(i, 2) = "F2000" Then
            Cells(i, 3).Value = "dd"
        End If
    Next j
Next i
End Sub
 
Upvote 0
Oddly enough the above code wasn't working for me either. What I did is copy the F2000 from the VBA code and paste it into B1,B2 and B3 and it worked.
 
Upvote 0
This is very random! I had to create a new excel and copy the code and it worked...
Thanks for your help, very much appreciated.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top