Calling a Sub & Number [duplicate] - vba

This question already has an answer here:
VBA - Calling a subroutine using a concatenation of strings
(1 answer)
Closed 1 year ago.
Afternoon All,
I have a list of subs for example (I will actually have 5 in total),
Sub Office1()
Does a thing
End Sub
Sub Office2()
Does a slightly different thing
End Sub
I also have a calculation that leaves lvl = a number between 1 and 5
lvl = 1 '(or 2,3,4,5)
So when I want to call Office1-5 it will depend on a lvl, so what I am looking to do is:
Call "Office" & lvl
I struggled searching this one out as I wasn't sure how to phrase the question.
Hope you can help,
Cheers,
Bill

Consider:
Sub office1()
MsgBox 1
End Sub
Sub office2()
MsgBox 2
End Sub
Sub office3()
MsgBox 3
End Sub
Sub main()
For lvl = 1 To 3
Application.Run "office" & lvl
Next lvl
End Sub

Related

How to delete rows based on empty cell value in VBA [duplicate]

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

Hide columns of a year

I want to hide all columns of dates which years aren't the last 2 years. For that (I don't know whether it is the correct approach or not) I've been doing some research given that I know nothing about Excel programming, and I've created this VBA script but it does not seem to work:
Function OcultarFechas(Range)
Rg1 = Range ' B2:AA2
Flag = "ok"
For Each c1 In Range(Rg1).Cells
If Year(c1) <> Year(Date) Then
Columns(c1.Column).EntireColumn.Hidden = True
Else
Flag = "notok"
End If
If Flag = "notok" Then Exit For
Next c1
End Function
It would be preferable that the scripts executes when I open the spreadsheet but right now with this code I think I need to call the function on a cell like: "=OcultarFechas(B2:AA2)".
PS. the dates are ordered that's why I exit the for loop when the current year is found, and from that column I need to keep them unhidden
You cannot hide a column using UDF.
Try something like this...
Sub HideColumns()
Dim c As Long, lc As Long
lc = Cells(2, Columns.Count).End(xlToLeft).Column
Columns.Hidden = False
For c = 2 To lc
If Year(Cells(2, c)) < Year(Date) Then
Columns(c).Hidden = True
End If
Next c
End Sub
The code above will find the last column used in Row2 and loop through column 2 to the last column found and check the year condition and hide the column accordingly.
To call this procedure automatically when you open the workbook, place the following code on ThisWorkbook Module.
Private Sub Workbook_Open()
Call HideColumns
End Sub

VBA How to delete in a specific direcitons?

I want to delete all values starting from A2:O2 and LOWER , i.e. A3:O3, A4:O4, A5:O5
A B C .... O P Q
1 ..............................
2 deletedeletedeletedeletedelete
3 deletedeletedeletedeletedelete
4 deletedeletedeletedeletedelete
5 deletedeletedeletedeletedelete
I am not sure how to do this
Sub DeleteLower()
Range("A2:O1048576").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.ClearContents
end sub
How do I explain that I need the range of "A2:0 infinity " "A2:O1048576" ?
Perhaps something like this would work for you:
Sub RemoveValues()
ActiveSheet.Range(ActiveSheet.Range("A2:O2").Address, ActiveSheet.Cells(Rows.Count, 1).Address).ClearContents
End Sub
This will remove all content from cells A2:O2 and downward.

How to select the last item in a pivot field using Excel VBA?

I have a pivot table having Pivot field "Date/Time" . I need a macro which selects the last item of the field.
I tried the following code but is not working
Sub Test()
Dim i As Long
i = ActiveSheet.PivotTables("PivotTable1").PivotFields("Date/Time").PivotItems.Count
With ActiveSheet
.PivotTables("PivotTable1").PivotFields("Date/Time").PivotItems(i).Visible=True
End With
End Sub
Please help me out. I just can't find my mistake.
This is no an elegant answer. Not something I could say that I am fully convinced. Since you said you just need to filter Date/Time, please try this out and comment.
Sub somePivot()
Dim n As Long
With Worksheets(2)
n = .PivotTables("PivotTable3").PivotFields("Date/Time").PivotItems.Count
MsgBox n
.PivotTables("PivotTable3").PivotFields("Date/Time").AutoSort xlAscending, "Date/Time"
MsgBox .PivotTables("PivotTable3").PivotFields("Date/Time").PivotItems(n).Value
End With
End Sub

Excel Macro module to copy variable columns

I'm trying to create a Macro that copies variable columns from one sheet to another. For example I would like to copy column A-sheet 3 to column A-sheet 6, column B-sheet 3 to column C-sheet 6, etc.
I was hoping to create one procedure that can be called many times.
Sub Copy_Column(a, b)
Sheets(3).Select
Range("a11:a1000").Select
Selection.Copy
Sheets(6).Select
Range("b15").Select
ActiveSheet.Paste
End Sub
Sub Master()
Call Copy_Column(a, a)
End Sub
What happens is it copies Column A in sheet 3 to Column B in sheet 6, not in Column A.
Thanks in advance!
Well you never use the a and b parameters in the routine.
Did you mean them to represent column letters? If so, then
Sub Copy_Column(byval a as string, byval b as string)
Sheets(3).Range(a & "11:" & a & "1000").Copy Sheets(6).Range(b & "15")
End Sub
Sub Master()
Call Copy_Column("a", "a")
End Sub