I'm a beginner at VBA and can't seem to figure out how to call a function in all the Worksheets in a Workbook. Is there a way to call a function like this? I would like it to be in 2 separate Subs if possible. Thanks.
Sub Formuoli2()
Dim iLastRow As Integer
Dim i As Integer
Dim ws As Worksheet
iLastRow = 5
For i = 1 To iLastRow
Range("A" & i) = "a" 'these are formulas
Range("B" & i) = "b" 'these are formulas
Range("C" & i) = "c" 'these are formulas
Range("D" & i) = "d" 'these are formulas
Range("E" & i) = "e" 'these are formulas
Range("F" & i) = "f"
Next i
End Sub
Sub Formuoli3()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
Call Formuoli2
End With
Next
End Sub
Add one line:
.Select
Call Formuoli2
You can pass the worksheet directly as an argument to the second sub:
Sub Formuoli2(ws As Worksheet)
Dim iLastRow As Long
Dim i As Long
iLastRow = 5
For i = 1 To iLastRow
ws.Range("A" & i) = "a" 'these are formulas
ws.Range("B" & i) = "b" 'these are formulas
ws.Range("C" & i) = "c" 'these are formulas
ws.Range("D" & i) = "d" 'these are formulas
ws.Range("E" & i) = "e" 'these are formulas
ws.Range("F" & i) = "f"
Next i
End Sub
Sub Formuoli3()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Call Formuoli2(ws)
Next
End Sub
it's better to pass ws
to Formuoli2()
sub
Option Explicit
Sub Formuoli3()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Formuoli2 ws
Next
End Sub
Sub Formuoli2(ws As Worksheet)
Dim iLastRow As Integer
Dim i As Integer
iLastRow = 5
With ws
For i = 1 To iLastRow
.Range("A" & i).Resize(, 6) = Array("a", "b", "c", "d", "e", "f") 'these are formulas
Next i
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.