Shuffle an existing column multiple times with no repeats on any row - vba

I am using VBA
I have a column of integers in an array
I run the array thru a randomizer module and copy that to a second column
I run the array thru a randomizer module and copy that to a third column
I run the array thru a randomizer module and copy that to a fourth column
The issue I can't seem to over come is duplicates on the same row
IE:
2 13 27 14
27 27 13 5
14 5 2 13
13 2 14 27
5 14 5 2
*columns are much longer
I have searched the web but everyone is focused on creating random numbers without duplicate. I have found shuffling but once again it seems to be one dimensional.
'// get a random number from 1 to the last row of column 25
For N1 = 1 To LastRow
Again:
Randomize Timer
RNum = Int(Rnd * (LastRow - 1 + 1) + 1)
'// this is where the values in "NArray" get shuffled around
Temp = NArray(N1)
NArray(N1) = NArray(RNum)
NArray(RNum) = Temp
ws5.Cells(N1, Lp) = NArray(N1)
'// this looks for duplicate numbers in the row
Select Case Lp
Case 26: a = ws5.Cells(N1, Lp).Value + 0
b = ws5.Cells(N1, Lp - 1).Value + 0
If a = b Then
GoTo Again
End If
Case 27: a = ws5.Cells(N1, Lp).Value + 0
b = ws5.Cells(N1, Lp - 2).Value + 0
c = ws5.Cells(N1, Lp - 1).Value + 0
If a = b Then
GoTo Again
ElseIf a = c Then
GoTo Again
End If
...
'// the shuffled array is inserted into the cells of column 26
For N1 = 1 To LastRow
ws5.Cells(N1, Lp) = NArray(N1)
Next N1
Next Lp
no errors with the exception that when comparing cell values in the row going from column to column it misses duplicate values such as row 2 column 3 may contain "14" and row 2 column 4 contains "14" it doesn't loop back and get another value for column 4

Related

Excel: Add sequential column using VBA

I am trying to add a column in Excel using VBA for sequential numbers.
Column 1 Column 2
Xyz Data
Zyx Data
Yzx Data
I wish to add a numbered sequence to column 1.
Column 1 Column 2
1 Xyz Data
2 Zyx Data
3 Yzx Data
Also I would like to control where the sequence starts from. For example from 5 onwards....
Column 1 Column 2
5 Xyz Data
6 Zyx Data
7 Yzx Data
Any help is appreciated. Thanks
If you are working on Row 1 to 100 of column A, you could try this:
Dim startNum As Long
Dim offNum As Long
Dim i As Long
Let startNum = InputBox("Start from:")
Let offNum = startNum - 1
For i = 1 To 100
ThisWorkbook.Sheets(1).Cells(i, 1).FormulaR1C1 = "=Row(R" & i & "C1) + " & offNum
Next
Sub enumeration()
Dim j, k As Integer
'determine the number to start with
j = Application.InputBox("What number should I start with?")
'determine how many rows are there to enumerate
k = Cells(1, 2).CurrentRegion.Rows.Count
For i = 0 To k - 2
Cells(i + 2, 1).Value = i + j
Next i
End Sub

Generate table by VBA in Excel

I'm new in VBA. I hope that this is not a difficult question for you.My problem:
I have TEXT in column A and NUMBER in column B. Like this:
Column A Column B
TEXT 1 3
TEXT 2 2
TEXT 3 3
..... ...
I need to auto-generate a table in other sheet which has two columns. First contains the text which repeats n times (NUMBER in column B) and then the next text from Column A. In the second column of this table I need number from 1 to NUMBER. like this:
Column A Column B
TEXT 1 1
TEXT 1 2
TEXT 1 3
TEXT 2 1
TEXT 2 2
TEXT 3 1
TEXT 3 2
TEXT 3 3
.... ....
Then I have to post-process this table, but I know how to make it. I don't know how to generate the table.
Expanding on my comment:
Sub MakeTable()
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim t As String
Dim ws As Worksheet
Sheets(1).Activate
Set ws = Sheets(2)
n = Cells(Rows.Count, 1).End(xlUp).Row()
k = 1
For i = 1 To n
t = Cells(i, 1).Value
m = Cells(i, 2).Value
For j = 1 To m
ws.Cells(k, 1).Value = t
ws.Cells(k, 2).Value = j
k = k + 1
Next j
Next i
End Sub
This assumes that the original data is in Sheet1 and you are transferring it to Sheet2, and that the data begins in row 1. Adjust accordingly if those assumptions are false. The way I determine the last cell in column A that has data is an important idiom in Excel VBA and should be mastered.

Sum values based on cells with an indent

