Skip to content

Instantly share code, notes, and snippets.

@zbyna
Last active January 4, 2019 00:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zbyna/9bb4e295f9a8025d1ff52d1e4b0d9e84 to your computer and use it in GitHub Desktop.
Basic vba statements - frequent uses
Sub TotoTlacitka_Click()
' callback for button with name TotoTlacitka, _ is part of vba syntax
' early binding
Dim Aktualni_vyber As Range
Dim bunka As Range
Dim cislo As Integer
' object initialisation
Set Aktualni_vyber = Selection
For Each bunka In Aktualni_vyber
Set bunka = Porovnej(bunka)
Next bunka
'free object
Set Aktualni_vyber = Nothing
End Sub
Function Porovnej(ByRef bunka As Range) As Range
' not for direct using from Excel cell b/c returns object and thus create circular reference
If bunka.Value <= 10 Then
bunka.Offset(0, 1).Value = "< = 10 "
ElseIf bunka.Value > 10 And bunka.Value <= 20 Then
bunka.Offset(0, 1).Value = "< = 20"
ElseIf bunka.Value > 20 And bunka.Value <= 30 Then
bunka.Offset(0, 1).Value = "< = 30"
Else
bunka.Offset(0, 1).Value = "> 30"
End If
Set Porovnej = bunka
End Function
Function PorovnejPrimoZBunky(ByRef bunka As Range) As String
' for direct using from Cell in Excel, returns text
If bunka.Value <= 10 Then
PorovnejPrimoZBunky = "< = 10 "
ElseIf bunka.Value > 10 And bunka.Value <= 20 Then
PorovnejPrimoZBunky = "< = 20"
ElseIf bunka.Value > 20 And bunka.Value <= 30 Then
PorovnejPrimoZBunky = "< = 30"
Else
PorovnejPrimoZBunky = "> 30"
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment