Kenneth Hobson
Active Member
I think it is going up from the last row and then down one. So, add a blank value above where you want the first output to go. The blank value is not really blank. Add one or more spacebar characters. Sneaky?
I think it is going up from the last row and then down one. So, add a blank value above where you want the first output to go. The blank value is not really blank. Add one or more spacebar characters. Sneaky?
Yeah, sneaky... that worked but when I delete outputs to start fresh I will probably always delete that blank/space in that cell... is there way to put in the code that way I won't have worry about always having to put a space in the cell above where I want the output to go?
If you can think it, you can likely do it.
Since the code goes something like: set r = cells(rows.count, "G").end(xlup)).offset(1), we can check the row number. If r.row < 7 then set r = Range("G7")
LOL... not with everything... I once thought I could fly... that didn't go so well....
where should I put this string??
If rS.Row < 7 Then Set rS = ws.Cells(7, rS.Column)
Generate:
Code:If rS.Row < 7 Then Set rS = ws.Cells(7, rS.Column) Generate:
'Title Cell for first random number set on Picks sheet
Set rStart = ws.Range("E5")
If rS.Row < rStart.row Then Set rS = ws.Cells(rStart.row, rS.Column)
Generate:
Things like that are usually near the start or top.
Code:'Title Cell for first random number set on Picks sheet Set rStart = ws.Range("E5")
As you can see, column E is the start. The 5 does not mean much unless you want row 5 rather than 7. Change the code in #31 post if needed. e.g.
Code:If rS.Row < rStart.row Then Set rS = ws.Cells(rStart.row, rS.Column) Generate:
IF you didn't "get" it, Generate: label was just shown so you could find where to add the line above it. Ctrl+F makes it easy to find such things...
No. rStart is the main one. rS is the one that checks rStart and does what you want. In normal practice, one might want to change rStart. The new rS should never need changing.
Think of it this way. Most will set row 1 values as column headers/titles. The code as it was, would have put the first value in row 2. Howsoever, had the user deleted all data in that column, the first value would be in row 2 still but that just looks silly with no column title...
Sub bGenerate()
Dim ws As Worksheet, rStart As Range, rS As Range
Dim i As Long, ii As Integer, j As Long, k As Long
Dim tf As Boolean, a(1 To 5), b
Dim r As Range, rr
Set ws = Picks
Set r = Picks.Range("A1", Picks.Range("A1").End(xlDown))
'Remove possible duplicate values
rr = UniqueArrayByDict(r.Value) 'Base 0 array.
'Title Cell for first random number set on Picks sheet
'******** Change to suit. Output will start here or next empty row below.**********
Set rStart = ws.Range("E5")
'******** Change to suit. Output will start here or next empty row below.**********
'Check if any preferred numbers were set.
'All blank or all numbers means no preference.
For i = 1 To 5
a(i) = Val(Picks.OLEObjects("TextBox" & i).Object.Value)
If a(i) = 0 Then
j = j + 1
Else
k = k + 1
End If
Next i
'Ok to generate all 5 numbers?
If j = 5 Or k = 5 Then
tf = True
Else 'Add numbers that may not be in rr.
For k = 0 To 4
If a(k + 1) <> 0 Then
ReDim Preserve rr(0 To UBound(rr) + 1)
rr(UBound(rr)) = a(k + 1)
End If
Next k
End If
'Remove possible duplicate values that were added.
rr = UniqueArrayByDict(rr) 'Base 0 array.
If Not IsNumeric(Picks.tSet.Value) Then Picks.tSet.Value = 1
For i = 1 To Picks.tSet.Value
'Next blank cell in Picks sheet for next set of numbers
Set rS = ws.Cells(Rows.Count, rStart.Column).End(xlUp).Offset(1)
'******** Sets rS based on rStart for location of output. *********************************
If rS.Row < rStart.Row Then Set rS = ws.Cells(rStart.Row, rS.Column)
Generate:
b = RndIntPick(1, UBound(rr) + 1, 5) 'Base 0 array.
'Set elements in b to rr values.
For k = 0 To 4
b(k) = rr(b(k) - 1) 'Random picks from col A in b now.
Next k
'Resort b
b = ArrayListSort(b)
If Not tf Then
For ii = 1 To 5
If a(ii) <> 0 And b(ii - 1) <> a(ii) Then GoTo Generate
Next ii
End If
'Fill textbox values and rStart cell or next blank row below it.
For ii = 0 To 4
Picks.OLEObjects("TextBox" & ii + 1).Object.Value = Format(b(ii), "0#")
rS.Offset(, ii).Value = b(ii)
Next ii
Next i
End Sub
I need to take a class or something.. I'm learning it, but it's hard. I'm the type of person who needs to sit there with someone and go over and over and over it. If i don't understand how something works it drives me crazy till I learn it.
Dang it Ken... I did it again..... I messed it up. I added a button to the Pick sheet named Update List so that I can pull in the list of numbers from my other worksheet of my workbook and put the list in column A of Pick sheet.. it works when I leave all textboxes blank to generate all numbers. it doesn't work when I enter a digit into one or more of the textboxes ... getting this error:
View attachment 46300
View attachment 46298
Sub RowsOfNumbersToSingleColumn()
Dim Cell As Range, Joined() As String, Nums(1 To 60) As String
For Each Cell In Sheets("Predictions").Range("D12:R12,D14:R14,D16:R16,D18:R18,D20:R20,D32:M32,D33:K33,D34:I34,D36:M38,D37:K37,D38:I38")
If CLng(Cell.Value) > 0 And CLng(Cell.Value) <= 60 And Len(Cell.Value) > 0 Then Nums(CLng(Cell.Value)) = Format$(Cell.Value, "00")
Next
Joined = Split(Application.Trim(Join(Nums)))
With Sheets("Picks").Range("A1").Resize(UBound(Joined) + 1)
.NumberFormat = "@"
.Value = Application.Transpose(Joined)
End With
End Sub
Ken,
This is the code I'm using with the button I added the Pick Sheet.. still having the problem mentioned above in post #39.. can you help?
Code:Sub RowsOfNumbersToSingleColumn() Dim Cell As Range, Joined() As String, Nums(1 To 60) As String For Each Cell In Sheets("Predictions").Range("D12:R12,D14:R14,D16:R16,D18:R18,D20:R20,D32:M32,D33:K33,D34:I34,D36:M38,D37:K37,D38:I38") If CLng(Cell.Value) > 0 And CLng(Cell.Value) <= 60 And Len(Cell.Value) > 0 Then Nums(CLng(Cell.Value)) = Format$(Cell.Value, "00") Next Joined = Split(Application.Trim(Join(Nums))) With Sheets("Picks").Range("A1").Resize(UBound(Joined) + 1) .NumberFormat = "@" .Value = Application.Transpose(Joined) End With End Sub
Hi ,
If you can upload your complete workbook with all of the code in it , you might get help from other members.
If you can only post the code , you can wait till Kenneth responds.
Narayan
Hi ,
There was no problem with the code ; the problem was with the data item 56 , which had been entered as text instead of as a number.
Correct this and see if the code works correctly.
Once you confirm this , we will look at your second request.
Narayan
Hi Narayan,
I see what you're saying, I stored numbers in the column as numbers, then tried generating some numbers, worked.. thing is though, I would like leading zeros for single digits, and it was working before that way... don't know why it suddenly changed??
Dave
Does using custom format 0# for those cells where needed not suffice?
Of course once you set it as Text format, you should first select those cells and all that might ever be numbers, click the warning arrow and Convert to Number. Then set the selection of cells to the custom format.