Visual Basic er et værktøj der kan være en stor hjælp ved repetetive opgaver. Under dette tema vil jeg introducere nogle af de intro videoer der er på dette område:



Sub Example()
Dim a As Integer
a = 2
Debug.Print a
Dim b As Long
b = a + 2
Debug.Print b
Dim c As String
c = "Hello, world!"
Debug.Print c
[a3] = "Hello!"


End Sub


Sub hello()


MsgBox "Hello World"
End Sub


Sub this()
ThisWorkbook.Sheets("Sheet1").Range("A1").Offset(1, 1).Select
ThisWorkbook.Sheets("Sheet1").Range("A1").Offset(1, 1).Value = "New Value"
ActiveCell.Offset(-1, -1).Value = ActiveCell.Value
ActiveCell.Value = vbNullString
End Sub
Sub TransposeRangeValues()
Dim TmpArray() As Variant, FromRange As Range, ToRange As Range
Set FromRange = Sheets("Sheet1").Range("a1:a12") 'Worksheets(1).Range("a1:p1")
Set ToRange = ThisWorkbook.Sheets("Sheet1").Range("a1")
TmpArray = Application.Transpose(FromRange.Value)


ToRange.Resize(FromRange.Columns.Count, FromRange.Rows.Count).Value2 = TmpArray
End Sub


Sub Namedrng()


Dim rng As Range
ThisWorkbook.Names.Add Name:="MyRange", _
'Delete defined named range by name
'Access Named Range by name


Set rng = ThisWorkbook.Worksheets("Sheet1").Range("MyRange")
Call MsgBox("Width = " & rng.Value)
' – Excel® VBA Notes for Professionals 26
'Access a Named Range with a Shortcut
'Just like any other range, named ranges can be accessed directly with through a shortcut notation that does not
'require a Range object to be created. The three lines from the code excerpt above can be replaced by a single line:
'Call MsgBox("Width = " & [MyRange


End Sub


'Side 28


Private Sub Max_Min()
Dim wks As Worksheet
Set wks = ThisWorkbook.Worksheets("Sheet1")
Dim units As Range
Set units = ThisWorkbook.Names("units").RefersToRange
Worksheets("Sheet1").Range("Year_Max").Value = WorksheetFunction.Max(units)
Worksheets("Sheet1").Range("Year_Min").Value = WorksheetFunction.Min(units)
End Sub


Private Sub Highlight_1()


'Highlighting Duplicate Values
With Range("b1:b100").FormatConditions.AddUniqueValues
.DupeUnique = xlDuplicate
With .Font
.Bold = True
' – Excel® VBA Notes for Professionals 38
.ColorIndex = 6
End With
End With
End Sub


Private Sub Highlight_2()


'Highlighting Unique Values
With Range("b1:b100").FormatConditions.AddUniqueValues
With .Font
.Bold = True
.ColorIndex = 3
End With
End With
End Sub


Public Sub Highlight()
Call Max_Min
Call Highlight_1
Call Highlight_2
End Sub