Combine these two macros. Replace in closed WB

gubertu

Board Regular
Joined
May 24, 2015
Messages
129
Hi all,

I have the following codes and I would like to mix them up so I can do the replacement in closed Workbooks.

Would it be possible?

Thanks in advance.

Code 1. Obtained from "www.thespreadsheetguru.com"

Sub FindReplaceAll()
'PURPOSE: Find & Replace text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant

fnd = "April"
rplc = "May"

For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht

End Sub

Code 2. Obtained from " www.exceleinfo.com"

Sub ModificarXLCerrados()
'Declaramos variables
Dim Archivo As Application
Dim Celda As Object
Dim NombreArchivo As String
'
'Creamos el objecto Excel
Set Archivo = CreateObject("Excel.Application")
'
With Archivo
'
'Recorremos cada celda de la selección para tomar el nombre de cada archivo
For Each Celda In Selection
NombreArchivo = Celda.Value
'
'Validamos si el archivo ya está abierto
If IsFileOpen(NombreArchivo) Then
Else
'
With .Workbooks.Open(NombreArchivo)
'Hacemos las modificaciones en el archivo
.Worksheets("Hoja1").Range("A1").Value = "Total"
.Worksheets("Hoja1").Range("A2").Value = 10
'Cerramos el archivo guardando cambios
.Close SaveChanges:=True
End With
End If
'
Next Celda
'
'Cerramos la aplicación de Excel
.Quit
End With
End Sub


' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
' Código de macro para comprobar si un archivo ya está abierto
' http://support.microsoft.com/kb/291295/es
'
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
'
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,136,969
Messages
5,678,875
Members
419,787
Latest member
juanam

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
Top