• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA to move data cells to up and Blank cells stay after data in multiple table.

CemHyur

Member
Hi!

need VBA code for table that move data cells from starting of the table and blanks cells after data cells.

Have Like this,
51.jpg

there are many tables like this below these tables,
Tables have format like Column B to F, H, K & L Manually input and Column G & I have formula. Talbe have no header row (means hided) and have total row.
So need to do that if first row of Column B of the table have data then check next row of Column B; and if cell of Column B found empty then copy data from next non-empty cell of columns B to F, H, K & L and paste here and then delete from copied cells. loop through all rows until last row of the table. and same loop through all tables in activesheet.

Please note that don't want to delete any row of cell, just move it.


Want result like this
52.jpg


for the same have write and tried below code, but it is not giving end result as want. It is working in 1st two row properly and then copying value from up side instead to find down side. and all data rows pasting at end of the table.

Code:
Option Explicit
Sub CopyDataToBlanks()
    Dim tbl As ListObject
    Dim rng As Range
    Dim i As Long

    For Each tbl In ActiveSheet.ListObjects
    Set rng = tbl.Range

    For i = 1 To rng.Rows.Count - 1
       If rng.Cells(i, 1) = "" Then
         rng.Cells(1, 1).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 1)
         rng.Cells(1, 1).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 2).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 2)
         rng.Cells(1, 2).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 3).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 3)
         rng.Cells(1, 3).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 4).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 4)
         rng.Cells(1, 4).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 5).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 5)
         rng.Cells(1, 5).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 7).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 7)
         rng.Cells(1, 7).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 10).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 10)
         rng.Cells(1, 10).Offset(1, 0).End(xlDown).ClearContents

         rng.Cells(1, 11).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 11)
         rng.Cells(1, 11).Offset(1, 0).End(xlDown).ClearContents
       End If
    Next i
  Next tbl
End Sub


this code result:
53.jpg

Thank You.
 

CemHyur

You should able to send a sample Excel-file,
which has some sample of realistic tables with sample data.
It's a challenge to verify any code with pictures.
 
How about just sorting them?:
Code:
Sub blah()
Dim lo
For Each lo In Worksheets("HaveLikeThis").ListObjects
  With lo.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=lo.listcolumns(1).range, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
Next lo
End Sub
 
Hi p45cal,
Thanks for your time.

But sorting will sort whole table from 1 column to last, I want to shift data cells up from specific columns only not whole table.

Thank You.
 

CemHyur

Your original wish is from starting of the table.
What is that? ... which row? or ... how do You know from which row?
What is the last row?
... Your code has from 1 to end of table.
from #5 reply I want to shift data cells up from specific columns only not whole table.
... now, You wrote about from ... columns and not whole table.
Could You explain other way - what would You like to get?
 
But sorting will sort whole table from 1 column to last, I want to shift data cells up from specific columns only not whole table.
Can you show me the differences between the two sheets after running the code in msg#4?
I have checked and I can see none (neither in the values nor in the formulae).
 
Hi vletm,

Thank You.

Your original wish is from starting of the table.
Yes means code should work from 1st row to last row of each Table.
What is the last row?
don't know, that's why I added "tbl.rng" in code. (can add "tbl.databodyrang.count" to find last row of table)
from #5 reply I want to shift data cells up from specific columns only not whole table.
as I mantioned in #1 "if cell of Column B found empty then copy data from next non-empty cell of columns B to F, H, K & L" not want whole table. Just cell from these columns only.
Could You explain other way - what would You like to get?
That non-empty cells of Columns B to F, H, K & L sholud comes up.

like this. (PLEASE NOTE THAT FROM ONLY COLUMNS B, C, D, E, F, H, K & L)
52.jpg



Hope it make sense and thank you very much. Please get me correct if I am mistakeing somewhere.

