VB Excel, How to make a loop? - excel

here is what i want:
Assume I have 2 ranges of data A and B. B is calculated from a series of calculation included values of range A. I want a loop of 20 times to put B into A and calculate B again. How do I do this?
Thanks in advance

Here is a very simple VBA loop that creates a 20 x 20 matrix. It will help you understand looping with two number ranges.
Sub looptutorial()
Dim a As Integer, b As Integer
ThisWorkbook.ActiveSheet.Select
For b = 1 To 20
For a = 1 To 20
Cells(a, b).Select
Selection.FormulaR1C1 = a * b
Next a
Next b
End Sub

Related

We need program that remove all rows with values in cell 6 like 2,3,4,5 and so on from column F1 and do not remove value 1?

We need program that remove all rows with values in cell 6 like 2,3,4,5 and so on from column F1 and leave only rows with value 1 and heard of table top first row? Also we need leave fist row intact with table heard.why this code work wrong beacause it does not remove 10 100 and only remove 2,3,4,5 and so on.
Sub RemoveRows1()
ThisWorkbook.ActiveSheet.Cells.ClearFormats
Dim m As Long
m = 1
Do While m <= ThisWorkbook.ActiveSheet.Range("F1").CurrentRegion.Rows.Count
If not InStr(1, Thisworkbook.Activesheet.cells(m,6).value = 1) > 0 Then
ThisWorkbook.ActiveSheet.Cells(m, 6).EntireRow.Delete
Else
m = m + 1
End If
Loop
End Sub
Instead of that great big if in the loop, just use
If not InStr(1, ThisWorkbook.ActiveSheet.Cells(m, 6).Text, "1", vbTextCompare) > 0 Then
ThisWorkbook.ActiveSheet.Cells(m, 6).EntireRow.Delete
End If
And that will delete the entire row of anything where column F doesn't contain 1.

How to add multiple columns between each column?

My code adds one column after each existing column.
I need to add 14 columns. I want this to start by adding the columns after column 2 for each column with data. I believe my current code covers that.
Dim z As Integer
Columns(2).Select
For z = 2 To 20
ActiveCell.EntireColumn.Insert
ActiveCell.Offset(0, 2).Select
Next z
Loop backwards, and no need to Select.
This doesn't check if there is any data in the column.
Sub x()
Dim z As Long
For z = 20 To 2 Step -1
Columns(z).Resize(, 14).Insert
Next z
End Sub
Welcome.... Your code inserts 19 columns.. As you have selected second column First column is inserted between A and B. Now if you want to insert first columns between B and C then Select third column first. and then Z from 4 to 17..Oh you mean 14 columns each time after col B? Then...
Dim z As Integer
Columns(3).Select
For z = 4 To 22
Range(ActiveCell, ActiveCell.Offset(0, 13)).EntireColumn.Insert
'ActiveCell.EntireColumn.Insert
ActiveCell.End(xlToRight).Offset(0, 1).Select
'ActiveCell.Offset(0, 2).Select
Next z

Excel: filter/delete certain numbers in one cell

Please help,
I have excel with numbers in 2 columns for example:
10 10
20 2010, 2011
30 30100, 30200,30500
40 40
And the result I want to have is as follows:
1,2,3,4,5,6,7,8,9
10,11
100,200,500
1,2,3,4,5,6,7,8,9
So if the result between column 1 and 2 is 0 to have numbers from 1 to 9.
Is there a way to do this??
If you want to remove the characters in column one from any part of the values in column two, then try:
=IF(A1=B1,"1,2,3,4,5,6,7,8,9",SUBSTITUTE(SUBSTITUTE(B1," ",""),A1,""))
If you only want to remove the column one values from the beginning part of the column two values, then try:
=IF(B1=C1,"1,2,3,4,5,6,7,8,9",MID(SUBSTITUTE(SUBSTITUTE(","&C1," ",""),"," &B1,","),2,99))
Note that, for the final result, I removed the spaces as you showed in your example.
Using Second formula:
Here is a VBA approach:
Function AdHoc(x As Variant, y As Variant) As String
Dim v As Variant
Dim i As Long
Dim s As String
v = Split(y, ",")
If Val(x) = Val(v(0)) Then
s = "1,2,3,4,5,6,7,8,9"
Else
For i = 0 To UBound(v)
v(i) = Mid(Trim(v(i)), 1 + Len(Trim(Str(x))))
Next i
s = Join(v, ",")
End If
AdHoc = s
End Function
If this is entered into a standard code module, then AdHoc can be used directly in the worksheet:
In the above I simply entered =adhoc(A1,B1) in cell C1 and then copied down.

