Help with a for loop

cspengel

Board Regular
Joined
Oct 29, 2022
Messages
173
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a cell "B259" that I want the value of each cell in column A going from cell A3:A253 to replace what is is in B259 upon each loop. This is my current code.

VBA Code:
Sub Generate()

 For x = 1 To 350
  
Worksheets("Sheet3").Range("B254:L259").Calculate
If Range("K256").Value < 35000 And Range("M256").Value = 9 Then
Worksheets("Sheet3").Range("B256:M256").Copy
Worksheets("Sheet4").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next x
End Sub

Basically I need another for loop within the current for loop that goes through each cell in A.. Changes value in B259 to reflect which value it's on, all the way to 253. I'd appreciate any assistance. BTW Sheet3 is what everything is on.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I think I got something working.
VBA Code:
Set dic = CreateObject("Scripting.Dictionary")

With Sheets("Sheet3")
        Set rng = .Range(.Range("A3"), .Range("A" & .Rows.Count).End(xlUp))
    End With
    
 For Each cel In rng
        If Not dic.exists(cel.Value) Then
            dic.Add cel.Value, cel.Value
        End If
    Next cel
    
 For x = 1 To 100
 For Each ky In dic.keys
       Range("B259").Value = dic(ky)
   

Worksheets("Sheet3").Range("B254:L259").Calculate
If Range("K256").Value < 35000 And Range("M256").Value = 9 Then
Worksheets("Sheet3").Range("B256:M256").Copy
Worksheets("Sheet4").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next ky
Next x
End Sub
 
Upvote 0
If you want to elaborate on what you are trying to do and we can make some suggestions.
Using a dictionary only adds value if you are saying that column A has duplicate values and you only want to use each unique value once.
Your "For x" loop is not changing anything apart from pushing the output row down one line, this means that you are performing the exact same inner loop x times, so you will have x sets of the same data. Is that really what you are aiming for ?
PS: You will find it easier to read the code if you indent the lines inside each loop.
 
Upvote 0
Appreciate the reply Alex. I think what you are describing is what I am aiming for. I have a list of MLB Teams along with their lineups that update for the day.
lineup.png



If I place a number next to the players name. It will add it into my lineup selector at the bottom. Like such


select.png


Now if you look at the blank spots in the team, OF OF OF UTIL needs to be filled. So essentially what I am doing is looping through each team in column A, It will put the team where the "TEAM STACK" text is and fill the whole row with players that could fill out the lineup. it will randomly select a player 100 times to fill the open spots with 4 players from that team and fill the spots. The column after the generate button will test if it meets salary cap restrictions and that no name is duplicated. If everything is met, it will copy the lineup to a new sheet. The purpose of looping the same team 100 times is to assure it gets through the players that don't fit and that I am generating every lineup that fits with each team. It seems to be pretty dang quick to. I appreciate your suggestion on indenting my loops!
 
Upvote 0
I am not really following how it works. If you are saying that it does what you need it to do, then I will leave it there.
If not then can you provide a sample of your workbook via a sharing platform showing a few lines of the expected results and talk me through how you get from your initial data to the results you are expecting.
VBA (and formulas) need cell references and your images don't include the columns and row identifiers. They also don't give us sheet names. An XL2BB samples would help, as does a copy of the desensitised verion of the workbook. It also gives us some test data to work with.
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,952
Members
449,198
Latest member
MhammadishaqKhan

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