I'm having a certain layout like this:
0
0
0 5
1 6
1
0 7
1 8
1
0
0 9
1 10
1
0 11
1 12
Above is a list with combinations from 000, 001, 010, 011 to 111. The combinations all have a certain value, from 5 to 12.
(The enters are new rows, the spaces are indentlevels of cells, the 5, 6, 7, etc. are in a new column. The 0's and 1's are all in the same column.)
Now I need to have the sum of all the values in which the first 0/1 is 1, the sum of all the values in which the second 0/1 is 1, and the same for the last value. The results in this case must be: 42, 38 and 36
I can't find out how to programm this properly. I was hoping for something like this:
While not Sheets("Sheet 1").Cells(j, 1).indentlevel(2).Value = 0
sum = sum + cells(j,2)
j = j + 1
Wend
But obviously this doesn't work. I can't program this all out without loops, because the codes can be up to 5 didgets (ex. 01101)
Have you thought of using a for loop? Also I think you are using the IndentLevel syntax incorrectly.
With ThisWorkbook.Sheets("Sheet 1")
set rng = Range(.cells(1,1), .cells(3,10)) ' Change this to match your range
End With
For each c in rng
If c.IndentLevel = 2 Then
sum = sum + c
End If
Next c
This doesn't need VBA. The real problem is an inappropriate data format. Update your convention such that each indentation level is moved to a separate column. You'll then be able to proceed with simple Excel formulas.

Selecting elements from one column missing in another column, with VBA

I have two columns, A and B, and I need to find every element on column B that is not on column A, and paste them in a third column, C.
For example:
A B
23 5
1 4
5 7
4 23
1
Then:
C
7
Searching for solutions, people usually suggest VLOOKUP() for similar problems. However, I need something with VBA, because each of these columns is in a different Workbook.
What is a good way to do this comparison?
Thank you
Using loops in Vba
counter = 0
k = 2
For i = 2 To lastrowA
For j = 2 To lastRowB
If Sheet1.Cells(i, "A") = Sheet1.Cells(j, "B") Then
counter = 1
End If
Next j
If counter = 0 Then
Sheet1.Cells(k, "C") = Sheet1.Cells(j, "B")
k = k + 1
End If
counter = 0
Next i
You can use VLookup in VBA. Here is the syntax:
Application.WorksheetFunction.VLOOKUP(lookup_value, table_array, column_index, range_lookup)
Otherwise you could do loops
For each elementB in columnB
For each elementA in columnA
If elmentA <> ElementB then
---Save ElementA in an Array
End if
next elementB
next elementA

What does the To and Step mean in VBA?

Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
Does anyone know how this loop works? I'm confused on the meaning of rows To 1 Step (-1).
from high number To 1 adding (-1) each iteration
Note: It's adding because + AND - in mathematical logic evaluate to a -
If rows = 10 then
for i = 10 to 1 step -2 would mean loop back from 10 to 1 subtracting 2 from the i in each loop cycle.
adding a Debug.Print i inside the loop may give you a better clue.
Note: turn ON the Immediate Window hitting CTRL+G or View => Immediate Window from the VBE menu bar
An example loop increasing by 3 on each cycle.
for i = 1 to 10 step 3
debug.print i
next i
Usage
The step-back technique is mostly used when deleting rows from a spreadsheet.
To see the logic in practice see the following
How to select and delete every 3rd column
Delete entire excel column if all cells are zeroed
Excel VBA - Scan range and delete empty rows
When deleting rows, it is often common practise to start at the end and step backwards, this is so no rows are skipped.
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
'delete row if "delete" is in column 1
If rng3.cells(i,1).Value = "delete" Then
rng3.Rows(i).EntireRow.Delete
End If
next i
Dim i as Integer
For i = 1 To 14 Step 3
Debug.Print i
Next i
In above code loop will iterate from 1 to 14 increment with 3 so output will be like
1 4 7 10 13
It means it can not cross 14 that is limit.
So whatever value is provided in step it will add into the variable use for looping purpose. Here
i = i +3
But in For loop in VBA, Step value can not be changed dynamically. For example:
Dim i As Integer
For i = 1 To 10 Step i
Debug.Print i
Next i
Here, before starting iteration Step is equal to the value of i that is the default value i.e. 0. So i will increment like below:
i = i+ i => i = i+0
So i will not increment here and loop will iterate for ever.
Now for below code:
Dim i as Integer
For i = 1 To 14 Step i+1
Debug.Print i
Next i
i will increment like :
i=i+(i+1) => i= i+(0+1) =>i = i+1
so it will increment by 1 and output will be 1 2 3 .... 14
Now for below code :
Dim i As Integer
i = 3
For i = 1 To 10 Step i
Debug.Print i
Next i
here, i is equal to 3 before loop execution, so Step value will be 3, but loop will start with i = 1 and will increment with 3 through out the loop.
here,
i = i+3
so output will be 1 4 7 10.
Now for some other variable:
Dim i As Integer
Dim j As Integer
j = 2
For i = 1 To 10 Step j
Debug.Print i
j = i
Next i
in above code Step value will be 2, so i will increment by 2 for every iteration whether j is modifying inside loop or not, it will not impact Step value, so output will be
1 3 5 7 9
Please correct me if I miss anything or something is wrong in this. Also suggest if there is any way for dynamic looping using For loop in VBA.