VBA: Delete empty rows based on value of cells
Solved/Closed
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
-
9 Oct 2019 à 06:55
TrowaD Posts 2921 Registration date Sunday 12 September 2010 Status Contributor Last seen 27 December 2022 - 4 Nov 2019 à 11:35
TrowaD Posts 2921 Registration date Sunday 12 September 2010 Status Contributor Last seen 27 December 2022 - 4 Nov 2019 à 11:35
Related:
- Delete rows based on cell value vba
- Vba find - Guide
- How to delete a row in word - Guide
- Vba case like - Guide
- Based on the values in cells b77 b81 what function can automatically return the value in cell c77 ✓ - Excel Forum
- Can't delete snapchat account - Snapchat Forum
7 responses
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
15 Oct 2019 à 12:02
15 Oct 2019 à 12:02
Hi Abdel,
Give the following code lines a try:
Best regards,
Trowa
Give the following code lines a try:
Dim cBlanks as Integer
cBlanks = WorksheetFunction.CountBlank(Range("A19:A32"))
If cBlanks = 14 then Range("A19:E32").ClearContents
Best regards,
Trowa
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
15 Oct 2019 à 13:49
15 Oct 2019 à 13:49
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
17 Oct 2019 à 11:52
17 Oct 2019 à 11:52
Hi Abdel,
Quote
delete rows from a19: e32 if cells a19:a32=" "
Unquote
This is what the provided code lines were for.
Now I understand you want to delete single rows (row 19 to 31) in where there is no value in column A.
For that try the following:
Row 32 is excluded, since that is your total row.
Hopefully this is more to your liking.
Best regards,
Trowa
Quote
delete rows from a19: e32 if cells a19:a32=" "
Unquote
This is what the provided code lines were for.
Now I understand you want to delete single rows (row 19 to 31) in where there is no value in column A.
For that try the following:
Dim x As Integer
For x = 31 To 19 Step -1
If Sheet2.Range("A" & x).Value = vbNullString Then Sheet2.Range("A" & x).EntireRow.Delete
Next x
Row 32 is excluded, since that is your total row.
Hopefully this is more to your liking.
Best regards,
Trowa
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
17 Oct 2019 à 13:36
17 Oct 2019 à 13:36
there is no change the same thing this is what shows in sheet2

and this is my file
https://ufile.io/3jb6t9fe
and this is my file
https://ufile.io/3jb6t9fe
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
21 Oct 2019 à 12:02
21 Oct 2019 à 12:02
Hi Abdel,
I notice in your file that sheet 2 has the range 84:96 and sheet 1 has the mentioned 19:31. In your TransferData code you clean up sheet 1 before copying the data to sheet 2 (at the bottom of your range?).
To show you the provided code lines work as intended, I have added the code RunMe. Before running the code, select the first sheet (the one with a hidden grid). Since I can't reproduce your sheet names (it just ends up showing ????1) in the code, I used ActiveSheet as the sheet reference.
Here is your file:
https://ufile.io/qq1im9c6
Best regards,
Trowa
I notice in your file that sheet 2 has the range 84:96 and sheet 1 has the mentioned 19:31. In your TransferData code you clean up sheet 1 before copying the data to sheet 2 (at the bottom of your range?).
To show you the provided code lines work as intended, I have added the code RunMe. Before running the code, select the first sheet (the one with a hidden grid). Since I can't reproduce your sheet names (it just ends up showing ????1) in the code, I used ActiveSheet as the sheet reference.
Here is your file:
https://ufile.io/qq1im9c6
Best regards,
Trowa
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
21 Oct 2019 à 13:47
21 Oct 2019 à 13:47
what do you mean the run code ? what you want for me and fix which line code? i'm confused
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
22 Oct 2019 à 11:33
22 Oct 2019 à 11:33
Running a code means the same as executing a code. Alt + F8 shows a list of macro codes, then double click "RunMe".
The code will check the active sheet if there are values in the range A19 to A31. When there in no value then the entire row will be deleted and then move on to check the column A value in the next row.
I'm a bit confused as well as you wanted the code to work for sheet2, but after looking at your file, sheet2 doesn't have data in the rows 19 to 31. Your sheet1 does have the data as shown in your screenshots. That is why I mentioned you might want to change sheet2 reference to sheet1.
The code will check the active sheet if there are values in the range A19 to A31. When there in no value then the entire row will be deleted and then move on to check the column A value in the next row.
I'm a bit confused as well as you wanted the code to work for sheet2, but after looking at your file, sheet2 doesn't have data in the rows 19 to 31. Your sheet1 does have the data as shown in your screenshots. That is why I mentioned you might want to change sheet2 reference to sheet1.
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
22 Oct 2019 à 11:52
22 Oct 2019 à 11:52
to clear for your confusing when i said deleting from rows 19:31 not sheet1 because i need to fill data next time you note when i fill data in sheet1 and transfer to sheet2 it contains more than process this like archive to save many operation just i need transfer filling data sheet1 not empty as i said you based on condition to sheet2 the rows 19:31 is not relation sheet2
by the way your code is worked but not as i want
i hope this help to understand it
by the way your code is worked but not as i want
i hope this help to understand it
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
22 Oct 2019 à 11:58
22 Oct 2019 à 11:58
For what I understand what you are trying to do is copy the table from sheet1 to sheet2 and then remove the empty rows.
The code to do that is:
I have changed the sheet references to english, for me to work with.
To see it in action, check your file here (where I also added the formula =E21 to cell E31 of sheet1, which changes automatically to the corresponding cell on sheet2):
https://ufile.io/682hkgel
Hopefully that will clean up all the confusion that might be left.
Best regards,
Trowa
The code to do that is:
Sub RunMe()
Sheets("Sheet2").Rows("83:96").Delete
Sheets("Sheet1").Rows("18:31").Copy Sheets("Sheet2").Rows("83")
Dim x As Integer
For x = 95 To 84 Step -1
If Sheets("Sheet2").Range("A" & x).Value = vbNullString Then Sheets("Sheet2").Range("A" & x).EntireRow.Delete
Next x
End Sub
I have changed the sheet references to english, for me to work with.
To see it in action, check your file here (where I also added the formula =E21 to cell E31 of sheet1, which changes automatically to the corresponding cell on sheet2):
https://ufile.io/682hkgel
Hopefully that will clean up all the confusion that might be left.
Best regards,
Trowa
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
22 Oct 2019 à 12:56
22 Oct 2019 à 12:56
yes this is what i want the result attched file i'm really sorry to say that it's still problem when i press macro again it comes back the problem :
the first image
this is what i want