Code in #1 is working but don't know why it look up for data?
as added that "xlDown" here
rng.Cells(1, 1).Offset(1, 0).End(xlDown).Copy rng.Cells(i, 1)

(1st two row code works fine, but in 3rd row code take data from 2nd row and so on and at the end of the code data comes ending of the table.)


Thanks.
 
Hi P45cal,

Can you show me the differences between the two sheets after running the code in msg#4?
I know, I had tried sorting before posting on forum. In sheet, column J I have left blank as I don't want to sort this column and also (not done yet) but I am thinking about adding serial number to the tables in Column A and will make it a part of table. So that's why I don't want to sort whole table.

I have checked and I can see none (neither in the values nor in the formulae).
You are right, It will not mak any diiference.

Thank You.
 

CemHyur

like this. (PLEASE NOTE THAT FROM ONLY COLUMNS B, C, D, E, F, H, K & L)
Above could make different result .... it You really would like to so?
... everything in columns G & I & J will stay in same place, no matter if other columns data will move up.
... could You create a samples which really shows that kind of expected result?
Or
use sort
 
it You really would like to so?
... everything in columns G & I & J will stay in same place, no matter if other columns data will move up.
Yes yes.
Don't want to change anything in G, I & J columns... Stay data where they are.

could You create a samples which really shows that kind of expected result?
I had shared excel in #3 and vba also added there.

but Vba is taking data to bottom of the table (means last rows of table) instead of up.

Please check.

Thank you.
 
But sorting will sort whole table from 1 column to last, I want to shift data cells up from specific columns only not whole table.
Hi, that's wrong according to your post #3 attachment, just manually sorting range by range that works as expected, without any VBA code !​
So you misexplained your need or your attachment does not well reflect it, not accurate according to your real workbook.​
As guessing can't be coding !​
At kid level to share a workbook with an exact before state and an exact expected result rather than confusing pics …​
 
Don't want to change anything in G, I & J columns... Stay data where they are.
In your WantToLikeThis sheet those data have moved!
If it's just column J where data remains in the same place, then attach a file with data in that column and show result too.
 

CemHyur

I checked Your file - and there are Your expected results different that You have written ...
... everything in columns G & I & J will stay in same place, no matter if other columns data will move up.
... could You create a samples which really shows that kind of expected result?

You've written something like below ... I marked with red font those which You expected to stay.
Screenshot 2023-12-16 at 17.41.37.png
Of course, that is possible, but ... is it useful?
 
Hi Marc L,
Thanks for your time.

just manually sorting range by range that works as expected, without any VBA code !
Is it mean that it has to be done manually and can't done it through vba?

you misexplained your need or your attachment does not well reflect it.
As guessing can't be coding !
I don't think so I misexplained, i have tried to explain everything as best I can. Still if you think so then please ask your query. And i didn't kept anything for guessing, explained everything that require end result should be.
At kid level to share a workbook
I am at nursery level in vba.
exact expected result rather than confusing pics
Expected result clearly explained in post, that move data cells to upper side.

I never say in post that i want to sort table. I am just trying to keep moving data upward from some columns only through vba.

Thank you.
 
your WantToLikeThis sheet those data have moved!
If it's just column J where data remains in the same place, then attach a file with data in that column and show result too.
Hi, in Column J, anything added will be stay as it is, because i am using it like reference, that i have deleted from this cell, so in future i can check with old sheet. So i don't want to change anything in that column.
I will also share excel with feeding some data afterwards, coz I'm out right now.

Thanks.
 
You've written something like below ... I marked with red font those which You expected to stay.
Hi valetm,

You hv moved data through cut paste? (I am guessing this)

