VB Use a file to Open another excel file and change its cells value
So I have a control file that opens a file and want to change the value in the sheets.
VB ScriptVBAMicrosoft ExcelMicrosoft Office
Last Comment
Fabrice Lambert
8/22/2022 - Mon
Fahad Qureshi
ASKER
This is my code
Sub test() Dim wk As String, yr As String Dim fname As String, fpath As String Dim owb As Workbook Dim fdObj As Object Dim fileName As String Dim x As Integer Dim NumRows As Integer Dim sMacroName As String sMacroName = "'!DisableUpdates" With Application .DisplayAlerts = False .ScreenUpdating = False .EnableEvents = False End With ' Set numrows = number of rows of data. NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count ' Select cell a1. 'Range("A2").Select ' Establish "For" loop to loop "numrows" number of times. For x = 2 To NumRows ' Insert your code here. ' Selects cell down 1 row from active cell. fpath = ActiveSheet.Cells(x, 5).Value fname = ActiveSheet.Cells(x, 6).Value Set fdObj = CreateObject("Scripting.FileSystemObject") CreateFolderRecursive (fpath) If fdObj.fileExists(fpath & fname & ".xlsm") = True Then Set owb = Application.Workbooks.Open(fpath & "Project Variance Report.xlsm") With owb .SaveAs fpath & fname & "new" & ".xlsm", 52 .Close End With Else Set owb = Application.Workbooks.Open(fpath & "Project Variance Report.xlsm") 'owb.Application.Run ("DisableUpdates") Application.Run "'" & owb.Name & "'!DisableUpdates" Application.Wait (Now + TimeValue("0:00:05")) With owb .SaveAs fpath & fname & ".xlsm", 52 .Close End With End If ActiveCell.Offset(1, 0).Select NextApplication.DisplayAlerts = True 'Turns back on alertsApplication.AlertBeforeOverwriting = True 'Turns on Overwrite alertsApplication.ScreenUpdating = True 'Turns on screen updatingEnd Sub
Open in new window