Consulta de código en visual basic para abrir libros de excel
Hola
Te doy una idea
Abre un nuevo libro
En la hoja1 celda A1 escribe cualquier texto para ensayar.
Crea en esta hoja un commandbutton y un textbox
Ahora abre VBA
Inserta un modulo y en pon el siguiente código:
Option Explicit
Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Global iCounter As Integer
Public Red As Integer, Green As Integer, Blue As Integer
Sub TimerProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal idEvent As Long, _
ByVal dwTime As Long)
iCounter = iCounter + 1
Worksheets(1).TextBox1.Text = CStr(iCounter)
If iCounter / 2 = iCounter \ 2 Then
Worksheets(1).Cells(1, 1).Font.Color = vbWhite
Else
Red = 256 * Rnd()
Green = 256 * Rnd()
Blue = 256 * Rnd()
Worksheets(1).Cells(1, 1).Font.Color = RGB(Red, Green, Blue)
Red = 256 * Rnd()
Green = 256 * Rnd()
Blue = 256 * Rnd()
Worksheets(1).Cells(1, 1).Interior.Color = RGB(Red, Green, Blue)
End If
End Sub
Ahora en el código de la hola 1 pon lo siguiente:
Option Explicit
Dim lngTimerID As Long
Dim BlnTimer As Boolean
Private Sub commandbutton1_Click()
'Inicia y detiene el Timer.
If BlnTimer = False Then
lngTimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)
If lngTimerID = 0 Then
MsgBox "El Timer no se ha podido crear. Finalizando..."
Exit Sub
End If
BlnTimer = True
CommandButton1.Caption = "Parar TIMER"
Else
lngTimerID = KillTimer(0, lngTimerID)
If lngTimerID = 0 Then
MsgBox "El Timer no se ha podido destruir."
End If
BlnTimer = False
CommandButton1.Caption = "Iniciar TIMER"
End If
End Sub
Private Sub Worksheet_Activate()
BlnTimer = False
CommandButton1.Caption = "Iniciar TIMER"
End Sub
Listo
Lo ensaye y funcionó
Saludos
Te doy una idea
Abre un nuevo libro
En la hoja1 celda A1 escribe cualquier texto para ensayar.
Crea en esta hoja un commandbutton y un textbox
Ahora abre VBA
Inserta un modulo y en pon el siguiente código:
Option Explicit
Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Global iCounter As Integer
Public Red As Integer, Green As Integer, Blue As Integer
Sub TimerProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal idEvent As Long, _
ByVal dwTime As Long)
iCounter = iCounter + 1
Worksheets(1).TextBox1.Text = CStr(iCounter)
If iCounter / 2 = iCounter \ 2 Then
Worksheets(1).Cells(1, 1).Font.Color = vbWhite
Else
Red = 256 * Rnd()
Green = 256 * Rnd()
Blue = 256 * Rnd()
Worksheets(1).Cells(1, 1).Font.Color = RGB(Red, Green, Blue)
Red = 256 * Rnd()
Green = 256 * Rnd()
Blue = 256 * Rnd()
Worksheets(1).Cells(1, 1).Interior.Color = RGB(Red, Green, Blue)
End If
End Sub
Ahora en el código de la hola 1 pon lo siguiente:
Option Explicit
Dim lngTimerID As Long
Dim BlnTimer As Boolean
Private Sub commandbutton1_Click()
'Inicia y detiene el Timer.
If BlnTimer = False Then
lngTimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)
If lngTimerID = 0 Then
MsgBox "El Timer no se ha podido crear. Finalizando..."
Exit Sub
End If
BlnTimer = True
CommandButton1.Caption = "Parar TIMER"
Else
lngTimerID = KillTimer(0, lngTimerID)
If lngTimerID = 0 Then
MsgBox "El Timer no se ha podido destruir."
End If
BlnTimer = False
CommandButton1.Caption = "Iniciar TIMER"
End If
End Sub
Private Sub Worksheet_Activate()
BlnTimer = False
CommandButton1.Caption = "Iniciar TIMER"
End Sub
Listo
Lo ensaye y funcionó
Saludos
1 Respuesta
Respuesta de jgirj
1