If there is unhighlighted data after the highlighted cell in a row then we have to clear all the data from start till highlighted cell and paste the unhighlighted data from start in the row

If there is unhighlighted   data after the highlighted cell in a row then we have to clear all the data from start  till highlighted cell and paste the unhighlighted data from start in the row
and if there is no data after the highlighted cell in a row then simply copy and paste that row don’t do anything with that row
Sample-7.xlsm
Avinash SinghAsked:
Who is Participating?
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this...
Sub UnHighlightedData()
Dim lr As Long, lc As Long, i As Long, j As Long, c As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
    lc = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = lc To 2 Step -1
        If Cells(i, j).Interior.ColorIndex = xlNone Then
            c = j - 1
        ElseIf Cells(i, j).Interior.ColorIndex <> xlNone Then
            Exit For
        End If
    Next j
    If c <> 0 Then Range(Cells(i, 2), Cells(i, c)).Delete shift:=xlToLeft
    c = 0
Next i
End Sub

Open in new window

In the attached, click the button called "Shift Data" on Sheet1 to run the code.
Sample-7.xlsm
0
 
Avinash SinghAuthor Commented:
Thnx again  Subodh Tiwari (Neeraj) Sir for giving ur precious time
u r very supportive sir
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Avinash!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.