VBA piemēri - Top 19 Excel VBA piemēru saraksts iesācējiem

Excel VBA piemēri iesācējiem

Makro ir jūsu labākais draugs, lai palielinātu produktivitāti vai ietaupītu kādu laiku savā darba vietā. Tieši no maziem uzdevumiem līdz lieliem uzdevumiem mēs varam automatizēt, izmantojot VBA kodēšanas valodu. Es zinu, ka jūs bieži domājat par dažiem Excel ierobežojumiem, taču ar VBA kodēšanu jūs varat tos visus novērst. Labi, ja šajā rakstā jūs cīnījāties ar VBA un joprojām esat iesācējs, mēs sniegsim dažus noderīgus VBA makro koda piemērus programmā Excel.

19 populārāko piemēru saraksts

  1. Drukāt visus lapu nosaukumus
  2. Ievietojiet dažādu krāsu indeksu VBA
  3. Ievietojiet sērijas numuru no augšas
  4. Ievietojiet sērijas numuru no apakšas
  5. Ievietojiet sērijas numuru no 10 līdz 1
  6. Ievietojiet darblapas tik daudz, cik vēlaties
  7. Dzēst visas tukšās darblapas no darbgrāmatas
  8. Ievietojiet tukšo rindu aiz katras citas rindas
  9. Iezīmējiet pareizrakstības kļūdu
  10. Mainīt visus uz lielajiem burtiem
  11. Mainīt visu uz mazajiem burtiem
  12. Iezīmējiet visas komentētās šūnas
  13. Iezīmējiet visas tukšās šūnas
  14. Slēpt visas lapas, izņemot vienu lapu
  15. Neslēpt visas lapas
  16. Dzēst visus mapē esošos failus
  17. Dzēst visu mapi
  18. Lapā atrodiet pēdējo izmantoto rindu
  19. Lapā atrodiet pēdējo izmantoto kolonnu

Apskatīsim katru no šiem piemēriem detalizēti.

# 1 - Izdrukājiet visu lapu nosaukumus

Kods:

Sub Print_Sheet_Names () Dim i kā vesels skaitlis i = 1 uz Sheets. Skaita šūnas (i, 1). Vērtība = Sheets (i). Nosaukums Next i End Sub

Tādējādi visi lapu nosaukumi tiks iegūti aktīvajā lapā.

# 2 - Ievietojiet dažādu krāsu indeksu VBA

Kods:

Sub Insert_Different_Colours () Dim i kā veselais skaitlis i = 1 līdz 56 šūnas (i, 1). Vērtība = i Cells (i, 2). Iekšpuse.ColorIndex = i Nākamā beigu apakšdaļa

Tādējādi nākamajā kolonnā tiks ievietoti skaitļi no 1 līdz 56 un to krāsu indekss.

# 3 - Ievietojiet sērijas numuru no augšas

Kods:

Sub Insert_Numbers_From_Top () Dim i kā vesels skaitlis i = 1 līdz 10 šūnām (i, 1). Vērtība = i Nākamais i Beigas Sub

Tādējādi no augšas tiks ievietoti sērijas numuri no 1 līdz 10.

# 4 - Ievietojiet sērijas numuru no apakšas

Kods:

Sub Insert_Numbers_From_Bottom () Dim i kā vesels skaitlis i = 20 līdz 1 solis -1 Šūnas (i, 7). Vērtība = i Nākamais i Beigas Sub

Tādējādi no apakšas tiks ievietoti sērijas numuri no 1 līdz 20.

# 5 - ievietojiet sērijas numuru no 10 līdz 1

Kods:

Desmit apakšdaļa_To_One () Dim i kā vesels skaitlis Dim j kā vesels skaitlis j = 10 i = 1 līdz 10 diapazons ("A" & i). Vērtība = jj = j - 1 Nākamais i Beigas Sub

Tas no augšas ievietos sērijas numurus no 10 līdz 1.

# 6 - Ievietojiet darblapas tik daudz, cik vēlaties

Kods:

Sub AddSheets () Dim ShtCount kā vesels skaitlis, i kā Integer ShtCount = Application.InputBox ("Cik daudz lapu vēlaties ievietot?", "Pievienot lapas",,,,,, 1) Ja ShtCount = False, tad iziet no citas daļas Par i = 1 ShtCount darblapām. Pievienojiet nākamo i End If End Sub

Tas prasīs ievadīt darblapu skaitu, kuru vēlaties ievietot. Vienkārši norādiet numuru ievades lodziņā un noklikšķiniet uz Labi, tas nekavējoties ievietos šīs daudzās lapas.

# 7 - dzēst visas tukšās darblapas no darbgrāmatas

Kods:

Sub Delete_Blank_Sheets () Dim ws kā darblapas Application.DisplayAlerts = False Application.ScreenUpdating = False katram ws programmā ActiveWorkbook.Worksheets If WorksheetFunction.CountA (ws.UsedRange) = 0 Tad ws.Delete Beigt, ja nākamais 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 = šūnas (Rows. Count, 1). End (xlUp). Rinda MsgBox LR End Sub

Šeit mēs atrodam pēdējo izmantoto rindu lapā

# 19 - lapā atrodiet pēdējo izmantoto kolonnu

Kods:

Sub Last_Column () Dim LC As Long LC = Cells (1, Columns.Count). End (xlToLeft). Kolonna MsgBox LC End Sub

Šeit mēs atrodam pēdējo izmantoto sleju lapā

Interesanti raksti...