How do I insert columns dynamically in Excel? - excel

I would like to insert separating columns into an Excel report to make the existing columns easier to view.
The report is created dynamically and I never know how many columns there will be; there could be 5, 10, 17, etc.
The section starts at F and goes to ival=Application.WorksheetFunction.CountIf(range("D2:D" & LastRow), "Other")
So if ival=10 then the columns are F G H I J K L M N O, and I need to insert columns between F&G, G&H, H&I, I&J, ... and N&O.
This may be a possibility for inserting columns: Workbooks("yourworkbook").Worksheets("theworksheet").Columns(i).Insert
But I'm not sure how to loop through ival.
Sub InsertColumns()
Dim iVal As Integer
Dim Rng As range
Dim LastRow As Long
Dim i As Integer
With Sheets("sheet1")
LastRow = .range("D" & .Rows.Count).End(xlUp).Row
End With
iVal = Application.WorksheetFunction.CountIf(range("D2:D" & LastRow), "Other")
For i = 7 To iVal - 1
Workbooks("yourworkbook").Worksheets("theworksheet").Columns(i+1).Insert
Next i
End Sub

The below code should work without needing to worry about ival:
Sub InsertSeparatorColumns()
Dim lastCol As Long
With Sheets("sheet1")
lastCol = Cells(2, .Columns.Count).End(xlToLeft).Column
For i = lastCol To 7 Step -1
.Columns(i).Insert
.Columns(i).ColumnWidth = 0.5
Next
End With
End Sub

Try this:
Sub InsertSeparatorColumns()
Dim ws as Worksheet
Dim firstCol As String
Dim lastRow As Long
Dim i As Long
Dim howManySeparators As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
firstCol = "F"
lastRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
howManySeparators = Application.WorksheetFunction.CountIf _
(ws.range("D2:D" & LastRow), "Other")
For i = 1 To howManySeparators * 2 Step 2
ws.Range(firstCol & 1).Offset(, i).EntireColumn.Insert
Next i
End Sub

Related

How to auto number rows if adjacent cell is not blank using VBA Excel?

I need to auto number rows if adjacent cell is not blank using VBA.
any one from below codes works perfectly , except if it counter blank cells.
as always, your support is much appreciated.
this the expected output
Sub Fill_Serial_Numbers_Option1()
Dim LastRow As Long
LastRow = Cells(Rows.count, "B").End(xlUp).Row
If LastRow > 2 Then
Range("A3:A" & Application.Max(2, LastRow)) = Evaluate("ROW(A1:A" & LastRow & ")")
End If
End Sub
Sub Fill_Serial_Numbers_Option2()
Dim LastRow As Long
LastRow = Cells(Rows.count, "B").End(xlUp).Row
If LastRow > 2 Then
With Range("A3:A" & LastRow)
.Cells(1, 1).value = 1
.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1, Trend:=False
End With
End If
End Sub
Please, test the next code:
Sub testCountNonBlanks()
Dim sh As Worksheet, lastR As Long, arr, arrA, count As Long, i As Long
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.rows.count).End(xlUp).row: count = 1
If lastR <= 2 Then Exit Sub
arr = sh.Range("B2:B" & lastR).value 'place the range in an array for faster iteration
arrA = sh.Range("A2:A" & lastR).value
For i = 1 To UBound(arr)
If arr(i, 1) <> "" Then arrA(i, 1) = count: count = count + 1
Next i
sh.Range("A2").Resize(UBound(arrA), 1).value = arrA
End Sub
If a formula (written in VBA) is allowed, you can use the next variant:
Sub testCountByFormula()
Dim sh As Worksheet, lastR As Long, rngB As Range
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.rows.count).End(xlUp).row
Set rngB = sh.Range("B2:B" & lastR)
sh.Range("A2:A10").Formula = "=IF(B2<>"""",COUNTA(" & rngB.Address & ")-COUNTA(" & rngB.Address(0, 1) & ")+1,"""")"
End Sub
You don't need a macro to accomplish this. Assuming all you care about is blank or not, then you can use a formula like this in cell A9. =Counta($B$1:$B9) If you have formulas you can try to leverage something with COuntif.
You can use a loop from the first row to the last one, something like this:
Sub Fill()
Dim LastRow As Long
Dim Count As Integer
Dim Row As Integer
Count = 0
Row = 1
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Do While Row <= LastRow
If Not (Cells(Row, 2) = "") Then
Count = Count + 1
Cells(Row, 1) = Count
End If
Row = Row + 1
Loop
End Sub

How to write Pythagoras formula in excel VBA, like I need to select all the values of column A and column B

