Тёмный

VBA - Looping through a Table (ListObject) and deleting a row 

EverydayVBA
Подписаться 11 тыс.
Просмотров 25 тыс.
50% 1

**Get the Excel file here
chrisjterrell....
**Grab the Free VBA Quick Reference Guide
www.chrisjterr...
Looping through a Table (ListObject) in Excel is slightly different than a sheet using "Cells" and arrays using the index in the parenthesis array(x,y). If you are using Tables in Excel, it is a brilliant move. Linking pivot tables to the table is fantastic because you don't have to resize your Pivot if your data grows or shrinks.
However, if you have coded Pivot you may have found them cumbersome. A great resource for coding ListObjects at this link here. In this link, "TheSpreadsheetGuru" gives you more than enough info to learn how to code ListObject
The Key to looping through an Object is to know its name or index. Which is similar to how you would code a Sheet and Cell. The second thing you need to know is the HeaderRowRange and the DatabodyRange because that is how you will loop through the ListObject
CODE
'A great blog post on www.thespreads...
Sub listobjLoop()
Application.ScreenUpdating = False
Dim lo As ListObject
Set lo = Sheet1.ListObjects("Table1")
For cl = 1 To lo.HeaderRowRange.Count
If lo.HeaderRowRange(cl) = "weight" Then Exit For
Next
num = 2500
For rw = lo.DataBodyRange.Rows.Count To 1 Step -1
If lo.DataBodyRange(rw, cl) LT num Then
lo.ListRows(rw).Delete
End If
Next
Application.ScreenUpdating = True
End Sub

Опубликовано:

 

23 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 33   
@querrythis
@querrythis Год назад
Beginner learner here. My code was crude. This is so efficient and logical. Thank you so much for this valuable help.
@joaogrilo4896
@joaogrilo4896 2 года назад
Thanks for your well explained and relevant video. At first, I couldn't understand why you decided to loop through the bottom to the top of the Table, so I decided to do the opposite, which I immediately regret of... When you delete a row, the table rows move one row to the top, so going from the bottom to the top ensures that every row that meets the criteria will be deleted. Thanks a lot!
@theworddoner
@theworddoner 2 года назад
Thank you so much for making this tutorial. I was banging my head over listobjects until I saw this.
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 2 года назад
Great to hear. Glad it helped
@m-squaredcontractors9720
@m-squaredcontractors9720 3 года назад
Great tutorial, thank you.Short and straight to the point. Well done!
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 3 года назад
Thanks for the Comment
@jacobcrelia1778
@jacobcrelia1778 2 года назад
Bruh! I about lost it when you showed you could turn off screen updates...thanks a billion!!!
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 2 года назад
I think I lost it too when I learned that!
@simplejuan
@simplejuan 3 года назад
Most of my data in Excel are in tables or listobjects. This tutorial helps a lot. Many thanks. Subscribed!!!
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 3 года назад
Glad it helped!
@SRous-y7d
@SRous-y7d Год назад
Excellent, I've learned a lot here Thanks.
@YT2007XYZ
@YT2007XYZ 2 года назад
I really liked your video, but you really need slow down a little to give the viewer time to follow where you are clicking and getting information (time 4:23 to 4:35). This may be very apparent to power users, but fortunately I could pause and replay. Also where did you DIM the "rw" variable in your code? In some other sub or is that not required to for this single loop?
@alexdre8888
@alexdre8888 3 года назад
Thank you, very inspiring video
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 3 года назад
Glad you liked it
@ElectromecanicaIndustrial
@ElectromecanicaIndustrial Год назад
very helpful, was struggling with the lookat:=xlWhole for looping through the table but databodyrange solved my issue
@johnabram4159
@johnabram4159 3 года назад
What is the datatype of variable "rw" here? Is it a Range or Long? or ListRow?
@MultiBodmin
@MultiBodmin Год назад
I would imagine it to be an integer and it represents the current row controlled by the "For rw = lo.DataBodyRange.Rows.Count To 1 Step -1" for loop
@redhaakhund1271
@redhaakhund1271 3 года назад
Thanks a million. Great video.
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 3 года назад
Thank you!
@dennisdevink5667
@dennisdevink5667 4 года назад
Great tutorial 👍👍
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 4 года назад
Thank you I appreciate it
@tomharrington1453
@tomharrington1453 3 года назад
Excellent video.
@kelitonandre4738
@kelitonandre4738 3 года назад
Hi. How could I loop through the tables in the database and fill in the name of these tables in an excel spreadsheet?
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 3 года назад
Do you know how to make a database connection? If will contact to the database and use SQL to get all the database tables.
@Pankaj-Verma-
@Pankaj-Verma- 2 года назад
That was very help. Thumbs it is.
@anshuljain7925
@anshuljain7925 4 года назад
Hi I need your help I am making excel VBA code in the Developer tab to create Powerpoint Presentation I have successfully generated PowerPoint slides using my VBA code in excel But, I want to set the background as an image. I am unable to do so In my excel VBA code, I have the variable ppSlide, that is for the newly created Slide In my excel VBA code, I am running the code ppSlide.Background.Fill.UserPicture ("Address of the picture that is local to my Laptop hard drive with JPG extension") Entire code is getting executed, but the background image of my Slide is not getting in the Slide I am using MS Office 2007 Note : I have already added Microsoft Powerpoint 12.0 Object Library in my Tools -> References Kindly help me.
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 4 года назад
Could the issue be because of a PowerPoint theme
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 4 года назад
You may have to move the image to the front or make sure nothing is in front of it
@anshuljain7925
@anshuljain7925 4 года назад
@@EverydayVBAExcelTraining I'll try to work on your suggestions and get back to you. Thanks for the reply
@pauljosephson4820
@pauljosephson4820 9 месяцев назад
you didn't define/Dim "rw"
@EverydayVBAExcelTraining
@EverydayVBAExcelTraining 9 месяцев назад
Yeah, I have constantly gone back an forth on that one. To not define variables is NOT best practice but it also takes time to explain on videos and can be more confusing for someone new to vba. I don't know what the right answer is here.
Далее
How to get the Last Row in VBA(The Right Way!)
15:41
Просмотров 159 тыс.
VBA For Loop - A Complete Guide
12:36
Просмотров 81 тыс.
Highlight Active Row & Column in Excel (7 Levels)
22:56
How to get data from Excel table using ListObject?
20:00
Excel Time-Savers - 5 Hidden Features for Busy People
14:24
Which is the Fastest VBA Method For Reading Tables?
8:39
Excel VBA: How to Delete Blank Rows [Two Techniques]
12:13
Loop Through a Named Range in Excel VBA
8:26
Просмотров 36 тыс.
Using VBA to Enter Data into an Excel Table
14:08
Просмотров 62 тыс.
Excel VBA - How to use Excel Table in VBA
15:28
Просмотров 8 тыс.