Fill down rows randomly by Loop - excel

I have the values on the range "A1:O1".
Each Column has a unique value in this range.
I need help to develop a loop that will fill down 04 times on each column the same Top Value (Column Value). Below a Pseudo Code
Sub FillDownRowsRandomly()
Dim i As Integer, j As Integer
'RamdomRow=Total of 04 unique ramdom numbers
'choosen from 01 to 06 {1,2,3,4,5,6}
'meaning that in a loop of 6 interations, when fill down
'2 will be Null or empty
'
For i = 1 To 15 'Columns "A" To "O"
For j = 2 To 7 '
'
Cells(RandomRow, i).Value = Cells(1, i).Value
Next j
Next i
End Sub
Below an Image where will be possible identify the result of the code.
Disregard the "Null" word written in the cells. I wrote that just to clarify that during the random loop, the code "ignored that cell".

Maybe something like:
Sub FillDownRowsRandomly()
Dim x As Long, y As Long, z As Long
With Sheet1 'Change accordingly
For y = 1 To 15
z = 0
Do While z < 4
x = Int((7 - 2 + 1) * Rnd + 2)
If .Cells(x, y) <> .Cells(1, y) Then
.Cells(x, y) = .Cells(1, y)
z = z + 1
End If
Loop
Next y
End With
End Sub

Loop the columns and randomly place the values till there are four in the six rows.
Sub FillDownRowsRandomly()
ActiveSheet.Range("A2:O7").ClearContents
Dim i As Long
For i = 1 To 15 'iterate the columns
Do Until Application.CountIf(ActiveSheet.Cells(2, i).Resize(6), ActiveSheet.Cells(1, i).Value) >= 4
Dim j As Long
j = Application.RandBetween(2, 7)
ActiveSheet.Cells(j, i).Value = ActiveSheet.Cells(1, i).Value
Loop
Next i
End Sub

Related

How to copy cell from Sheet 1 to Sheet 2 (in certain sequential) if condition in Sheet 1 is meet

I am a VBA beginner.
I want to copy cells from Sheet 1 into Sheet 2 in a certain sequential (in my case, after every 13 rows) with the condition of this: if any of the D2 to D32 in Sheet 1 is 0, copy A2 to A32 respectively. Then paste it in a sequential of +13 starting from B23 in Sheet 2.
For example:
if D2 is 0, copy A2 and paste it into B23 in Sheet 2.
if D3 is 0, copy A3 and paste it into B36 in Sheet 2.
If D4 is not 0, skip to next.
If D5 is 0, copy A5 and pate it into B49 in Sheet 2.
I feel that it is workable in VBA but I can't seem to figure it out.
I have searched the web but no answer came close to my requirement.
Sub Test()
Sheets("Sheet1").Select
For i = 2 To 32
If Sheets("Sheet1").Cells(i, 4) = 0 Then
Cells(i, 1).Copy
Else
End If
Sheets("Sheet2").Select
For j = 23 To 361 Step 13
Sheets("Sheet2").Cells(j, 2).PasteSpecial Paste:=xlPasteValues
Next j
Next i
End Sub
My current VBA keeps looping in Sheet2 until the end when the condition in Sheet1 is met. That's not what I want.
For flexibility in ranges, some speed using array and avoiding .Select and .PasteSpecial, you could try the following:
Sub Test()
Dim lr As Long, x As Long, z As Long, arr As Variant
With Sheets("Sheet1") 'Change accordingly
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
arr = .Range("A2:D" & lr).Value
End With
For x = LBound(arr) To UBound(arr)
If arr(x, 4) = 0 Then
Sheets("Sheet2").Cells(23 + z * 13, 2) = arr(x, 1)
z = z + 1
End If
Next x
End Sub
If you always just interested in A2:A32 then this will do:
Sub Test()
Dim x As Long, z As Long, arr As Variant
arr = Sheets("Sheet1").Range("A2:D32").Value
For x = LBound(arr) To UBound(arr)
If arr(x, 4) = 0 Then
Sheets("Sheet2").Cells(23 + z * 13, 2) = arr(x, 1)
z = z + 1
End If
Next x
End Sub
You'll benefit from reading this too.
Please try this
Sub Test()
Dim i As Integer, n As Integer
Sheets("Sheet1").Select
n = 0
For i = 2 To 32
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(i, 4) = 0 Then
Cells(i, 1).Copy
Sheets("Sheet2").Activate
Sheets("Sheet2").Cells(23 + (n * 13), 2).PasteSpecial Paste:=xlPasteValues
n = n + 1
Else
End If
'Sheets("Sheet2").Select
' For j = 23 To 361 Step 13
' Sheets("Sheet2").Cells(j, 2).PasteSpecial Paste:=xlPasteValues
' Next j
Next i
End Sub
Try this one:
Sub Test()
Dim i,j as integer
j= 1
Sheets("Sheet1").Activate
For i = 2 To 32
If Sheets("Sheet1").Cells(i, 4) = 0 Then
Sheets("Sheet2").Cells(10 + 13 * j, 2).Value2 = Cells(i, 1).Value2
j = j + 1
End If
Next
End Sub
Hope it helps

