¿Cómo abrir un archivo de excel, pero si el nombre no corresponde no copiar los datos?

Hola a todos, espero me puedan ayudar con este problema ya que no soy muy buena programando.

Realice una macro en excel y pues abre una ventana, seleccionas el archivo y después lo pega. Lo que quiero hacer es que cuando seleccione el archivo si este NO tiene en el nombre del documento escrito hacker (no importa que este escrito con mayúsculas o con minúsculas o si tiene mas texto agregado, solo que al principio diga hacker) no lo copie y mande un msgbox "No se pueden copiar ya que no corresponde el nombre.

Este es mi código

Range("M4").Select
Respuesta = Application.Dialogs(xlDialogOpen).Show
If Respuesta = False Then Exit Sub
nombre = ActiveWorkbook.Name
Range("B3:B56").Select
Selection.Copy
ActiveWindow.Close
ActiveSheet.Paste
Selection.NumberFormat = "0.00"
Selection.NumberFormat = "General"
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With

Agradeceré su ayuda.

1 Respuesta

Respuesta
1

Te regreso la macro con el código

Sub carga()
Range("M4").Select
Respuesta = Application.Dialogs(xlDialogOpen).Show
If Respuesta = False Then Exit Sub
nombre = ActiveWorkbook.Name
If Left(nombre, 6) <> "hacker" Then
MsgBox "No se pueden copiar ya que no corresponde el nombre.", vbCritical
Exit Sub
End If
Range("B3:B56").Select
Selection.Copy
ActiveWindow.Close
ActiveSheet.Paste
Selection.NumberFormat = "0.00"
Selection.NumberFormat = "General"
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub

Saludos. Dam
Si es lo que necesitas.

Hola gracias por tu ayuda, me funciona bien para la palabra hacker en minúscula, pero se puede realizar para cualquier forma de escribir hacker ya sea con mayúsculas o minúsculas HACKER, hACKER, hAckEr, etc. no importa como este escrito pero que se lea la palabra hacker.

Te cambio la macro, funciona para cualquier caso, siempre y cuando diga hacker.

Range("M4").Select
Respuesta = Application.Dialogs(xlDialogOpen).Show
If Respuesta = False Then Exit Sub
nombre = ActiveWorkbook.Name
maynombre = UCase(nombre)
If Left(maynombre, 6) <> "HACKER" Then
MsgBox "No se pueden copiar ya que no corresponde el nombre.", vbCritical
Exit Sub
End If
Range("B3:B56").Select
Selection.Copy
ActiveWindow.Close
ActiveSheet.Paste
Selection.NumberFormat = "0.00"
Selection.NumberFormat = "General"
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With

Saludos. Dam

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas