„Excel VBA“ ištrinti eilutę
Paprastai „Excel“ darbalapyje mes turime du skirtingus būdus, kaip ištrinti eilutes, viena iš jų yra spartusis klavišas, o kitas - naudojant dešinįjį pelės klavišą ir įterpimo metodą, tačiau VBA turime naudoti komandą „Delete“ ir darbalapio ištrynimą, kad ištrintume visas eiles, apgauti tai yra tai, kad jei mums reikia ištrinti vieną eilutę, mes pateikiame vienos eilutės nuorodą, bet keliems stulpeliams - kelias eilutes.
Naudodami VBA Delete Row Method, mes galime ištrinti visas tuščias eilutes ir galime ištrinti eilutę pagal langelio vertę. Taip pat galime ištrinti visą eilutę, jei kuri nors iš langelių yra tuščia.
Šiame straipsnyje aptarsime metodą „VBA Delete Row“. Laikykitės užimtas kitas 15–20 minučių, kad sužinotumėte apie koncepciją.

Kaip ištrinti eilutę?
1 pavyzdys
VBA turime paminėti ištrinamą eilutę.
Kodas:
Sub DeleteRow_Example1 () Cells (1, 1) End Sub

Ląstelės (1, 1) reiškia pirmos eilutės pirmąjį stulpelį, ty A1 langelį. Tada mes naudojame metodą „ištrinti“.
Kodas:
Sub DeleteRow_Example1 () ląstelės (1, 1). Ištrinti pabaigos antrinę

Dabar tai ištrins pirmąjį langelį. Visos dešiniojo krašto reikšmės vieną langelį perkels į kairę.

2 pavyzdys
Jei norite ištrinti visą eilutę, turime naudoti ypatybę „EntireRow“, tada turime naudoti metodą „ištrinti“, kad ištrintume visą pasirinkto langelio eilutę.
Kodas:
Sub DeleteRow_Example2 () langeliai (1, 1). EntireRow.Delete End Sub

Pavyzdžiui, į „Excel“ lapą įvedžiau kelis simbolius taip.

Jei paleisiu šį kodą, jis ištrins visą eilutę, o ne vieną langelį.

3 pavyzdys
Eilutę galime ištrinti keliais būdais. Ankstesniame pavyzdyje mes ištrynėme eilutę naudodami nuosavybę CELLS. Dabar pamatysime, kaip ištrinti naudojant nuosavybę ROWS.

Dabar turime paminėti, kurią eilutę turime ištrinti. Tarkime, kad turime ištrinti 5 -ąją eilutę.

Dabar naudokite nuosavybę „EntireRow“.

Pasirinkę nuosavybę, ką turime padaryti, ty metodą. Turime ištrinti eilutę.
Kodas:
Sub DeleteRow_Example3 () eilutės (5) .EntireRow.Delete End Sub

Taigi, šis kodas ištrins 5 -ąją eilutę.
4 pavyzdys
Ištrinkite kelias eilutes naudodami „Range Object“
Kaip ištrinti kelias eilutes?
Norėdami ištrinti daugiau nei vieną eilutę, galime naudoti objektą VBA RANGE. Tarkime, kad turite keletą reikšmių nuo A1 iki A6 langelių.

Dabar noriu ištrinti pirmąsias 5 eilutes, kad galėčiau nurodyti šias eilutes naudodamas objektą „Range“ kaip „Range („ A1: A5 “)“
Kodas:
Sub DeleteRow_Example4 () Range ("A1: A5") Pabaiga Sub

Dabar noriu naudoti žodį „EntireRow“.
Kodas:
Sub DeleteRow_Example4 () diapazonas („A1: A5“). „EntireRow End“ antrinis

Visoje šioje eilutėje turime atlikti ištrynimo metodą, todėl naudokite ištrinimo metodą.
Kodas:
Sub DeleteRow_Example4 () Range ("A1: A5"). EntireRow.Delete End Sub

Dabar tai ištrins pasirinktas eilutes.

5 pavyzdys
Delete Rows Based On Cell Value
We can also use this “EntireRow.Delete” method to delete the row based on cell value in VBA. For example, I have Yes & No values from cell A1 to A10.

Now we need to delete the rows which have the value “No.” To perform this task, we need to use the function “IF” with loops to delete all the rows which have the value of “No.”
The below code will do the job for us.
Code:
Sub DeleteRow_Example5() Dim k As Integer For k = 10 To 1 Step -1 If Cells(k, 1).Value = "No" Then Cells(k, 1).EntireRow.Delete End If Next k End Sub

Example #6
Delete All the Blank Cells Rows
There are situations where we need to delete the entire row if any of the cells in the range are blank. For example, I have below set of data.

All the colored cells are blank, so I need to delete the entire row. We can perform this task with two sets of code. Below is the code.
Code:
Sub DeleteRow_Example6() Range("A1:F10").SpecialCells(xlCellTypeBlanks).EntireRow.Delete End Sub

This will identify the blank cells in the range A1 to F10.IF any blank cells are found, they will delete the entire row.

The problem with this code is it will only delete the blank cell’s row only in the range A1 to F10, but if any cells are blank in any other cells, it will not delete. So keeping this in mind, I have written one more code.
Code:
Sub DeleteRow_Example7 () Dim RangeToDelete As Range Dim DeletionRange As Range Set RangeToDelete = Application.InputBox ("Prašome pasirinkti diapazoną", "Tuščių langelių eilių ištrynimas", tipas: = 8) Nustatykite DeletionRange = RangeToDelete RangeToDelete.SpecialCells (xlCellTyE) .Ištrinti pabaigos pogrupį

Kai paleisite šį kodą, pirmiausia jis paprašys pasirinkti diapazoną su įvesties laukeliu, rodomu priešais jus.

Pasirinkę diapazoną, turite spustelėti Gerai. Tai ištrins visas tuščias langelių eilutes pasirinktame diapazone.