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
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 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.
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.
I am struggling to write an effective macro that will find a cell with error in column, then replace that cell with value of the first non-empty cell without error below it (there might be consecutive error cells) then loop for 12 columns.
The code I have below replaces each of the error cells in all 12 columns, but not in a consistent manner: some cells will indeed be filled by the next cell below that contains a number, but some cells end up with value of the second next cell below that contains a number. I can't tell where the problem in my code is.
Option Explicit
Sub ClearError()
ThisWorkbook.Sheets("WorkSheet1").Activate
Dim c, x, z As Integer
Dim y As Long
For z = 3 To 14 Step 1 ' Start with column 'C' and do for total of 12 columns
x = 999
For c = 1 To x Step 1
If IsError(Cells(c, z)) Then
Cells(c, z) = Range(Cells(1, z), Cells(x, z)).Find(y, _
After:=Cells(c, z), LookIn:=xlValues, SearchDirection:=xlNext).Value
End If
Next c
Next z
End Sub
If you could offer any insight or advice, I would greatly appreciate it. Thank you for your time! Ante
Try reversing your loop.
For c = x to 1 Step -1
If IsError(Cells(c, z)) Then
Cells(c, z) = Cells(c + 1, z)
End If
Next c
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
I need some help converting three colums into a matrix using excel macro.
Here is an example:
From this:
A A 0
A B 23
A C 3
B A 7
B B 56
B C 33
C A 31
C B 6
C C 5
to this:
A B C
A 0 23 3
B 7 56 33
C 31 6 5
Hope you can help me.
Thanks
Not quite sure what exactly you are meaning by matrix. For the code below I assumed you were looking for a way to read the data in the first two columns as Row and Column data of the output table. Assume the input data is in the Columns 1 - 3 of "Sheet1"
Sub ConvertTableOfData()
Dim testArray(1 to 3)
Dim chkROW as Integer
Dim chkCOL as Integer
Dim chkVAL as Integer
'// index the Row and Column headers
testArray(1) = "A"
testArray(2) = "B"
testArray(3) = "C"
'// Iterate through every row in the initial dataset
For i = 1 to Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
With Worksheets("Sheet1")
'// Assign the Output Row and Column values
'// based on the array indices
For j = 1 to UBound(testArray, 1)
If .Cells(i, 1) = testArray(j) Then
chkROW = j
End If
If .Cells(i, 2) = testArray(j) Then
chkCOL = j
End If
Next j
'// store the actual value
chkVAL = .Cells(i, 3)
End With
'// output table (in Sheet2)
With Worksheets("Sheet2")
.Cells(chkROW, chkCOL) = chkVAL
End With
Next i
'// Add headers to Output table
For i = 1 to 3
With Worksheets("Sheet2")
.Cells(i + 1, 1) = testArray(i)
.Cells(i, i + 1) = testArray(i)
End With
Next i
End Sub
You can also perform this without VBA.
Assume your table of data is in the range A1:C9.
Assume the first number (0) in the 3 by 3 grid of data is cell F3, with A, B, C in the row above, and A, B, C in the column to the left.
Enter the formula in cell F3 as
=INDEX($C$1:$C$9,SUMPRODUCT(--($A$1:$A$9=$E3),--($B$1:$B$9=F$2),ROW($A$1:$A$9)))
Copy this formula to all 9 cells in the 3 by 3 grid.
This generalized to any size of data.