Sub Exponent()
Dim pop As Object
'Posledný character vo zvolenej bunke
'sa zmení na exponent
ActiveSheet.Select
n = ActiveCell.Characters.Count
Set pop = ActiveCell
pop.Characters(n, 1).Font.Superscript = True
End Sub
Ďalší snippet je vlastne už malá aplikácia.
Program uvedený nižšie prevedie teplotné stupne:
V Exceli, v editore jazyka Visual Basic vložte kliknutím na menu Insert UserForm. Veľkosť si upravte približne na 334(šírka) x 198(dĺžka) pixelov. Na formulár ktorý obdrží meno UserForm1 si vložte nasledovné ovladače:
V popiske s menom Label1 bude upozornenie, že po vložení teplotných stupňov, treba stlačiť medzerník a potom obdržíme výsledok. Ostatne 3 popisky pomenujte Cels,Fahr,Kelv. CommandButton1 slúži na ukončenie aplikácie. Pretože príkaz na prevod teplotných stupňov bude odoslaný po stlačení medzerníka, bude vhodné na toto upozorniť zmenou farby pozadia a farby fontu popisiek.
Dim ttc As Single, ttf As Single, ttk As Single
Private Sub CommandButton1_Click()
End
End Sub
Private Sub UserForm_Initialize()
Label1.BackColor = RGB(255, 255, 255)' biela
End Sub
Sub TxtB1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Nepoužívať TABULÁTOR, ani ENTER (kód 13)
' funguje vo VB ale vo VBA v povele KeyPress nie
' Prepínaj farbu pozadia medzi modrou a červenou
If Label1.BackColor = RGB(255, 255, 255) Then
Label1.BackColor = RGB(0, 0, 255)
Else
Label1.BackColor = RGB(255, 0, 0)
Label1.ForeColor = RGB(255, 255, 255)
End If
If (KeyAscii) = 32 Then
ttc = Val(TxtB1.Text) 'ttc stupne Celsia
ttf = 9 / 5 * ttc + 32 'ttf stupne Fahrenheit
ttk = ttc + 273.16 ' ttk stupne Kelvina
far = Format(ttf, "0.0")
TxtB2.Text = far
kel = Format(ttk, "0.0")
TxtB3.Text = kel
End If
End Sub
Private Sub TxtB2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Label1.BackColor = RGB(255, 255, 255) Then
Label1.BackColor = RGB(0, 0, 255)
Else
Label1.BackColor = RGB(255, 0, 0)
Label1.ForeColor = RGB(255, 255, 255)
End If
If (KeyAscii) = 32 Then
ttc = (Val(TxtB2.Text) - 32) * 5 / 9
cel = FormatNumber(ttc, 1)
TxtB1.Text = cel
ttk = (ttf - 32) * 5 / 9 + 273.16
kel = FormatNumber(ttk, 1)
TxtB3.Text = ttk
End If
End Sub
Private Sub TxtB3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Label1.BackColor = RGB(255, 255, 255) Then
Label1.BackColor = RGB(0, 0, 255)
Else
Label1.BackColor = RGB(255, 0, 0)
End If
If (KeyAscii = 32) Then
ttc = Val(TxtB3.Text) - 273.16
cel = Format(ttc, "0.0")
TxtB1.Text = cel
ttf = ttc * 9 / 5 + 32
far = Format(ttf, "0.0")
TxtB2 = far
End If
End Sub