VBA: shift data in a range towards left

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have a variable range defined as set = range1 in which there are empty cells.

I need, for each row in the range, to shift all not empty cells toward the left.
The operation has not to impact the columns on the right of the range, because they contains other data.
For example, range C3:L28, column from N to the right have to remain at their place.

How can I manage the issue?

Thank's.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hello everybody.

I have a variable range defined as set = range1 in which there are empty cells.

I need, for each row in the range, to shift all not empty cells toward the left.
The operation has not to impact the columns on the right of the range, because they contains other data.
For example, range C3:L28, column from N to the right have to remain at their place.

How can I manage the issue?

Thank's.

More precisely, I mean:

Code:
set range1 = Range(...........)
 
Last edited:
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Jul08
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C3:M28")
Application.ScreenUpdating = False
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Rows
    ReDim Ray(1 To Rng.Columns.Count)
    c = 0
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Rw [COLOR="Navy"]In[/COLOR] Dn.Cells
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Rw.Value) [COLOR="Navy"]Then[/COLOR]
           c = c + 1
           Ray(c) = Rw.Value
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw
Dn.Cells.ClearContents
Dn(1).Resize(, c).Value = Ray
[COLOR="Navy"]Next[/COLOR]
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Jul08
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C3:M28")
Application.ScreenUpdating = False
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Rows
    ReDim Ray(1 To Rng.Columns.Count)
    c = 0
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Rw [COLOR="Navy"]In[/COLOR] Dn.Cells
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Rw.Value) [COLOR="Navy"]Then[/COLOR]
           c = c + 1
           Ray(c) = Rw.Value
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw
Dn.Cells.ClearContents
Dn(1).Resize(, c).Value = Ray
[COLOR="Navy"]Next[/COLOR]
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

Excel VBA run-time error 1004 : Application-defined or object-defined error

on the following line:

Code:
Dn(1).Resize(, c).Value = Ray
 
Upvote 0
Add lines as shown in red:-
Code:
Next Rw
[B][COLOR=#ff0000]If c > 0 Then[/COLOR][/B]
    Dn.Cells.ClearContents
    Dn(1).Resize(, c).Value = Ray
[B][COLOR=#ff0000]End If[/COLOR][/B]
Next
 
Upvote 0
Add lines as shown in red:-
Code:
Next Rw
[B][COLOR=#ff0000]If c > 0 Then[/COLOR][/B]
    Dn.Cells.ClearContents
    Dn(1).Resize(, c).Value = Ray
[B][COLOR=#ff0000]End If[/COLOR][/B]
Next

Yes, now the process reaches the end instead of having an error.

Yet, something strange happens to the dates. If you see the image, on each single row the scheme is always date followed by text. The yellow area is entirely formatted "general".

At the end of the process, some data assumes the format "date" for "general", but this date is incoherent because is a futur date (for example 08 january 2018 becomes 01 august 2018, and this is not possible because the date refers to event already happened).

How can I manage this issue about date?

Thank's.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,603
Messages
6,125,788
Members
449,260
Latest member
Mrw1

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