Coz I had tried that before and got this type of result as you pic attached. (Exactly I don't remember, but result was like this) and it is changing formulas in column G and column I.

So No cut paste, coz in column G 3rd row formula subtract value from 2nd row... formula should take value from what cell reference given to them and shouldn't be change.

So for that I did it with copy paste and then delete copied data. Please check, you can do better in what I did...

Thanks.
 

CemHyur

That layout can do many ways - it's only matter - what do You really would like to have?
.. as You've verified few times everything in columns G & I & J will stay in same place, no matter if other columns data will move up.
Now Your ... want is different coz in column G 3rd row formula subtract value from 2nd row...
Those cannot be same! ... Your everything
What is Your point that You won't use 'Sorting'?
... which given same looking results as in Your file.
 
Vletm,

as You've verified few times everything in columns G & I & J will stay in same place, no matter if other columns data will move up.
Right this is what I want.

Now Your ... want is different coz in column G 3rd row formula subtract value from 2nd row...
Please check complete line.
So No cut paste, coz in column G 3rd row formula subtract value from 2nd row...
I said this after cheking your post #14 the image you attached is showing that, In your image the data in Column G (formulas) is changed. I don't want to disturb G & I & J.

anyways, I got the solution, I will add it in next post, after doing some tests. Please check that and help me if you polish that code.

Thank you for looking in this and bearing with me.

Sorry if I troubled you.
Once again Thank You.
 
Hi Guys,
after wasting 3 weeks and learning and traying, I have write this 1st small VBA code that what I was looking for.
Pasting here so that in future it can help needy one. If anyone can polish this code please do it.

and vletm & p45cal & Marc L, Thank you very much guys for your questions and queries, coz due to your questions I learned and tried something and get solved. Request you please check it and if you find any mistake in it please rectify it. Thank you very much.

In this code there is one problem with my sheet in post #3 that I have added "Total" word at total row in each table, and code is taking this total row also to the up, So that I have removed that "Total" word from all the tables and kept that cell blank, now code is working for me.
here is code:
Code:
Option Explicit
Sub CopyDataToBlanks()
    Dim tbl As ListObject
    Dim rng As Range
    Dim s As Long
    Dim i As Long

    For Each tbl In ActiveSheet.ListObjects
    Set rng = tbl.DataBodyRange

    For i = 1 To rng.Rows.Count - 1
       If rng.Cells(i, 1) = "" Then
            If Not rng.Cells(i, 1).Offset(0, 0).End(xlDown).Value = "" Then
                 s = rng.Cells(i, 1).Offset(0, 0).End(xlDown).Row - rng.Cells(1, 1).Row + 1
                 rng.Cells(s, 1).Copy rng.Cells(i, 1)
                 rng.Cells(s, 1).ClearContents
        
                 rng.Cells(s, 2).Copy rng.Cells(i, 2)
                 rng.Cells(s, 2).ClearContents

                 rng.Cells(s, 3).Copy rng.Cells(i, 3)
                 rng.Cells(s, 3).ClearContents

                 rng.Cells(s, 4).Copy rng.Cells(i, 4)
                 rng.Cells(s, 4).ClearContents

                 rng.Cells(s, 5).Copy rng.Cells(i, 5)
                 rng.Cells(s, 5).ClearContents

                rng.Cells(s, 7).Copy rng.Cells(i, 7)
                 rng.Cells(s, 7).ClearContents

                 rng.Cells(s, 10).Copy rng.Cells(i, 10)
                 rng.Cells(s, 10).ClearContents

                 rng.Cells(s, 11).Copy rng.Cells(i, 11)
                 rng.Cells(s, 11).ClearContents
            End If
       End If
    Next i
  Next tbl
End Sub
 
Does your post #20 code works with your post #3 attachment ?​
If not share the workbook with the code included in order we can test …​
 
Hi Marc L,

Does your post #20 code works with your post #3 attachment ?
Yes, working...
after deleting "Total" word from Last Row of First Column from Every Table.

share the workbook with the code included in order we can test …
Yeah sure, Please find attached sample excel. Added Code and deleted "Total" Word from all Tables.

Thanks.
 

Attachments

  • SampleSheet_1.xlsm
    36.2 KB · Views: 4
Back
Top