3 Actions VBA Copy, Clear contents ("") and Shift all to left

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Hello,</SPAN></SPAN>

Below example columns C:K result are by formulas, all formulas have not an outcome; in that case the formula has leaved a cell blank with (""). </SPAN></SPAN>

Now I need a VBA code that does 3 actions...</SPAN></SPAN>
1-Copy values of columns C:K into M:U</SPAN></SPAN>
2- Clear contents ("") of columns M:U</SPAN></SPAN>
3-Shift all values to left </SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTU
1
2
3
4
5n1n2n3n4n5n6n7n8n9n1n2n3n4n5n6n7n8n9
622212221
722122212
822212221
91111311113
1023112311
11211111211111
12232232
132211122111
141111311113
1521312131
161211212112
17232232
1841114111
192121121211
20211111211111
212112121121
222112121121
233111131111
242211122111
256161
2632113211
2721222122
2812221222
291212112121
3032113211
316161
3213211321
33121111121111
3431213121
351221112211
362111221112
3731123112
383111131111
3921222122
401221112211
4131123112
421212112121
432112121121
442211122111
4541114111
4641114111
4741114111
4831123112
4923112311
5041114111
511221112211
5241114111
53211111211111
54211111211111
551113111131
561122111221
57211111211111
5841114111
595252
601212112121
Sheet8


Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Check if it suites all the purpose!

Sub chk_val()
Dim l_row As Integer
Dim i As Integer, j As Integer, k As Integer
l_row = 0
k = 1
For i = 3 To 11
If l_row <= Cells(Rows.Count, i).End(xlUp).Row Then l_row = Cells(Rows.Count, i).End(xlUp).Row
Next
Range("M6:V" & l_row).ClearContents
For i = 6 To l_row
For j = 1 To 9
If Cells(i, 2 + j).Value2 <> "" Then
Cells(i, 12 + k) = Cells(i, 2 + j)
k = k + 1
End If
Next
k = 1
For j = 1 To 9
Cells(i, 22).Value2 = Cells(i, 22).Value2 & Cells(i, 12 + j).Value2 & " | "
Next
Next
End Sub
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Check if it suites all the purpose!

Sub chk_val()
Dim l_row As Integer
Dim i As Integer, j As Integer, k As Integer
l_row = 0
k = 1
For i = 3 To 11
If l_row <= Cells(Rows.Count, i).End(xlUp).Row Then l_row = Cells(Rows.Count, i).End(xlUp).Row
Next
Range("M6:V" & l_row).ClearContents
For i = 6 To l_row
For j = 1 To 9
If Cells(i, 2 + j).Value2 <> "" Then
Cells(i, 12 + k) = Cells(i, 2 + j)
k = k + 1
End If
Next
k = 1
For j = 1 To 9
Cells(i, 22).Value2 = Cells(i, 22).Value2 & Cells(i, 12 + j).Value2 & " | "
Next
Next
End Sub
Wow!! KolGuyXcel, it is just amazing solution spot on!! :beerchug:</SPAN></SPAN>

Thank you very much for your help and time for giving key code
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti :-D
</SPAN></SPAN>
 
Upvote 0
Just to post the code for what I suggested adding to Ricks code.
Code:
Sub motilulla()
    Dim lr As Long
    Application.ScreenUpdating = -False
    lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
    Range("V6:V" & lr).ClearContents
    With Range("C6:K" & lr)
        .Offset(, 10).Value = .Value
        .Offset(, 10).SpecialCells(xlBlanks).Delete xlShiftToLeft
    End With
    Range("V6:V" & lr).FormulaR1C1 = _
    "=RC[-9]&"" | ""&RC[-8]&"" | ""&RC[-7]&"" | ""&RC[-6]&"" | ""&RC[-5]&"" | ""&RC[-4]&"" | ""&RC[-3]&"" | ""&RC[-2]&"" | ""&RC[-1]&"" | """
    Application.ScreenUpdating = True
End Sub

The above took 52.4 milliseconds to run on the data in post number 1, the code in post number 11 took 243.72 milliseconds to run (170.6 if you turn screenupdating off).
Obviously there is an option of using arrays which would also be fast.
 
Upvote 0
Just to post the code for what I suggested adding to Ricks code.
Code:
Sub motilulla()
    Dim lr As Long
    Application.ScreenUpdating = -False
    lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
    Range("V6:V" & lr).ClearContents
    With Range("C6:K" & lr)
        .Offset(, 10).Value = .Value
        .Offset(, 10).SpecialCells(xlBlanks).Delete xlShiftToLeft
    End With
    Range("V6:V" & lr).FormulaR1C1 = _
    "=RC[-9]&"" | ""&RC[-8]&"" | ""&RC[-7]&"" | ""&RC[-6]&"" | ""&RC[-5]&"" | ""&RC[-4]&"" | ""&RC[-3]&"" | ""&RC[-2]&"" | ""&RC[-1]&"" | """
    Application.ScreenUpdating = True
End Sub
The above took 52.4 milliseconds to run on the data in post number 1, the code in post number 11 took 243.72 milliseconds to run (170.6 if you turn screenupdating off).
Obviously there is an option of using arrays which would also be fast.

Thank you MARK858, for adding a formula as you suggest. Yes it worked, but I will go with KolGuyXcel, code. Because 1-that keeps the format, 2-if cases I extend my project to right columns also it works fine do not miss match the next columns. Over all seems to me very useful and I find it more versatile macro solution.
</SPAN></SPAN>

I do appreciate your help and time you spent to find a solution.
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti :)
</SPAN></SPAN>
 
Upvote 0
If you mean by "keeps the format" the borders that is just

Code:
Sub motilulla()
    Dim lr As Long
    Application.ScreenUpdating = -False
    lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
    Range("V6:V" & lr).ClearContents
    With Range("C6:K" & lr)
        .Offset(, 10).Value = .Value
        .Offset(, 10).SpecialCells(xlBlanks).Delete xlShiftToLeft
        [COLOR="#FF0000"].Offset(, 10).SpecialCells(xlConstants).Borders.Weight = xlMedium[/COLOR]
    End With
    Range("V6:V" & lr).FormulaR1C1 = _
    "=RC[-9]&"" | ""&RC[-8]&"" | ""&RC[-7]&"" | ""&RC[-6]&"" | ""&RC[-5]&"" | ""&RC[-4]&"" | ""&RC[-3]&"" | ""&RC[-2]&"" | ""&RC[-1]&"" | """
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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