Excel VBA code for categories the data

I have an Excel file that contains some data in column B, now i wish to categories the data in A column like serial number first 1 to 5 again starts from 1 to 5 until the data ends,
for example in below format
1 A
2 B
3 C
4 D
5 E
1 F
2 G
3 H
4 I
5 J
1 K
2 L
3 M
4 N
5 O
I do not have existing code for above task please help me.
you can use the following
put 1 in the Cell A1
put =IF(OFFSET(A2,-1,0)=5,0,OFFSET(A2,-1,0))+1 in cell A2
double click in the bottom corner of cell A2, this will repeat the function for all cells in column A
hope that it will help you
Use some code
Sub DoItGood()
Dim rws As Long, rng As Range, t As Range
Columns(1).ClearContents
rws = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("A1:A" & rws)
x = 1
For Each t In Range("A1:A5")
t = t + x
x = x + 1
Next t
Range("A1:A5").AutoFill Destination:=rng, Type:=xlFillCopy
End Sub
You can get a repeated list of numbers from 1 to n downwards in rows with the following approach:
=MOD((ROW(A1)-1),n)+1
Take the integer remainder of the division row number (starting with 0) and n. You will get 0,1,2,...,n-1,0,1,2,...,n-1,0,1... To this add 1.
In your case n is 5:
=MOD((ROW(A1)-1),5)+1
filled downwards.

Insert row every X rows in excel

I have a long list of codes such as 008.45, etc that will need multiple lines of text to explain them. I have the list of codes and I would like to know how I can automatically insert a row every, say, fifth row. Example Below
1
2
3
4
5
6
7
8
9
10...
100
Every five rows I would like to insert a given number of my choosing of rows. How can I do this? Thanks
Test with a range from row 1 to row 100.
Sub InsertRows()
For i = Sheet1.UsedRange.Rows.Count To 1 Step -5
For j = 0 To 4
Sheet1.Rows(i).Insert
Next
Next
End Sub
You would need to use a loop as below:
for i=1 to 100 step 1
if i mod 5 = 0 then
// Insert the rows
end if
next i
This worked great for me:
Sub add_rows_n()
t = 6
Do Until Cells(t, "A") = ""
Rows(t).Insert
t = t + 6
Loop
End Sub
To insert a row at row myRowNumber, your VBA code would look like this:
Rows(myRowNumber).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
You can incorporate that into Andy's answer.
Or you could use the modulus function like so:
=IF(MOD(ROW()-1,7),"",A1)
in B1, where A1 is the first number of your dataset.
NB: Change 7 to n to get every n'th row.
For example if I want 5 of my records between my rows of data I would use Mod 6, however, you need to allow for these new rows as they will affect the used range count! To do this you will want to add the number of rows that will be inserted to the length of the loop (eg. Absolute value of(numberOfRows/YourModValue)).
Code to do this:
Sub InsertRows()
For i = 1 To Sheet1.UsedRange.Rows.Count + Abs(Sheet1.UsedRange.Rows.Count / 6) Step 1
If i Mod 6 = 0 Then
Sheet1.Rows(i).Insert
Cells(i, 1).Value = "Whatever data you want in your new separator cell"
End If
Next i
End Sub
Here's the code I wound up with. Note that the FOR loop actually runs backwards from the end of UsedRange. The Mod 5 inserts a row every 5 rows.
For i = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
If (i - 1) Mod 5 = 0 Then
ActiveSheet.Rows(i).Insert Shift:=xlDown
End If
Next

Resources