Link to home
Start Free TrialLog in
Avatar of Fahad Qureshi
Fahad Qureshi

asked on

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.
Avatar of Fahad Qureshi
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
   Next


Application.DisplayAlerts = True                 'Turns back on alerts
Application.AlertBeforeOverwriting = True        'Turns on Overwrite alerts
Application.ScreenUpdating = True                'Turns on screen updating


End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial