Saltar al contenido
View in the app

A better way to browse. Learn more.

Ayuda Excel

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
publicado

Hola a todos

Como el titulo lo indica, estas dos funciones son muy similares, pero "Replace" tiena ventaja... explico

Ayuda de la funcion Application.WorksheetFunction.Substitute: El método Substitute reemplaza el texto nuevo por el texto original dentro de una cadena de texto. Utilice el método Substitute cuando desee reemplazar texto específico en una cadena de texto; use el método Replace si desea reemplazar cualquier texto que aparezca en una ubicación específica dentro de una cadena de caracteres.

Sintaxis

expresión.Substitute(Arg1, Arg2, Arg3, Arg4)

expresión Variable que representa un objeto WorksheetFunction.

Ayuda de la funcion VBA.Replace: Devuelve una cadena en la que se reemplazó una subcadena especificada con otra subcadena un número especificado de veces.

Sintaxis

Replace(expresión, encontrar, reemplazarCon [, inicio[, Contar[, comparar]]])

Imaginemos una columna con datos, y que a esta en cada celda se debe reemplazar o sustituir algunos caracteres, pues bien he elaborado un archivo en el cual pueden realizar pruebas y notar, que la ventaja de VBA.Replace es que se puede reemplazar los caracteres cuantas ( Contar[ ) veces necesite dentro de la cadena de texto, por el contrario Substitute tiene la limitante de poder hacerlo solo una vez

Sub sustituir()
Dim texto1 As String
Dim texto2 As String
texto1 = Application.InputBox("Sustituir!", "Texto1", , , , Type:=1 + 2)
texto2 = Application.InputBox("Sustituir!", "Texto2", , , , Type:=1 + 2)
With Application
.ScreenUpdating = False
For x = 2 To Range("A" & Rows.Count).End(xlUp).Row
Cells(x, 1) = .Trim(.Substitute(Cells(x, 1), texto1, "", 1))
Cells(x, 1) = .Trim(.Substitute(Cells(x, 1), texto2, "", 1))
Next x
.ScreenUpdating = True
End With
Range("A1").Select
End Sub[/PHP]

[PHP]Sub reemplazar()
Dim texto1 As String
Dim texto2 As String
texto1 = Application.InputBox("Reemplazar!", "Texto1", , , , Type:=1 + 2)
texto2 = Application.InputBox("Reemplazar!", "Texto2", , , , Type:=1 + 2)
With Application
.ScreenUpdating = False
For x = 2 To Range("A" & Rows.Count).End(xlUp).Row
Cells(x, 1) = VBA.Trim(Replace(Cells(x, 1), texto1, "", 1, 1))
Cells(x, 1) = VBA.Trim(Replace(Cells(x, 1), texto2, "", 1, 1))
Next x
.ScreenUpdating = True
End With
Range("A1").Select
End Sub
[/PHP]

NOTA: No confundir ninguna de las dos funciones arriba con Application.WorksheetFunction.Replace si necesitan ver la diferencia busquen en la ayuda de VBA en Excel

Espero sea de mucha utilidad y experimento :)

Saludos desde Honduras

Reemplazar texto en cadena (macro).zip

Featured Replies

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.