„Excel VBA“ pavyzdžiai pradedantiesiems
Makrokomandos yra geriausias jūsų draugas, kai norite padidinti savo produktyvumą ar sutaupyti šiek tiek laiko savo darbo vietoje. Nuo mažų iki didelių užduočių galime automatizuoti naudodami VBA kodavimo kalbą. Aš žinau, kad dažnai galvojote apie kai kuriuos „Excel“ apribojimus, tačiau naudodami VBA kodavimą, galite juos pašalinti. Gerai, jei šiame straipsnyje kovojote su VBA ir vis dar esate pradedantysis, pateiksime keletą naudingų „VBA Macro“ kodo „Excel“ pavyzdžių.

19 geriausių pavyzdžių sąrašas
- Spausdinti visus lapų pavadinimus
- Įterpkite skirtingų spalvų indeksą į VBA
- Įterpkite serijos numerį iš viršaus
- Įterpkite serijos numerį iš apačios
- Įveskite serijos numerį nuo 10 iki 1
- Įdėkite darbalapių tiek, kiek norite
- Ištrinkite visas tuščias darbalapius iš darbaknygės
- Įterpkite tuščią eilutę po kiekvienos kitos eilutės
- Pažymėkite rašybos klaidą
- Keisti visus į didžiųjų raidžių simbolius
- Keisti viską į mažąsias raides
- Pažymėkite visas komentuojamas ląsteles
- Pažymėkite visas tuščias ląsteles
- Slėpti visus lakštus, išskyrus vieną
- Nebeslėpti visų lapų
- Ištrinkite visus aplanko failus
- Ištrinti visą aplanką
- Lape raskite paskutinę naudotą eilutę
- Raskite paskutinį naudotą stulpelį lape
Pažiūrėkime kiekvieną iš šių pavyzdžių išsamiai.
# 1 - atsispausdinkite visus lapų pavadinimus
Kodas:
Sub Print_Sheet_Names () Dim i kaip sveikas skaičius i = 1 į Sheets. Skaičiuokite langelius (i, 1). Vertė = Sheets (i). Pavadinimas Next i End Sub
Tai ištrauks visus lapų pavadinimus į aktyvųjį lapą.

# 2 - Įterpkite skirtingą spalvų indeksą į VBA
Kodas:
Sub Insert_Different_Colours () Dim i kaip sveikasis skaičius i = 1-56 ląstelės (i, 1). Vertė = i ląstelės (i, 2). Interjeras.ColorIndex = i Kitas pabaigos antrinis
Kitame stulpelyje bus įterpti skaičiai nuo 1 iki 56 ir jų spalvų indeksas.

# 3 - Įterpkite serijos numerį iš viršaus
Kodas:
Sub Insert_Numbers_From_Top () Dim i kaip sveikasis skaičius i = 1–10 langelių (i, 1). Vertė = i Kitas i End Sub
Iš viršaus įterps serijos numerius nuo 1 iki 10.

# 4 - Įterpkite serijos numerį iš apačios
Kodas:
Sub Insert_Numbers_From_Bottom () Dim i kaip sveikas skaičius i = 20 iki 1 žingsnio -1 langelių (i, 7). Vertė = i Kitas i End Sub
Iš apačios bus įterpti serijos numeriai nuo 1 iki 20.

# 5 - Įrašykite serijos numerį nuo 10 iki 1
Kodas:
Dešimtoji dalis_To_One () Dim i kaip sveikasis skaičius j j sveikasis skaičius = j = 10 i = 1–10 diapazonas („A“ ir i). Vertė = jj = j - 1 Kitas i End Sub
Iš viršaus įterps serijos numerius nuo 10 iki 1.

# 6 - Įdėkite darbalapių tiek, kiek norite
Kodas:
Sub AddSheets () Dim ShtCount As Integer, i As Integer ShtCount = Application.InputBox ("Kiek lapų norėtumėte įterpti?", "Pridėti lapus",,,,,, 1) Jei ShtCount = False, tada išeikite iš dar antro Jei i = 1 į „ShtCount“ darbalapius. Pridėkite kitą „i End“, jei „End Sub“
Tai paprašys įvesti norimų įterpti darbalapių skaičių. Tiesiog įveskite laukelį įvesties laukelyje ir spustelėkite Gerai, jis tuojau pat įterps tuos daug lapų.

# 7 - Ištrinkite visus tuščius darbalapius iš darbaknygės
Kodas:
Sub Delete_Blank_Sheets () Dim ws as Worksheet Application.DisplayAlerts = False Application.ScreenUpdating = False for every ws in ActiveWorkbook.Worksheets If WorksheetFunction.CountA (ws.UsedRange) = 0 Tada ws.Delete End If Next ws Application.DisplayAlerts = True .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 = Cells (Rows.Count, 1). End (xlUp). Eilutė MsgBox LR End Sub
Čia randame paskutinę naudotą eilutę lape

# 19 - Raskite paskutinį naudotą stulpelį lape
Kodas:
Sub Last_Column () Dim LC As Long LC = Cells (1, Columns.Count). End (xlToLeft). Column MsgBox LC End Sub
Čia randame paskutinį naudotą stulpelį lape
