Ejecutar macros-userform en cualquier hoja
Tengo una macros que incluye forms y que guarda datos en hoja3. Por defecto cuando aperturó el archivo se ubica en hoja 1 o 2 y cuando se ejecuta la macros arroja el error 1004... Pero esto sucede cuando no esta ubicada en la predeterminada (en este caso hoja 3) ya que he probado ubicarlo y guardar el archivo con la hoja 3 luego aperturar nuevamente el archivo y ahi si corre.
¿Cómo puedo hacer para que sin necesidad de estar en hoja3 se ejecute el archivo y pueda trabajar mis forms?
1 respuesta
Gracias experto por responde... el detalle esta que los form no apertura si es que la hoja 3 no esta activa.... estas son las rutinas:
Rutina Form2
Private Sub CommandButton1_Click()
Unload Me
UserForm3.Show
End Sub
Private Sub CommandButton2_Click()
Unload Me
UserForm5.Show
End Sub
Rutina Form3
Private Sub TextBox1_Change()
Sheets("Hoja3").Range("C1").Value = TextBox1.Value
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!"
End If
End Sub
Private Sub TextBox2_Change()
TextBox2.Text = UCase(TextBox2.Text)
Sheets("Hoja3").Range("D1").Value = TextBox2.Value
End Sub
Private Sub TextBox3_Change()
TextBox3.Text = UCase(TextBox3.Text)
Sheets("Hoja3").Range("E1").Value = TextBox3.Value
End Sub
Private Sub TextBox4_Change()
TextBox4.Text = LCase(TextBox4.Text)
Sheets("Hoja3").Range("F1").Value = TextBox4.Value
End Sub
Private Sub TextBox5_Change()
TextBox5.Text = UCase(TextBox5.Text)
Sheets("Hoja3").Range("G1").Value = TextBox5.Value
End Sub
Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!"
End If
End Sub
Private Sub TextBox6_Change()
TextBox6.Text = UCase(TextBox6.Text)
Sheets("Hoja3").Range("H1").Value = TextBox6.Value
End Sub
Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!"
End If
End Sub
Private Sub ComboBox1_Change()
ComboBox1.Text = UCase(ComboBox1.Text)
Sheets("Hoja3").Range("I1").Value = ComboBox1.Value
End Sub
Private Sub TextBox7_Change()
TextBox7.Text = UCase(TextBox7.Text)
Sheets("Hoja3").Range("J1").Value = TextBox7.Value
End Sub
Private Sub TextBox8_Change()
TextBox8.Text = UCase(TextBox8.Text)
Sheets("Hoja3").Range("U1").Value = TextBox8.Value
End Sub
Private Sub TextBox9_Change()
TextBox9.Text = UCase(TextBox9.Text)
Sheets("Hoja3").Range("U1").Value = TextBox9.Value
End Sub
Private Sub TextBox10_Change()
TextBox10.Text = UCase(TextBox10.Text)
Sheets("Hoja3").Range("AB1").Value = TextBox10.Value
End Sub
Private Sub ComboBox2_Change()
ComboBox2.Text = UCase(ComboBox2.Text)
Sheets("Hoja3").Range("K1").Value = ComboBox2.Value
End Sub
Private Sub CommandButton3_Click()
Unload Me
UserForm2.Show
End Sub
Private Sub CommandButton4_Click()
Limpiar
End Sub
Private Sub CommandButton1_Click()
Sheets("Hoja3").Range("S1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("S1").Select
Selection.NumberFormat = "hh:mm:ss AM/PM"
Range("S1:S1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
Private Sub CommandButton2_Click()
Sheets("Hoja3").Range("T1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("T1").Select
Selection.NumberFormat = "hh:mm:ss AM/PM"
Range("T1:T1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
Private Sub ComboBox3_Change()
ComboBox3.Text = UCase(ComboBox3.Text)
Sheets("Hoja3").Range("L1").Value = ComboBox3.Value
End Sub
Private Sub ComboBox4_Change()
ModoContacto
ComboBox4.Text = UCase(ComboBox4.Text)
Sheets("Hoja3").Range("R1").Value = ComboBox4.Value
End Sub
Private Sub CommandButton5_Click()
Unload Me
UserForm4.Show
End Sub
Private Sub UserForm_Initialize()
RegistroDia
RegistroHora
ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear
ComboBox1 = "SELECCIONAR"
ComboBox1.AddItem "AREQUIPA"
ComboBox1.AddItem "CUSCO"
ComboBox1.AddItem "TACNA"
ComboBox1.AddItem "PUNO"
ComboBox1.AddItem "JULIACA"
ComboBox1.AddItem "PUERTO MALDONADO"
ComboBox1.AddItem "ILO"
ComboBox1.AddItem "MOQUEGUA"
ComboBox2 = "SELECCIONAR"
ComboBox2.AddItem "ALTO SELVA ALEGRE"
ComboBox2.AddItem "CAYMA"
ComboBox2.AddItem "CERRO COLORADO"
ComboBox2.AddItem "JACOBO HUNTER"
ComboBox2.AddItem "JOSÉ LUIS BUSTAMANTE Y RIVERO"
ComboBox2.AddItem "MARIANO MELGAR"
ComboBox2.AddItem "MIRAFLORES"
ComboBox2.AddItem "PAUCARPATA"
ComboBox2.AddItem "SABANDIA"
ComboBox2.AddItem "SACHACA"
ComboBox2.AddItem "SOCABAYA"
ComboBox2.AddItem "TIABAYA"
ComboBox2.AddItem "YANAHUARA"
ComboBox3 = "SELECCIONAR"
ComboBox3.AddItem "JMEDINA"
ComboBox3.AddItem "AMEZA"
ComboBox3.AddItem "PHERRERA"
ComboBox3.AddItem "MESCOBEDO"
ComboBox3.AddItem "MBANDA"
ComboBox3.AddItem "CSANCHEZ"
ComboBox3.AddItem "GVALDIVIA"
ComboBox4 = "SELECCIONAR"
ComboBox4.AddItem "VISITA"
ComboBox4.AddItem "TELÉFONO"
End Sub
Private Sub UserForm_Activate()
Label15 = Now
Label15 = Format(Now, "[$-80A]dddd, dd"" de ""mmmm"" de ""yyyy hh:mm:ss")
End Sub
Sub Limpiar()
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
TextBox8 = ""
TextBox9 = ""
ComboBox1 = "SELECCIONAR"
ComboBox1 = "SELECCIONAR"
ComboBox3 = "SELECCIONAR"
ComboBox4 = "SELECCIONAR"
Label15 = Now
Label15 = Format(Now, "[$-80A]dddd, dd"" de ""mmmm"" de ""yyyy hh:mm:ss")
End Sub
Sub ModoContacto()
If ComboBox4 = "TELÉFONO" Then
CommandButton1.Visible = True
CommandButton2.Visible = True
Else
CommandButton1.Visible = False
CommandButton2.Visible = False
End If
End Sub
Sub RegistroDia()
Sheets("Hoja3").Range("A1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("A1").Select
Selection.NumberFormat = "mm/dd/yyyy"
Range("A1:A1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
Sub RegistroHora()
Sheets("Hoja3").Range("B1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("B1").Select
Selection.NumberFormat = "hh:mm:ss AM/PM"
Range("B1:B1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
H o l a:
Haber cambia la macro por esto y me comentas
Private Sub CommandButton1_Click() Unload Me UserForm3.Show End Sub ' Private Sub CommandButton2_Click() Unload Me UserForm5.Show End Sub 'Rutina Form3 Private Sub TextBox1_Change() Set h1 = Sheets("Hoja3") h1.Range("C1").Value = TextBox1.Value End Sub ' Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!" End If End Sub ' Private Sub TextBox2_Change() Set h1 = Sheets("Hoja3") TextBox2.Text = UCase(TextBox2.Text) h1.Range("D1").Value = TextBox2.Value End Sub ' Private Sub TextBox3_Change() Set h1 = Sheets("Hoja3") TextBox3.Text = UCase(TextBox3.Text) h1.Range("E1").Value = TextBox3.Value End Sub ' Private Sub TextBox4_Change() Set h1 = Sheets("Hoja3") TextBox4.Text = LCase(TextBox4.Text) h1.Range("F1").Value = TextBox4.Value End Sub ' Private Sub TextBox5_Change() Set h1 = Sheets("Hoja3") TextBox5.Text = UCase(TextBox5.Text) h1.Range("G1").Value = TextBox5.Value End Sub ' Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!" End If End Sub ' Private Sub TextBox6_Change() Set h1 = Sheets("Hoja3") TextBox6.Text = UCase(TextBox6.Text) h1.Range("H1").Value = TextBox6.Value End Sub ' Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 MsgBox "Este campo es unicamente numérico!!!", vbExclamation, "ERROR!!!" End If End Sub ' Private Sub ComboBox1_Change() Set h1 = Sheets("Hoja3") ComboBox1.Text = UCase(ComboBox1.Text) h1.Range("I1").Value = ComboBox1.Value End Sub ' Private Sub TextBox7_Change() Set h1 = Sheets("Hoja3") TextBox7.Text = UCase(TextBox7.Text) h1.Range("J1").Value = TextBox7.Value End Sub ' Private Sub TextBox8_Change() Set h1 = Sheets("Hoja3") TextBox8.Text = UCase(TextBox8.Text) h1.Range("U1").Value = TextBox8.Value End Sub ' Private Sub TextBox9_Change() Set h1 = Sheets("Hoja3") TextBox9.Text = UCase(TextBox9.Text) h1.Range("U1").Value = TextBox9.Value End Sub ' Private Sub TextBox10_Change() Set h1 = Sheets("Hoja3") TextBox10.Text = UCase(TextBox10.Text) h1.Range("AB1").Value = TextBox10.Value End Sub ' Private Sub ComboBox2_Change() Set h1 = Sheets("Hoja3") ComboBox2.Text = UCase(ComboBox2.Text) h1.Range("K1").Value = ComboBox2.Value End Sub ' Private Sub CommandButton3_Click() Unload Me UserForm2.Show End Sub ' Private Sub CommandButton4_Click() Limpiar End Sub ' Private Sub CommandButton1_Click() Set h1 = Sheets("Hoja3") h1.Range("S1").Select ActiveCell.FormulaR1C1 = "=NOW()" Range("S1").Select Selection.NumberFormat = "hh:mm:ss AM/PM" Range("S1:S1").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A1").Select End Sub ' Private Sub CommandButton2_Click() Set h1 = Sheets("Hoja3") h1.Range("T1").Select ActiveCell.FormulaR1C1 = "=NOW()" Range("T1").Select Selection.NumberFormat = "hh:mm:ss AM/PM" Range("T1:T1").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A1").Select End Sub ' Private Sub ComboBox3_Change() Set h1 = Sheets("Hoja3") ComboBox3.Text = UCase(ComboBox3.Text) h1.Range("L1").Value = ComboBox3.Value End Sub ' Private Sub ComboBox4_Change() ModoContacto Set h1 = Sheets("Hoja3") ComboBox4.Text = UCase(ComboBox4.Text) h1.Range("R1").Value = ComboBox4.Value End Sub ' Private Sub CommandButton5_Click() Unload Me UserForm4.Show End Sub ' Private Sub UserForm_Initialize() RegistroDia RegistroHora ComboBox1. Clear ComboBox2. Clear ComboBox3. Clear ComboBox1 = "SELECCIONAR" ComboBox1. AddItem "AREQUIPA" ComboBox1. AddItem "CUSCO" ComboBox1. AddItem "TACNA" ComboBox1. AddItem "PUNO" ComboBox1. AddItem "JULIACA" ComboBox1. AddItem "PUERTO MALDONADO" ComboBox1. AddItem "ILO" ComboBox1. AddItem "MOQUEGUA" ComboBox2 = "SELECCIONAR" ComboBox2. AddItem "ALTO SELVA ALEGRE" ComboBox2. AddItem "CAYMA" ComboBox2. AddItem "CERRO COLORADO" ComboBox2. AddItem "JACOBO HUNTER" ComboBox2. AddItem "JOSÉ LUIS BUSTAMANTE Y RIVERO" ComboBox2. AddItem "MARIANO MELGAR" ComboBox2. AddItem "MIRAFLORES" ComboBox2. AddItem "PAUCARPATA" ComboBox2. AddItem "SABANDIA" ComboBox2. AddItem "SACHACA" ComboBox2. AddItem "SOCABAYA" ComboBox2. AddItem "TIABAYA" ComboBox2. AddItem "YANAHUARA" ComboBox3 = "SELECCIONAR" ComboBox3. AddItem "JMEDINA" ComboBox3. AddItem "AMEZA" ComboBox3. AddItem "PHERRERA" ComboBox3. AddItem "MESCOBEDO" ComboBox3. AddItem "MBANDA" ComboBox3. AddItem "CSANCHEZ" ComboBox3. AddItem "GVALDIVIA" ComboBox4 = "SELECCIONAR" ComboBox4. AddItem "VISITA" ComboBox4. AddItem "TELÉFONO" End Sub Private Sub UserForm_Activate() Label15 = Now Label15 = Format(Now, "[$-80A]dddd, dd"" de ""mmmm"" de ""yyyy hh:mm:ss") End Sub ' Sub Limpiar() TextBox1 = "" TextBox2 = "" TextBox3 = "" TextBox4 = "" TextBox5 = "" TextBox6 = "" TextBox7 = "" TextBox8 = "" TextBox9 = "" ComboBox1 = "SELECCIONAR" ComboBox1 = "SELECCIONAR" ComboBox3 = "SELECCIONAR" ComboBox4 = "SELECCIONAR" Label15 = Now Label15 = Format(Now, "[$-80A]dddd, dd"" de ""mmmm"" de ""yyyy hh:mm:ss") End Sub ' Sub ModoContacto() If ComboBox4 = "TELÉFONO" Then CommandButton1.Visible = True CommandButton2.Visible = True Else CommandButton1.Visible = False CommandButton2.Visible = False End If End Sub ' Sub RegistroDia() Set h1 = Sheets("Hoja3") h1.Range("A1").Select ActiveCell.FormulaR1C1 = "=NOW()" Range("A1").Select Selection.NumberFormat = "mm/dd/yyyy" Range("A1:A1").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A1").Select End Sub ' Sub RegistroHora() Set h1 = Sheets("Hoja3") h1.Range("B1").Select ActiveCell.FormulaR1C1 = "=NOW()" Range("B1").Select Selection.NumberFormat = "hh:mm:ss AM/PM" Range("B1:B1").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A1").Select End Sub
- Compartir respuesta