Primeri Excel VBA za začetnike
Makri so vaš najboljši prijatelj, ko želite povečati svojo produktivnost ali prihraniti nekaj časa na svojem delovnem mestu. Od majhnih do velikih nalog lahko avtomatiziramo z uporabo kodnega jezika VBA. Pogosto vem, da ste morda pomislili na nekatere omejitve, ki jih ima excel, toda s kodiranjem VBA lahko vse te odpravite. Ok, če ste se v tem članku spopadali z VBA in ste še vedno začetnik, bomo v Excelu podali nekaj koristnih primerov kode VBA Macro.

Seznam najboljših 19 primerov
- Natisni vsa imena listov
- Vstavite drugačen barvni indeks v VBA
- Vstavite serijsko številko od zgoraj
- Vstavite serijsko številko od spodaj
- Vstavite serijsko številko od 10 do 1
- Vstavite delovne liste, kolikor želite
- Iz delovnega zvezka izbrišite vse prazne delovne liste
- Vstavi prazno vrstico za vsako drugo vrstico
- Označite pravopisno napako
- Spremeni vse v velike črke
- Spremeni vse v male črke
- Označite Vse komentirane celice
- Označite Vse prazne celice
- Skrij vse liste razen enega lista
- Razkrij vse liste
- Izbriši vse datoteke v mapi
- Izbriši celotno mapo
- Poiščite zadnjo uporabljeno vrstico v listu
- V listu poiščite zadnji uporabljeni stolpec
Oglejmo si vsakega od teh primerov podrobno.
# 1 - Natisnite vsa imena listov
Koda:
Sub Print_Sheet_Names () Dim i kot celo število za i = 1 na liste. Število celic (i, 1). Vrednost = listi (i). Ime Naprej i Končaj pod
To bo izvleklo vsa imena listov v aktivni list.

# 2 - Vstavite drugačen barvni indeks v VBA
Koda:
Sub Insert_Different_Colours () Dim i Kot celo število za i = 1 do 56 celic (i, 1) .Vrednost = i Celice (i, 2) .Interior.ColorIndex = i Naslednji konec Sub
S tem boste v naslednji stolpec vstavili številke od 1 do 56 in njihov barvni indeks.

# 3 - Vstavite serijsko številko od zgoraj
Koda:
Sub Insert_Numbers_From_Top () Dim i As Integer za i = 1 do 10 celic (i, 1). Vrednost = i Naslednji i End Sub
Tako boste od zgoraj vstavili serijske številke od 1 do 10.

# 4 - Vstavite serijsko številko od spodaj
Koda:
Sub Insert_Numbers_From_Bottom () Dim i kot celo število za i = 20 do 1 korak -1 celic (i, 7) .Vrednost = i Naslednji i End Sub
Tako boste od spodaj vstavili serijske številke od 1 do 20.

# 5 - Vstavite serijsko številko od 10 do 1
Koda:
Sub Ten_To_One () Dim i As Integer Dim j As Integer j = 10 For i = 1 to 10 Range ("A" & i) .Value = jj = j - 1 Naslednji i End Sub
Tako boste od zgoraj vstavili serijske številke od 10 do 1.

6. - Vstavite delovne liste, kolikor želite
Koda:
Sub AddSheets () Dim ShtCount As Integer, i As Integer ShtCount = Application.InputBox ("Koliko listov želite vstaviti?", "Add Sheets",,,,,, 1) Če je ShtCount = False, potem zapustite Sub Else Za i = 1 na delovne liste ShtCount.Naprej dodajte i End If End Sub
Zahtevali boste, da vnesete število delovnih listov, ki jih želite vstaviti. Samo vnesite številko v polje za vnos in kliknite V redu, takoj bo vstavilo toliko listov.

# 7 - Iz delovnega zvezka izbrišite vse prazne delovne liste
Koda:
Sub Delete_Blank_Sheets () Dim ws as Worksheet Application.DisplayAlerts = False Application.ScreenUpdating = False Za vsako ws v ActiveWorkbook.Worksheets If WorksheetFunction.CountA (ws.UsedRange) = 0 Potem ws.Delete End If Next ws Application.DisplayAlerts = .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 As Long LR = Celice (Vrstice. Število, 1). End (xlUp). Vrstica MsgBox LR End Sub
Tu najdemo zadnjo uporabljeno vrstico v listu

# 19 - Poiščite zadnji uporabljeni stolpec v listu
Koda:
Sub Last_Column () Dim LC As Long LC = Celice (1, Columns.Count). End (xlToLeft). Stolpec MsgBox LC End Sub
Tu najdemo zadnji uporabljeni stolpec v listu
