# For Loops

#### luci_gg

##### New Member
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.

### Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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``````

Tried that and it doesn't work either...

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``````

No luck...

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``````

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.

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.

Replies
1
Views
102
Replies
7
Views
385
Replies
1
Views
122
Replies
6
Views
217
Replies
1
Views
110

1,207,255
Messages
6,077,313
Members
446,278
Latest member
hoangquan2310

### 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.

### Which adblocker are you using?

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

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