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"
Code 2. Obtained from " www.exceleinfo.com"
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