Sub MS()
Data = Sheets("Tabelle1").Select
Rows("1:1").Select
Rows("11409:11409").Select
Dim bilder As Long
Dim n As Long
Dim d As Long
Dim t As Long
bilder = 64
n = 1
d = 0
t = 0
'Dim i As Long
'For i = 1 To lastrow
Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Range("b1:b" & Cells(Rows.Count, 1).End(xlUp).Row).Select
'Range("a1").Select
'Range("b1").Select
Range("a1,b1").Select
Do While ActiveCell.Value <> ""
Radius = Sqr(Range("A1").Value * Range("A1").Value + Range("B1").Value * Range("B1").Value)
ActiveCell.Offset(1, 1).Select
Loop
End Sub
I'm not sure why you'd want to do it this way (given that it can be done with a simple formula in-cell), but looking at the remnants of code in your question we can see what you're trying to achieve. Here's how I'd do it:
Sub MS()
Dim sht As Worksheet, StartRow As Long, LastRow As Long, OutputColumn As Long
Dim SideA As Double, SideB As Double, SideC As Double
With Worksheets("Tabelle1")
'Set StartRow to the first row of your data ignoring headers
StartRow = 2
'Locate LastRow as last occupied cell in column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set OutputColumn to 3
OutputColumn = 3
'Start loop
For r = StartRow To LastRow
SideA = .Cells(r, 1).Value
SideB = .Cells(r, 2).Value
SideC = Sqr(SideA * SideA + SideB * SideB)
.Cells(r, OutputColumn).Value = SideC
Next
End With
End Sub
Output:
You do not need to select the range to work with it. You may want to see How to avoid using Select in Excel VBA
In your code you are not writing the output to any cell. Here are two ways that will help you achieve what you want.
NON VBA - WAY 1
Put the formula =SQRT(A1*A1+B1*B1) or =SQRT(A1^2+B1^2) in C1 and drag it down
VBA - WAY 2 (Without Looping)
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = Sheets("Tabelle1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("C1:C" & lRow)
.Formula = "=SQRT(A1*A1+B1*B1)"
.Value = .Value
End With
End With
End Sub
VBA - WAY 3 (Without Looping) Slightly complicated way of doing this. Explanation can be seen HERE
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = Sheets("Tabelle1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("C1:C" & lRow)
.Value = Evaluate("index(SQRT((A1:A" & lRow & _
")^2+(B1:B" & lRow & _
")^2),)")
End With
End With
End Sub

VBA - Want to put the sum result at the bottom automatically

I need to put the result of the sum at the end of the last column on different sheets (Not the same number of columns and number of rows)
I need to fix the last part of the code to let that happen.
this is the code (I marked in the code what does not work):
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
Dim ColumnNumber As Long
Dim ColumnLetter As String
ColumnNumber = Range("S3").End(xlToLeft).Column
ColumnLetter = Split(Cells(1, ColumnNumber).Address, "$")(1)
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range(ColumnLetter & 2).End(xlDown).Offset(1, 0)
**LastCell = WorksheetFunction.Sum(ws.Range(ws.Range(ColumnLetter & 2), ws.Range(ColumnLetter & 2).End(xlDown)))**
Next ws
End Sub
Your code works one with two bugs
You searched for "LastCell" in the first sheet only because it was before the FOR loop. The spreadsheets have different dimensions from what you wrote, so I moved the search to a loop so that there is a re-check for each iteration.
Another (in my opinion) problem was the reboot which added up the previous calculations.
for example.
2 + 2 = 4, 2 + 2 + 4 = 8, 2 + 2 + 4 + 8 = 16 e.t.c.
My code is:
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As String 'I changed from Range to String
Dim ColumnNumber As Long
Dim ColumnLetter As String
For Each ws In ThisWorkbook.Worksheets
LastColumnNumber = ws.Range("s3").End(xlToLeft).Column
LastColumnLetter = Split(Cells(1, LastColumnNumber).Address, "$")(1)
LastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
LastCell = LastColumnLetter & LastRow + 1 ' (+1) - last but not the first empty
ws.Range(LastCell) = WorksheetFunction.Sum(ws.Range(ws.Range(LastColumnLetter & 2), ws.Range(LastColumnLetter & LastRow)))
ws.Range(LastCell).Interior.ColorIndex = 43 ''I added for better visualization
Next ws
End Sub
If you are satisfied with the answer, give me a plus point from the answer and close the topic, unless you have any questions.
:)
IT'S OK I did some changes and now it's working!
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As String 'I changed from Range to String
Dim ColumnNumber As Long
Dim ColumnLetter As String
For Each ws In ThisWorkbook.Worksheets
LastColumnNumber = ws.Range("s3").End(xlToLeft).Column
LastColumnLetter = Split(Cells(1, LastColumnNumber).Address, "$")(1)
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ***''REPLACE TO A FROM G IN THIS LINE***
LastCell = LastColumnLetter & LastRow + 1 ' (+1) - last but not the first empty
ws.Range(LastCell) = WorksheetFunction.Sum(ws.Range(ws.Range(LastColumnLetter & 2), ws.Range(LastColumnLetter & LastRow)))
ws.Range(LastCell).Interior.ColorIndex = 43 ''I added for better visualization
Next ws
End Sub

alternative to Vlookup with VBA. compairing values from a column and copying the corresponding values from a second column to another column

I have a task and I don't want to use vlookup because it makes the process very slow. I'm looking for a purely VBA code solution for the task.
Here I combine the values in column A and D in sheet 2. If the values in column A and B of sheet 1 are the same as that in sheet 2, then I copy the corresponding values in column G in sheet 2 to column D in sheet 1.
Application.ScreenUpdating = False
Sheets("Sending List").Select
Dim Lastrow1, Lastrow2 As Long
Dim ws1, ws2 As Worksheet
Dim tempVal, tempVal2 As String
Set ws1 = Sheets("Sending List")
Set ws2 = Sheets("P13 D-Chain Status")
Lastrow1 = ws1.Range("B" & Rows.Count).End(xlUp).Row
Lastrow2 = ws2.Range("B" & Rows.Count).End(xlUp).Row
With ws2
.Range("Q2:Q" & Lastrow2).Formula = "=A2&D2"
.Range("R2:R" & Lastrow2).Formula = "=G2"
End With
With ws1
.Range("D2:D" & Lastrow1).Formula = "=VLOOKUP(A2&B2,'P13 D-Chain Status'!Q:R,2,0)"
End With
Application.ScreenUpdating = True
End Sub
Try this one:
=INDEX('P13 D-Chain Status'!Q:R,MATCH(A2&B2,'P13 D-Chain Status'!Q:Q,0),2)
But I don´t think it will work much faster.
Hope it helps
Try this. This uses array instead of formulas.
Option Explicit
Sub Sample()
Dim wsI As Worksheet
Dim wsO As Worksheet
Dim wsIAr As Variant
Dim wsOAr As Variant
Dim FinaAr As Variant
Set wsI = Sheet1 'Sheets("P13 D-Chain Status")
Set wsO = Sheet2 'Sheets("Sending List")
Dim lRowI As Long
Dim lRowO As Long
With wsI
lRowI = .Range("B" & .Rows.Count).End(xlUp).Row
wsIAr = .Range("A2:G" & lRowI).Value
End With
With wsO
lRowO = .Range("B" & .Rows.Count).End(xlUp).Row
wsOAr = .Range("A2:B" & lRowO).Value
End With
ReDim FinaAr(lRowO)
Dim searchValue As String
Dim i As Long, j As Long, k As Long
For i = LBound(wsOAr) To UBound(wsOAr)
searchValue = wsOAr(i, 1) & wsOAr(i, 2)
For j = LBound(wsIAr) To UBound(wsIAr)
If searchValue = wsIAr(j, 1) & wsIAr(j, 4) Then
FinaAr(k) = wsIAr(j, 7)
Exit For
End If
Next j
k = k + 1
Next i
wsO.Range("D2").Resize(lRowO, 1).Value = _
Application.WorksheetFunction.Transpose(FinaAr)
End Sub

macro to count and give result

Can anyone help me. I want to count how many of the numbers are > 45 and put the result 3 rows below the last data cell. Lets give it a name - call it result. Then to the left of result I would like to put the words Number > 45. The amount of data rows will change, so when I run the macro on column D it will find the last data point and do the calculation. Some of the rows will be empty. Thanks for the help
Its should like that this
50
20
100
120
45
30
30
Return >45= 4
Sub enter()
Dim result As Integer
Dim firstrow As Integer
Dim lastwow As Integer
Firstrow = d2
Result = ‘ Value of count
Worksheets("sheet1").Range("c?").Value = "Total>45"
Range("d100000").End(xlUp).Select
End Sub
Try this
Sub Sample()
Dim result As Long, firstrow As Long, lastrow As Long
Dim ws As Worksheet
Dim rng As Range
'~~> Set this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Lastrow in Col D
lastrow = .Range("D" & .Rows.Count).End(xlUp).Row
'~~> Set First row
firstrow = 1
'~~> Set your range
Set rng = .Range("D" & firstrow & ":D" & lastrow)
'~~> Put relevant values
.Range("C" & lastrow + 3).Value = "Total>45"
.Range("D" & lastrow + 3).Value = _
Application.WorksheetFunction.CountIf(rng, ">45")
End With
End Sub
Screenshot
Here's one that will let you pass in any number, not just 45
Sub MakeCount(lGreaterThan As Long)
Dim lLast As Long
With Sheet1
lLast = .Cells(.Rows.Count, 4).End(xlUp).Row
.Cells(lLast + 3, 4).FormulaR1C1 = "=COUNTIF(R[-" & lLast + 1 & "]C:R[-3]C,"">""&RC[-1])"
.Cells(lLast + 3, 3).Value = lGreaterThan
.Cells(lLast + 3, 3).NumberFormat = """Number>""#"
End With
End Sub
can't you use a worksheet formula like
=COUNTIF(A2:A7,">45")
alternatively, in VBA as Mr Siddharth Rout suggests in his answer
is vba required?
if not, the function =COUNTIF(C:C,">45") will give you the answer you want.

Resources