and when i press macro again it changes the past data and the new data come back the past problem
the past data become this:

the new data become this

the first image
this is what i want
and when i press macro again it changes the past data and the new data come back the past problem
the past data become this:
the new data become this
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
24 Oct 2019 à 11:29
24 Oct 2019 à 11:29
Hi Abdel,
That is not the result I'm getting. Are you by any chance working on a Mac? That seems to mess with codes.
I did find a better formula for Sheet1 E31: =MIN(E19:E30). This way you don't have to change the formula every time the number of items changes.
Let us look at the code:
Code line 2: The table is deleted from sheet2.
Code line 3: The table from sheet1 is copied to sheet2.
Code line 7: For each row in the table on sheet2, when column A is empty, then the row will be deleted.
So for a row NOT to be deleted, there must be something in column A.
Hopefully that creates some understanding of what is happening.
Best regards,
Trowa
That is not the result I'm getting. Are you by any chance working on a Mac? That seems to mess with codes.
I did find a better formula for Sheet1 E31: =MIN(E19:E30). This way you don't have to change the formula every time the number of items changes.
Let us look at the code:
Sub RunMe()
Sheets("Sheet2").Rows("83:96").Delete
Sheets("Sheet1").Rows("18:31").Copy Sheets("Sheet2").Rows("83")
Dim x As Integer
For x = 95 To 84 Step -1
If Sheets("Sheet2").Range("A" & x).Value = vbNullString Then Sheets("Sheet2").Range("A" & x).EntireRow.Delete
Next x
End Sub
Code line 2: The table is deleted from sheet2.
Code line 3: The table from sheet1 is copied to sheet2.
Code line 7: For each row in the table on sheet2, when column A is empty, then the row will be deleted.
So for a row NOT to be deleted, there must be something in column A.
Hopefully that creates some understanding of what is happening.
Best regards,
Trowa
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
31 Oct 2019 à 12:43
31 Oct 2019 à 12:43
Hi Abdel,
Ok, so you want to keep the transfered data, when you transfer a new data table.
To be complete change the formula's on Sheet1:
E19: =Sheet1!$E$9-D19
E31: =MIN(E19:E30)
Then use the following code:
Best regards,
Trowa
Ok, so you want to keep the transfered data, when you transfer a new data table.
To be complete change the formula's on Sheet1:
E19: =Sheet1!$E$9-D19
E31: =MIN(E19:E30)
Then use the following code:
Sub RunMe()
Dim lRow As Integer
lRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet1").Range("A18:E31").Copy Sheets("Sheet2").Range("A" & lRow + 2)
Dim x As Integer
For x = lRow + 14 To lRow + 2 Step -1
If Sheets("Sheet2").Range("A" & x).Value = vbNullString Then Sheets("Sheet2").Range("A" & x).EntireRow.Delete
Next x
End Sub
Best regards,
Trowa
abdelfatah_0230
Posts
73
Registration date
Thursday 18 July 2019
Status
Member
Last seen
23 July 2022
31 Oct 2019 à 14:30
31 Oct 2019 à 14:30
thanks so much finally the code perfectly works i appreciate your efforts
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
4 Nov 2019 à 11:35
4 Nov 2019 à 11:35
Nice, thanks for the feedback!