Excel VBA-eksempler for begyndere
Makroer er din bedste ven, når det kommer til at øge din produktivitet eller spare tid på din arbejdsplads. Lige fra små opgaver til store opgaver kan vi automatisere ved hjælp af VBA-kodningssproget. Jeg ved ofte, at du måske har tænkt på nogle af de begrænsninger, excel har, men med VBA-kodning kan du fjerne alle disse. Ok, hvis du kæmpede med VBA og stadig er en nybegynder i denne artikel, vil vi give nogle af de nyttige eksempler på VBA Makro-kode i Excel.

Liste over top 19 eksempler
- Udskriv alle arknavne
- Indsæt et andet farveindeks i VBA
- Indsæt serienummer ovenfra
- Indsæt serienummer fra bunden
- Indsæt serienummer fra 10 til 1
- Indsæt regneark så meget som du vil
- Slet alle tomme regneark fra projektmappen
- Indsæt tom række efter hver anden række
- Fremhæv stavefejl
- Skift alt til store bogstaver
- Skift alt til små bogstaver
- Fremhæv alle de kommenterede celler
- Fremhæv alle de tomme celler
- Skjul alle ark undtagen et ark
- Vis alle ark
- Slet alle filer i mappen
- Slet hele mappen
- Find den sidst anvendte række i arket
- Find den sidst anvendte kolonne i arket
Lad os se hvert af disse eksempler i detaljer.
# 1 - Udskriv alle arknavne
Kode:
Underudskriv_ark_navne () Dim i som heltal for i = 1 til ark. Tælle celler (i, 1). Værdi = ark (i). Navn næste i afslut sub
Dette ekstraherer alle arknavne til det aktive ark.

# 2 - Indsæt forskellige farveindekser i VBA
Kode:
Sub Insert_Different_Colours () Dim i som heltal for i = 1 til 56 celler (i, 1). Værdi = i celler (i, 2). Interior.ColorIndex = i Next End Sub
Dette indsætter tal fra 1 til 56 og deres farveindeks i den næste kolonne.

# 3 - Indsæt serienummer fra toppen
Kode:
Sub Insert_Numbers_From_Top () Dim i som heltal for i = 1 til 10 celler (i, 1). Værdi = i næste i afslut sub
Dette indsætter serienumre fra 1 til 10 fra toppen.

# 4 - Indsæt serienummer fra bunden
Kode:
Sub Insert_Numbers_From_Bottom () Dim i som heltal for i = 20 til 1 trin -1 celler (i, 7). Værdi = i næste i afslut sub
Dette indsætter serienumre fra 1 til 20 fra bunden.

# 5 - Indsæt serienummer fra 10 til 1
Kode:
Under Ten_To_One () Dim i som heltal Dim j Som heltal j = 10 For i = 1 til 10 område ("A" & i). Værdi = jj = j - 1 Næste i Afslut sub
Dette indsætter serienumre fra 10 til 1 fra toppen.

# 6 - Indsæt regneark så meget som du vil
Kode:
Sub AddSheets () Dim ShtCount Som Integer, i As Integer ShtCount = Application.InputBox ("Hvor mange ark vil du indsætte?", "Add Sheets",,,,,,, 1) Hvis ShtCount = Falsk, så afslut under anden side For i = 1 til ShtCount-regneark.Tilføj næste i Afslut, hvis slut sub
Dette vil bede dig om at indtaste antallet af regneark, du vil indsætte. Angiv bare nummeret i indtastningsfeltet, og klik på Ok, det indsætter de mange ark med det samme.

# 7 - Slet alle tomme regneark fra projektmappen
Kode:
Sub Delete_Blank_Sheets () Dim ws As Worksheet Application.DisplayAlerts = False Application.ScreenUpdating = False For hver ws I ActiveWorkbook.Worksheets Hvis WorksheetFunction.CountA (ws.UsedRange) = 0 Så ws.Slet Slet hvis næste med Application.DisplayAlerts = True Application .ScreenUpdating = True End Sub
This will delete all the blank worksheets from the workbook we are working on.

#8 - Insert Blank Row After Every Other Row
Code:
Sub Insert_Row_After_Every_Other_Row() Dim rng As Range Dim CountRow As Integer Dim i As Integer Set rng = Selection CountRow = rng.EntireRow.Count For i = 1 To CountRow ActiveCell.EntireRow.Insert ActiveCell.Offset(2, 0).Select Next i End Sub
For this first, you need to select the range where you would like to insert alternative blank rows.

#9 - Highlight Spelling Mistake
Code:
Sub Chech_Spelling_Mistake() Dim MySelection As Range For Each MySelection In ActiveSheet.UsedRange If Not Application.CheckSpelling(Word:=MySelection.Text) Then MySelection.Interior.Color = vbRed End If Next MySelection End Sub
First, select the data and run the VBA code. It will highlight the cells which have spelling mistakes.

#10 - Change All To Upper Case Characters
Code:
Sub Change_All_To_UPPER_Case() Dim Rng As Range For Each Rng In Selection.Cells If Rng.HasFormula = False Then Rng.Value = UCase(Rng.Value) End If Next Rng End Sub
First, select the data and run the code. It will convert all the text values to upper case characters.

#11 - Change All To Lower Case Characters
Code:
Sub Change_All_To_LOWER_Case() Dim Rng As Range For Each Rng In Selection.Cells If Rng.HasFormula = False Then Rng.Value = LCase(Rng.Value) End If Next Rng End Sub
First, select the data and run the code. It will convert all the text values to lower case characters in excel.

#12 - Highlight All the Commented Cells
Code:
Sub HighlightCellsWithCommentsInActiveWorksheet() ActiveSheet.UsedRange.SpecialCells(xlCellTypeComments).Interior.ColorIndex = 4 End Sub
Result:

#13 - Highlight All the Blank Cells
Code:
Sub Highlight_Blank_Cells() Dim DataSet As Range Set DataSet = Selection DataSet.Cells.SpecialCells(xlCellTypeBlanks).Interior.Color = vbGreen End Sub
First, select the data range and run the code. It will highlight all the blank cells with green color.

#14 - Hide All Sheets Except One Sheet
Code:
Sub Hide_All_Except_One() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets If Ws.Name "Main Sheet" Then Ws.Visible = xlSheetVeryHidden Next Ws End Sub
The above code hides all the sheets except the sheet named “Main Sheet.” You can change the worksheet name as per your wish.

#15 - Unhide All Sheets
Code:
Sub UnHide_All() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Visible = xlSheetVisible Next Ws End Sub
This will unhide all the hidden sheets.

#16 - Delete All Files in the Folder
Code:
Sub Delete_All_Files() 'You can use this to delete all the files in the folder Test '' On Error Resume Next Kill "C:UsersAdmin_2.Dell-PcDesktopDelete Folder*.*" On Error GoTo 0 End Sub
Change the folder path, which is marked in red as per your folder deletion.
#17 - Delete Entire Folder
Code:
Sub Delete_Whole_Folder() 'You can use this to delete entire folder On Error Resume Next Kill "C:UsersAdmin_2.Dell-PcDesktopDelete Folder*.*" 'Firstly it will delete all the files in the folder 'Then below code will delete the entire folder if it is empty RmDir "C:UsersAdmin_2.Dell-PcDesktopDelete Folder " 'Note: RmDir delete only a empty folder On Error GoTo 0 End Sub
Change the folder path, which is marked in red as per your folder deletion.
#18 - Find the Last Used Row in the Sheet
Code:
Sub Last_Row () Dim LR så længe LR = Celler (Rækker.Tælling, 1) .End (xlUp) .Row MsgBox LR End Sub
Her finder vi den sidst anvendte række i arket

# 19 - Find den sidst anvendte kolonne i arket
Kode:
Sub Last_Column () Dim LC As Long LC = Cells (1, Columns.Count) .End (xlToLeft) .Column MsgBox LC End Sub
Her finder vi den sidst anvendte kolonne i arket
