VBA Help - Two Nested For Loops

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am working with a piece of code that does the following,

1st loop (For J): Defines my starting row on my "Data Entry" sheet and then steps down every 20 rows (Currently not doing this exactly but its what I need)

2nd loop (For K): Looks at my "Lookups" sheet Column("S2:S" & Lastrow) for a text string which is defined by "BlockCode" as my text variable

So what happens is that the first loop updates the Variable from the second loop BlockCode in cell A4 and then it was intended to step down 20 rows and using the value from the second loop update with the BlockCode string

Here is what the data looks like
Marketing Timing Model v2.8.xlsb
ABC
1
2
3Order
4Block 11
5Block 12
63
74
85
96
107
118
129
1310
1411
1512
1613
1714
1815
20
21
22
23
24Order
25Block 11
26Block 12
273
284
295
306
317
328
339
3410
3511
3612
3713
3814
3915
41
Data Entry



So some explanation to the variables that are clear
Count = how many time the "Blocks" were created which in my test data is currently set to 4. So there are 4 full blocks of data which looks like Row 1:20
BlockCode = is a text String that is dropped to identify the block numbers, so "Block 1", "Block 2" , "Block 3", "Block 4"

Currently my kinda works, problem is I don't have the proper method of stepping down 20 rows based on the variable Count (which tells the code to only do it 4 times)
The code is also not going to the 2nd next J Statement which would update the BlockCode string. Any ideas on what I am doing wrong?

Here is my code:
Code:
Dim J as long, k as long

For j = 4 To Count * 20 'Count * 20 = 4*20 which is 100
            For k = 2 To LastR2
                BlockCode = ws3.Cells(k, 19).Value
                    If ws2.Cells(j, 1).Value = "Block 1" Then
                        ws2.Cells(j, 1).Value = BlockCode
                    End If
            Next k
            Next j
 
Its a text String, but with that swap it did run without error.

Thanks for the additional pass on this.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You mean the cells in column S contain values exactly like
"Block1", "Block2" in the formula bar (without the speech marks?) I'm not sure why then that swap works or runs error free.

And if it ran without error, do you mean it ran successfully or what you were expecting?
 
Upvote 0
So, you were right, I double checked and the text was formatted as a formula which is why the first run failed.

it looked something like ="Block " & T2

T2 = 1

Once I used the formula variation it fixed the code. Glad I now understand how that is different with the SpecialCells syntax.

Thanks again for all the help.
 
Upvote 0
No probs, glad you have something new to try and use in VBA :)
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

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