Looping through an array in Excel

Trying to loop through a sheets"data".Range"AM1:AS12" and copy the data to range beginning at BD1 as long as the data doesn't equal "#N/A"
My code works with copying the first column, but doesn't do anything with the data after that. Where am I going wrong?
Set S2 = Sheets("data").Range("AM:AM")
Set S3 = Sheets("data").Range("BD:BD")
Dim i As Integer, j As Integer
j = 1
For i = 1 To 12
If S2.Cells(i, 1).Value <> "#N/A" Then
S3.Cells(j, 2).Value = S2.Cells(i, 1).Value
j = j + 1
End If
Next i
Replace:
<> "#N/A"
By:
Not(Application.WorksheetFunction.IfNa(...))
This works when i tested it.
Sub CopyCell()
Set S2 = Sheets("data").Range("A:A")
Set S3 = Sheets("data").Range("M:M")
Dim i As Integer, j As Integer
For j = 1 To 2
For i = 1 To 12
If S2.Cells(i, j).Value <> "#N/A" Then
S3.Cells(i, j).Value = S2.Cells(i, j).Value
End If
Next i
Next j
Call DeleteBlank
End Sub
Sub DeleteBlank()
Dim x As Integer
Dim y As Integer
For y = 13 To 16 'Range numbers for the columns the data is copied to
For x = 1 To 10 ' Number of cells of data you want to loop through
If Cells(x, y).Value = "" Then
Cells(x, y).Delete Shift:=xlUp
End If
Next x
Next y
End Sub
the best thing to is not to check if it is equal to "#N/A"
The best is to check if it is an error : If Not (IsError(S2.Cells(i, 1).Value)) Then

How to Loop Through Every OTHER Column Using VBA

So I'm trying to compare dates in the chart pictured. I want to compare cells 1 and 2 and if the dates are the same then move to 3 and 4 and do the same comparison and then move to 5 and 6 and so on. If the dates are different I want to add 1 to a counter. Then at the end of each row I need to fill the cell at the end of the row with the 0 in it currently with the counter value and then reset the counter and move to the next row and so on. so the circled counter should read 1 because there is one pair of different dates. The code i have so far is attached. Currently it tells me "Object required" at Set CompD1. Pretty new to this so any help is appreciated.
Dim i As Integer
Dim j As Integer
Dim AdjPln As Integer
Dim CompD1 As Range
Dim CompD2 As Range
Dim cRow As Integer
For i = 0 To 49
AdjPln = 0
cRow = i + 13
For j = 0 To 9
Set CompD1 = Cells(cRow, j + 5).value
Set CompD2 = Cells(cRow, j + 6).value
If CompD1 = CompD2 Then
j = j + 2
Else
AdjPln = AdjPln + 1
j = j + 2
End If
Next j
Cells(cRow, 24) = AdjPln
Stop
Next i
I think your j loop is the issue when you tried to change the value with the formula to increment by 2 instead of 1. When you went to loop by increments other than one, you can use the Step option. In your case, you want to loop j by 2 so Step 2. You can also do negative if that is useful.
See if this works:
Dim i As Integer, j As Integer, AdjPln As Integer, cRow As Integer
For i = 0 To 49
AdjPln = 0
cRow = i + 13
For j = 0 To 9 Step 2
If Cells(cRow, j + 5).Value <> Cells(cRow, j + 6).Value Then
AdjPln = AdjPln + 1
End If
Next j
Cells(cRow, 24) = AdjPln
Next i
It appears that this could be done with a simple formula. Put this in X13 and drag down.
=SUMPRODUCT((E13:U13<>F13:V13)*(E13:U13<>"")*(F13:V13<>"")*ISODD(COLUMN(E13:U13)))

Can't make Loop on Excel VBA and print results

I am new to Excel VBA and I want to calculate the distance between two atoms and make a loop to calculate it for all wanted cases
with coordinate B(i), C(i), D(i) in the Excel sheet correspond to x,y,z cartesian coordinate..
these atoms are located : One in a row (i) and the other in a row (i+5)
I write this algorithm but I cant transfer it to excel VBA
For i=4 to 1000
For j=9 to 1000
d=SQRT(POWER(B(i)-B(j),2)+ POWER(C(i)-C(j),2)+ POWER(D(i)-D(j),2))
print **d** in (P(i)) #want to print the distance **d** in a case
j=j+4 # **j** is a multiple of 4
i=i+4 # **i** is a multiple of 4
next i
Thanks, this is my first question
I think that the following should work for you:
Sub FindDistances()
Dim i As Long, j As Long
Dim r As Long, c As Long 'row and column indices for output
Dim data As Variant
Application.ScreenUpdating = False 'useful when doing a lot of writing
data = Range("B4:D1000").Value 'data is a 1-based array
c = 5 'column E
For i = 1 To UBound(data) - 5 Step 4
r = 1 'first row printed in -- adjust if need be
For j = i + 5 To UBound(data) Step 4
Cells(r, c).Value = Sqr((data(i, 1) - data(j, 1)) ^ 2 + (data(i, 2) - data(j, 2)) ^ 2 + (data(i, 3) - data(j, 3)) ^ 2)
r = r + 1
Next j
c = c + 1
Next i
Application.ScreenUpdating = True
End Sub
Something like this? In VBA, you refer to cells like Cells(row, column). Data is supposed to be located in a worksheet named Sheet1. I'm calculating each dimension separately (d1, d2, d3) just for reading simplicity. You can merge those four lines in one if you like. EDIT: reading your comments above, I add a nested loop (j).
Sub Distances()
Dim i As Integer
Dim j As Integer
Dim d1 As Double, d2 As Double, d3 As Double, d As Double
For i = 4 To 1000 Step 4 'Can't understand your data, but Step 4 tries to account for your j=j+4 and i=i+4
For j = 9 To 1000 Step 4
d1 = (Worksheets("Sheet1").Cells(i, 2) - Worksheets("Sheet1").Cells(j, 2)) ^ 2
d2 = (Worksheets("Sheet1").Cells(i, 3) - Worksheets("Sheet1").Cells(j, 3)) ^ 2
d3 = (Worksheets("Sheet1").Cells(i, 4) - Worksheets("Sheet1").Cells(j, 4)) ^ 2
d = Sqr(d1 + d2 + d3)
Worksheets("Sheet1").Cells(i, 16).Value = d
Next j
Next i
End Sub
Option Explicit
Sub AtomDistance()
'
' AtomDistance Macro1
'
'
Dim i As Integer
Dim j As Integer
Dim Distance As Double
Dim Column As String
Column = InputBox("Which column you want to print results(put a letter)?")
Dim MyCell11 As String
Dim MyCell12 As String
Dim MyCell13 As String
Dim MyCell21 As String
Dim MyCell22 As String
Dim MyCell23 As String
Dim MyCell3 As String
j = 9
For i = 4 To 12
MyCell3 = Column & i
MyCell11 = "B" & i
MyCell12 = "C" & i
MyCell13 = "D" & i
MyCell21 = "B" & j
MyCell22 = "C" & j
MyCell23 = "D" & j
Distance = (((Range(MyCell11).Value - Range(MyCell21).Value) ^ 2) + ((Range(MyCell12).Value - Range(MyCell22).Value) ^ 2) + ((Range(MyCell13).Value - Range(MyCell23).Value) ^ 2)) ^ 0.5
If i Mod 4 = 0 Or j Mod 4 = 0 Then
Range(MyCell3).Value = Distance
End If
j = j + 1
Next i

Compare each and every cell in a colum by custom macro (unhandy for loop)

i'm currently trying to compare each and every cell in a column with each other in order to find duplicates. I wrote below code, i know it as possible via default Excel functions, but i would like to write a macro with the above mentioned function. Excel currently doesn't respond when i run my code, my guess is that i run a double for loop with 14K cells to compare is each resulting in 14.000*14.000 loops, which is kinda unhandy. Any help will be appreciated :).
Sub findidentical()
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim ColumnG As Integer
x = 0
ColumnG = Application.WorksheetFunction.CountA(Range("G:G"))
'ColumnG is 14K cells long'
For i = 2 To ColumnG
For j = 1 + 1 To ColumnG
If Cells(i, 7).Value = Cells(j, 7).Value Then
x = x + 1 & Cells(i, 7).Font.Bold = True & Cells(j, 7).Font.Bold = True
End If
Next j
Next i
Range("L25").Text = "Amount of duplicates"
Range("L26").Value = x
End Sub
Try this:
Sub findidentical()
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim ColumnG As Integer
x = 0
ColumnG = Application.WorksheetFunction.CountA(Range("G:G"))
'ColumnG is 14K cells long'
For i = 2 To ColumnG
For j = i + 1 To ColumnG
If Cells(i, 7).Value = Cells(j, 7).Value Then
x = x + 1
Cells(i, 7).Font.Bold = True
Cells(j, 7).Font.Bold = True
End If
Next j
Next i
Range("L25").Text = "Amount of duplicates"
Range("L26").Value = x
End Sub
First you need to make each command in the if statement its own line or separate them with : instead of &. & is a string concatenation not a command separator.
Second there is no reason to start Loop j at 2 it has already been compared to everything above. So start it at i+1

Resources