Auto down to next row if mod 3 = 0 - excel

I try to write code to insert value in a range have 3 column and row based on array of values. I need to insert value from 1 to 1000 to my range as 1,2,3 next row, 4,5,6 next row, 7,8,9 next row until 1000.
Please someone help me, I have tried to find in stackoverflow but no result match

Try this:
Sub test2()
Dim i As Integer, r As Integer
r = 1
For i = 1 To 1000
If i Mod 3 = 0 Then
Cells(r, 3) = i: r = r + 1
Else
Cells(r, i Mod 3) = i
End If
Next i
End Sub

If you are about Excel;
If first row is 1 :
Col1 = (ROW() - 1) * 3 + 1,
Col2 = (Row() - 1) * 3 + 2,
Col3 = (ROW() - 1) * 3 + 3
Else : #fr = row number of first row :
Col1 = (ROW() - #fr) * 3 + 1,
Col2 = (ROW() - #fr) * 3 + 2,
Col3 = (ROW() - #fr) * 3 + 3
A Better Way is use This : (#fr = 1 if first row is 1)
=(ROW() - #fr) * 3 + COLUMN()
In VBA add this:
Public Function myFunc(row as integer, col as integer, Optional fr As Integer = 1) As Integer
myFunc = (row - fr) * 3 + Col + 1
End Function
Then use it.
For you:
Sub test2()
Dim i As Integer, row As Integer, col As Integer
a = 1
For i = 1 To 1000
row = (i - 1) \ 3 + 1
col = (i - 1) mod 3 + 1
Cells(row, col) = myfunc(row, col)
Next i
End Sub

Related

VBA: How to call an Userform Object by its name (Loop)

I just started to learn VBA for a project and i have a userform where i have multiple sliders that give me values that i want to use to build a 2D Array with For loops.
My Array is 5 by 5 and it is "Symmetric" (1 as diagonal, Thus I only need one side for the values).
I obtain the matrix values after converting values obtained from sliders in the GUI, which i named Sld1v2 (for row 1, col 2), Sld1v3 (for row1, col3) etccc.
I am thus looking for a way to call the right Slider (by its custom name) in the for loops but i cannot figure how i can do it, can you help me ? I currently have a Type Error, when running the line --> JudgementMatrix(lig,col) = JudgementVector(....)
Following you can see the Excel Version of what i want to code and my attempt of a script.
I hope my request is clear :)
EDIT: By changing Variant type to Double for the Matrix and Vector (since i saw my vector had different types within). I now get an error when i call the function Array. But the thing is that Array returns a variant! --> Filled the Vector manually
Screenshot of the Excel of what i want to code
Dim JudgementVector(16) As Double
Dim JudgementMatrix(4, 4) As Double
Dim lig As Integer
Dim col As Integer
For i = 0 To 16
If i < 8 Then
JudgementVector(i) = (1 / (9 - i))
Else
If i >= 8 Then
JudgementVector(i) = i - 7
End If
End If
Next i
For lig = 0 To 4
For col = 0 To 4
If col = lig Then
JudgementMatrix(lig, col) = 1
Else
If col > lig Then
JudgementMatrix(lig, col) = JudgementVector(UserForm1.Controls(["sld"&lig&"v"&col]).Value + 8)
Else
If lig > col Then
JudgementMatrix(lig, col) = 1 / JudgementMatrix(col, lig)
End If
End If
End If
Next col
Next lig
Apparently it didnt like having "Text" & variable & "Text" & Variable
so i by passed it by creating temp values to combine step by step.
Dim JudgementVector(16) As Double
Dim JudgementMatrix(4, 4) As Double
Dim lig As Integer
Dim col As Integer
Dim Temp1 As String
Dim Temp2 As String
Dim Temp3 As String
' JudgementVector = ([{1 / 9, 1 / 8, 1 / 7, 1 / 6, 1 / 5, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5, 6, 7, 8, 9}])
For i = 0 To 16
If i < 8 Then
JudgementVector(i) = (1 / (9 - i))
Else
If i >= 8 Then
JudgementVector(i) = i - 7
End If
End If
Next i
For lig = 0 To 4
For col = 0 To 4
If col = lig Then
JudgementMatrix(lig, col) = 1
Else
If col > lig Then
Temp1 = "sld" & lig + 1
Temp2 = "v" & col + 1
Temp3 = Temp1 & Temp2
JudgementMatrix(lig, col) = JudgementVector(UserForm1.Controls(Temp3) + 8)
Else
If lig > col Then
JudgementMatrix(lig, col) = 1 / JudgementMatrix(col, lig)
End If
End If
End If
Next col
Next lig

Compare Rows with Header then insert value in column with duplicate check in VBA

Hi Everyone I'm new to VBA Code & I'm Stuck with this one. I hope someone help me..
Step 1: compare the row with every SU headers. Here in row2, the value in SU_BS is 211. Once there is a value in SU , the column NAME2 should display the corresponding NAME (s1) with SU value (211). So NAME2 should display s1 211.
Step 2: Once NAME2 display the value, want to move to the next row without further checking SU headers.
Step 3: As per step 1 & 2 the column NAME2 in Next row will also display S1 211. But what i need is while filling the value, NAME2 column should check for the duplicate value. If duplicate value exist the control moves to next SU header. If the SU header has no value, move on to the next SU header if value exist NAME2 should display the value (S1 ZY) in row3.
Use an array to store each row and previous row in turn, compare elements until different.
Option Explicit
Sub macro()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim r As Long, c As Integer, n As Integer
Dim k As Integer, kLast As Integer
Dim ar(4, 1) As String
r = 2 ' start row
While Len(ws.Cells(r, 1)) > 0
' fill array
For n = 0 To 3
c = n * 2 + 4
ar(n, 1) = ws.Cells(r, c)
Next
k = 0
If r > 2 Then
' compare
While k <= kLast And ar(k, 1) = ar(k, 0)
k = k + 1
Wend
' skip pass any blanks
While Len(ar(k, 1)) = 0 And k <= 3
k = k + 1
Wend
End If
' output
ws.Cells(r, 3) = ws.Cells(r, 2) & " " & ar(k, 1)
' remember last row
For n = 0 To 3
ar(n, 0) = ar(n, 1)
Next
kLast = k
r = r + 1
Wend
MsgBox "Done"
End Sub

simple VBA function that I programmed produces #VALUE! error when used in excel

The function is designed to take in input - variable pg - that is in a cell on the spreadsheet, go through the rows of data to see which row in a column 1 matches variable pg. Once the match is found, it then goes through the columns to see which of the columns has "VRP23" Or "VRP24" in the first row. When that is found, it takes the number of the matching row/column and performs the "step1" modification. The issue is that in the spreadsheet the error #VALUE! appears and I'm not sure why this is.
Function getECONpgdimscore1(pg As String) As Double
Dim row As Integer
row = 2
Dim c As Integer
c = 1
Dim econ As Double
econ = 0
Dim x As Integer
Dim NumRows As Integer
NumRows = Range("A2", Range("A2").End(xlDown)).rows.count
Cells(row, 1).Select
For x = 1 To NumRows
If Cells(row, 1).Value = pg Then
Do While c < 48
Cells(row, 7 + c).Select
If Cells(1, 7 + c).Value = ("VRP23" Or "VRP24") Then
econ = econ + step1(Cells(1, 7 + c), Cells(row, 7 + c))
End If
c = c + 1
Loop
End If
row = row + 1
Next x
getECONpgdimscore1 = (econ / 100) * 2.5
End Function

Generate one random 5 digit number not present in a column of numbers

I have a list of 5 digit numbers. I need to randomly generate a 5 digit number that is not part of this list. I would like to achieve this on click of a button or simply by using a formula.
For example : If this is my column A in excel with 5 digit numbers,
11111
11113
11115
On click of a button I want to generate a unique 5 digit number which is not in this column.
I am using this function but it is only matching with the first cell. When I click on my button it gives me all numbers from 11110 to 11116 except 11111. I need it to check with full column and return me a unique value.
Private Sub CommandButton1_Click()
Dim rw As Long
For rw = 1 To 1
Cells(rw, 2) = Int((11116 - 11110 + 1) * Rnd + 11110)
Do Until Cells(rw, 2).Value <> Cells(rw, 1).Value
Cells(rw, 2) = Int((11116 - 11110 + 1) * Rnd + 11110)
Loop
Next rw
End Sub
Try this
Dim rw As Long
For rw = 1 To 1
Rnd_Num = Int((11116 - 11110 + 1) * Rnd + 11110)
Do Until Columns(1).Find(Rnd_Num) Is Nothing
Rnd_Num = Int((11116 - 11110 + 1) * Rnd + 11110)
Loop
Cells(rw, 2) = Rnd_Num
Next rw
Dim rw As Long
Dim start_, end_ As Integer
start = 11110
end_ = 11116
For rw = 1 To 1
random_ = Int((end_ - start + 1) * Rnd + start)
Do Until Columns(1).Find(random_) Is Nothing
random_ = Int((end_ - start + 1) * Rnd + start)
Loop
Cells(rw, 2) = random_
Next rw

Excel Function to repeat a set of cells for a certain number of times based on cell value

I am looking for a way to repeat a set of cells horizontally a certain number of times before moving on to the next set of cells. For example:
If I have this in 3 columns:
5 4 3
0 1 2
and I have 3 columns which dictate how many times I want the values iterated:
4 2 3
This function should give me this when dragged over a range:
5 5 5 5 4 4 3 3 3
0 0 0 0 1 1 2 2 2
Does anyone know the best manner to do this?
I have been using some convoluted reasoning to get through this with an array formula ( has the "{}" brackets around it and you have to use Shift+Enter). I am using SUMIF and COUNTIF functions to do some things, but it never really works out.
Here's a hacky VBA solution. This assumes the "repeat counts" are on the first row (4, 2, 3) and the "values to repeat" are on the second row (5, 4, 3, 0, 1, 2).
Sub outputRepeatedValues()
Dim xStart As Integer, yStart As Integer
Dim i As Integer, j As Integer
Dim valueToRepeat As Integer, numTimesRepeat As Integer
Dim xOffset As Integer, yOffset As Integer
xStart = ActiveCell.Column
yStart = ActiveCell.Row
i = 1
j = 1
xOffset = 0
yOffset = 0
While Cells(2, j) <> ""
If Cells(1, i) = "" Then
i = 1
yOffset = yOffset + 1
xOffset = 0
End If
numTimesRepeat = Cells(1, i)
valueToRepeat = Cells(2, j)
For k = 1 To numTimesRepeat
Cells(yStart + yOffset, xStart + xOffset) = valueToRepeat
xOffset = xOffset + 1
Next k
j = j + 1
i = i + 1
Wend
End Sub
Stick this in a new module. To use this code, select the cell representing the upper left corner of the output region. Then press Alt+F8 to bring up the Macro box, and then you can run the macro.

Resources