Hello all,
Im writing a macro that will open every xls files in a folder and add an equation at a specific cell in each of them. An example of the algorithm can be seen below.
My problem is that the range which will need to be autofilled will vary (the number of rows will vary for each workbook). How can I modify this algorithm so that it can determine the range it needs to fill before filling it?
Any help/suggestions will be deeply appreciated
Lovi
Option Explicit
Sub Open_All_Files()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String
Dim ru As Double
sPath = "C:\New folder" 'location of files
ChDir sPath
sFil = Dir("*.xls")
Do While sFil <> ""
Set oWbk = Workbooks.Open(sPath & "\" & sFil)
Range("E1").Select
ActiveCell.FormulaR1C1 = "ffff"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=(RC[-4]+RC[-3])*RC[-1]"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E5")
oWbk.Close True
sFil = Dir
Loop
End Sub
Im writing a macro that will open every xls files in a folder and add an equation at a specific cell in each of them. An example of the algorithm can be seen below.
My problem is that the range which will need to be autofilled will vary (the number of rows will vary for each workbook). How can I modify this algorithm so that it can determine the range it needs to fill before filling it?
Any help/suggestions will be deeply appreciated
Lovi
Option Explicit
Sub Open_All_Files()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String
Dim ru As Double
sPath = "C:\New folder" 'location of files
ChDir sPath
sFil = Dir("*.xls")
Do While sFil <> ""
Set oWbk = Workbooks.Open(sPath & "\" & sFil)
Range("E1").Select
ActiveCell.FormulaR1C1 = "ffff"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=(RC[-4]+RC[-3])*RC[-1]"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E5")
oWbk.Close True
sFil = Dir
Loop
End Sub