change cell value of closed workbooks (VBA)

sabi.kiss

Board Regular
Joined
Nov 7, 2009
Messages
118
Hi there,

I have got a folder with 50 different workbooks, and I need to change cell B7 value in each one of them.
Is it possible to have a command button with a VBA to change the value of B7 in all these files, without having to open them?

Thanks,
Sabi
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The following macro will open each file within the specified folder, change the value in B7, save and close the workbook. Note that this is all done in the background. So you won't see the workbooks being opened, etc. Also, make the necessary changes, where indicated within the code.

Code:
Option Explicit

Sub test()


    Dim MyPath          As String
    Dim MyFile          As String
    Dim Wkb             As Workbook
    Dim Cnt             As Long
    
    Application.ScreenUpdating = False
    
    MyPath = "C:\Users\Domenic\Documents\MyFolder\" 'change the path accordingly
    
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    
    MyFile = Dir(MyPath & "*.xls")
    
    Cnt = 0
    Do While Len(MyFile) > 0
        Cnt = Cnt + 1
        Set Wkb = Workbooks.Open(MyPath & MyFile)
        Wkb.Worksheets("Sheet1").Range("B7").Value = "MyNewValue" 'change the new value accordingly
        Wkb.Close savechanges:=True
        MyFile = Dir
    Loop
    
    If Cnt > 0 Then
        MsgBox "Completed...", vbExclamation
    Else
        MsgBox "No files were found!", vbExclamation
    End If
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Just dropping by to say thanks for the reply provided by Domenic. This small piece of code has saved me hours and hours of work. Just wanted to record my appreciation and to let you know that years after the original question your response is still helping people such as me.
 
Upvote 0
Just dropping by to say thanks for the reply provided by Domenic. This small piece of code has saved me hours and hours of work. Just wanted to record my appreciation and to let you know that years after the original question your response is still helping people such as me.

That's great to hear!

Cheers!
 
Upvote 0
How would I adjust this code to replace a variable cell (based on another open workbook cell results)
 
Upvote 0
@Domenic How would this code need to be adapted to make it work not only for one specific folder but for all files in all subfolders?
 
Upvote 0

Forum statistics

Threads
1,214,568
Messages
6,120,278
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top