Remove blank cell of rows and shift the rest to left

mehrana

New Member
Joined
Aug 1, 2024
Messages
19
Office Version
  1. 365
Platform
  1. Windows
I have a table with 60+ columns. More data is periodically added (using Microsoft form) to this table. There are / will be many blank cells in each row (80% of the entire cells). Instead of this very wide table, I need to display only the data on a screen without the blank cells. The screen can of any kind, such as another table with the blank cells removed and data cells are shifted to left. It will be great if this can done automatically each time when a new row is added or more data is added to cells (on left side of the existing).

I appreciate any help you can provide.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Please see the images below.

#1 existing data > #2 how to display the date
 

Attachments

  • Screenshot_1.png
    Screenshot_1.png
    65.2 KB · Views: 12
  • Screenshot_2.png
    Screenshot_2.png
    64.9 KB · Views: 12
Upvote 0
Correction: more data is added to cells (on right side of the existing) >> the cells are blank.
 
Upvote 0
you could try this code. Recommend you try it on dummy data first though just to be safe.
VBA Code:
Sub mehrana()

Dim LR As Long, LC As Long
Dim Starttime As Double, Secondselapsed As Double


Starttime = Timer
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False

LR = Cells(Rows.count, "A").End(xlUp).Row
LC = Cells(1, Columns.count).End(xlToLeft).Column + 1
For C = LC To 1 Step -1
    For R = LR To 1 Step -1
        If Cells(R, C).Value = "" Then
            Cells(R, C).Delete shift:=xlToLeft
            Else
        End If
    Next R
Next C
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Secondselapsed = Round(Timer - Starttime, 2)
MsgBox "This code ran successfully in " & Secondselapsed & " seconds", vbInformation
End Sub
 
Upvote 0
Thank you very much for the macro. As you suggested, I will try in a copy of the spreadsheet. I need to learn how to add a macro first!
 
Upvote 0
Another option with a formula:

Would this work? Columns A:H is Table1, which could be in another sheet than the formula.

Book6.xlsx
ABCDEFGHIJKLMNOP
1Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8
2Aab777abcAab777abc
3Bbc127788Bbc127788
4CdeabceCdeabce
5Deg1Deg1
6EkksddsafEkksddsaf
7ddeeddee
8baadfbaadf
9aa
10
Sheet6
Cell Formulas
RangeFormula
J2:P9J2=LET(t, Table1, tj, BYROW(t, LAMBDA(x, TEXTJOIN("|", 1, x)&"|")), res, DROP(REDUCE("", tj, LAMBDA(arr,new, VSTACK(arr, TEXTSPLIT(new, "|")))), 1), IFERROR(res, "") )
Dynamic array formulas.
 
Upvote 0
Yes, thank you. This is exactly what I need!
Can I automate the formula to run each time a new row is added and/or when new data is added to one or more existing blank cell(s) in the range (on right side)?
 
Upvote 0
you could try this code. Recommend you try it on dummy data first though just to be safe.
VBA Code:
Sub mehrana()

Dim LR As Long, LC As Long
Dim Starttime As Double, Secondselapsed As Double


Starttime = Timer
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False

LR = Cells(Rows.count, "A").End(xlUp).Row
LC = Cells(1, Columns.count).End(xlToLeft).Column + 1
For C = LC To 1 Step -1
    For R = LR To 1 Step -1
        If Cells(R, C).Value = "" Then
            Cells(R, C).Delete shift:=xlToLeft
            Else
        End If
    Next R
Next C
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Secondselapsed = Round(Timer - Starttime, 2)
MsgBox "This code ran successfully in " & Secondselapsed & " seconds", vbInformation
End Sub
This Macro did not work. It deleted any column that had 1 or more blank cell, and shifted columns will all filled cells to left
 
Upvote 0
Another option with a formula:

Would this work? Columns A:H is Table1, which could be in another sheet than the formula.

Book6.xlsx
ABCDEFGHIJKLMNOP
1Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8
2Aab777abcAab777abc
3Bbc127788Bbc127788
4CdeabceCdeabce
5Deg1Deg1
6EkksddsafEkksddsaf
7ddeeddee
8baadfbaadf
9aa
10
Sheet6
Cell Formulas
RangeFormula
J2:P9J2=LET(t, Table1, tj, BYROW(t, LAMBDA(x, TEXTJOIN("|", 1, x)&"|")), res, DROP(REDUCE("", tj, LAMBDA(arr,new, VSTACK(arr, TEXTSPLIT(new, "|")))), 1), IFERROR(res, "") )
Dynamic array formulas.

Another option with a formula:

Would this work? Columns A:H is Table1, which could be in another sheet than the formula.

Book6.xlsx
ABCDEFGHIJKLMNOP
1Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8
2Aab777abcAab777abc
3Bbc127788Bbc127788
4CdeabceCdeabce
5Deg1Deg1
6EkksddsafEkksddsaf
7ddeeddee
8baadfbaadf
9aa
10
Sheet6
Cell Formulas
RangeFormula
J2:P9J2=LET(t, Table1, tj, BYROW(t, LAMBDA(x, TEXTJOIN("|", 1, x)&"|")), res, DROP(REDUCE("", tj, LAMBDA(arr,new, VSTACK(arr, TEXTSPLIT(new, "|")))), 1), IFERROR(res, "") )
Dynamic array formulas.
Yes, thank you. This is exactly what I need!

I tried it a few times, not sure exactly to make it work.

Can I automate the formula to run each time a new row is added and/or when new data is added to one or more existing blank cell(s) in the range (on right side)?
 
Last edited:
Upvote 0
Yes, thank you. This is exactly what I need!
Can I automate the formula to run each time a new row is added and/or when new data is added to one or more existing blank cell(s) in the range (on right side)?
It will update automatically every time the table changes.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top