vba useform to patch formula in excel files

ezonemy

New Member
Joined
Dec 7, 2014
Messages
19
hi, im having multiple excel files in a folder which is password protected (same password for all files). I need a userform with 4 text box (textbox 1 = to input forlder name to be opened, textbox 2 = cell to find, e.g a1... textbox 3= to select sheet e.g Sheet 1 , textbox 4= to input formula e.g "=a1+a2+a3")

basically this userform will open all the excel files in the folder which was selected based on textbox 1, unprotect the sheet based on the input in text box 3 and find the cell based on textbox 2 and insert the formula based on textbox 4 then later protect the sheet back with same password and save the files. the mcaro need to loop to all excel files in the folder which selected based on textbox 1.

Hope some one can help on the vba code for this process.

Thank you.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi
Please test this:

Code:
Private Sub cbutGo_Click()
Dim wb As Workbook, pth$, ext$, f$


pth = Me.TextBox1.Value         ' example: "c:\pub\source\"
ChDir pth
ext = Dir("*.xls?")
Do While ext <> ""
    Set wb = Workbooks.Open(pth & ext, , , , "password")
    wb.Worksheets(Me.TextBox3.Value).Range(Me.TextBox2).Formula = Me.TextBox4.Value
    wb.Protect "password", True, True
    wb.Save
'   wb.Close
    ext = Dir
Loop
End Sub
 
Upvote 0
hi its working, just that the workbook is not protected, its the worksheet which is protected with same password for all sheets. Also noted the vba code above makes the workbook to hang and unable to close.
 
Upvote 0
New version:

Code:
' at a standard module
Sub showuf2()


UserForm2.Show vbModeless


End Sub

Code:
' UserForm module
Private Sub cbutGo_Click()
Dim wb As Workbook, pth$, ext$, f$, ws As Worksheet
pth = Me.TextBox1.Value         ' example: "c:\pub\source\"
ChDir pth
ext = Dir("*.xls?")
Do While ext <> ""
    Set wb = Workbooks.Open(pth & ext)
    Set ws = wb.Worksheets(Me.TextBox3.Value)
    If ws.ProtectContents Then ws.Unprotect "password"
    ws.Range(Me.TextBox2).Formula = Me.TextBox4.Value
    ws.Protect "password"
    wb.Save
Rem wb.Close        ' to close the workbook, delete the Rem keyword
    ext = Dir
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

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