Shift cells up

whitoulias

Board Regular
Joined
Jun 22, 2012
Messages
153
Good day everyone

I have this code (command button1) which works just fine. What it does is if cell 02 in sheet1 is "14" then it copies the entire row to sheet2. The same goes for all O column

I would like to change it a little bit.

Instead of copy i would like to cut the row, paste it in sheet2 and shift cells up in sheet1

Any ideas.

Thank u in advance

Code:
Private Sub CommandButton1_Click()
Dim FirstAddress As String
    Dim myArr As Variant
    Dim Rng As Range
    Dim Rcount As Long
    Dim I As Long
    Dim LastCol As String


    Application.ScreenUpdating = False

    myArr = Array("14")

 

    Rcount = 0
    With Sheets("Sheet1").Range("O1:O1000")
    

        For I = LBound(myArr) To UBound(myArr)


            Set Rng = .Find(What:=myArr(I), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rcount = Rcount + 2

                    LastCol = Chr(Worksheets("Sheet1").Range("A" & Rng.Row).End(xlToRight).Column + 64)
                    Worksheets("Sheet1").Range("A" & Rng.Row & ":" & LastCol & Rng.Row).Copy _
                    Destination:=Worksheets("Sheet2").Range("A" & Rcount)
                    'Sheets("Sheet2").Range("A" & Rcount).Value = Rng.Value
                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next I
    End With
    Application.ScreenUpdating = True

End Sub
 
Hello whitoulias,

Give this a try, it may do. I did notice the result from using this are in a reverse order. I suppose a bit more debugging could have that worked out... maybe using a step -1 in the loop.


Code:
Private Sub CommandButton1_Click()
Dim c As Long
        
        For c = 2 To Range("O2").End(xlDown).Row   '[highlight]For c = Range("O2").End(xlDown).Row to 2  step -1 (As a guess).[/highlight]
            With Range("O" & c)
                If .Value = 14 Then
                    With .EntireRow
                        .Copy Destination:=Worksheets("Sheet2").Range("A" & Range("A1").End(xlDown).Row)
                        .Delete Shift:=xlUp
                    End With
                    c = c - 1
                End If
            End With
        Next c
        
End Sub

Also welcome to MrExcel message board!!!
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thank u for the reply i'll give it a try tommorow and let u know

Sounds great, I am curious as I think it is very close to what you would like.

Here is an update:


Code:
Private Sub CommandButton1_Click()
Dim c As Long
      
        For c = Range("O2").End(xlDown).Row To 2 Step -1
            With Range("O" & c)
                If .Value = 14 Then
                    With .EntireRow
                        .Copy Destination:=Worksheets("Sheet2").Range("A" & Range("A1").End(xlDown).Row)
                        .Delete Shift:=xlUp
                    End With
                    c = c + 1
                End If
            End With
        Next c
        
End Sub
 
Upvote 0
First of all i'm impressed by the simplicity of this code:)

Secondly it does cut and shift cells up in sheet1- that's great.

The tricky thing is that it started copying data to sheet2 from cell a190 and if i continue it copies data from that cell and up!!!
 
Upvote 0
I did notice another thing

If in column O there is an empty cell and after that "14" appears, the code is not able to read it and cut+paste:eek:
 
Upvote 0
Hi, :)

try:

Code:
Private Sub CommandButton1_Click()
    Dim strFirstAddress As String
    Dim intLastCol As Integer
    Dim varSearch As Variant
    Dim intCount As Integer
    Dim rngRange As Range
    Dim rngTMP As Range
    Dim lngRow As Long
    On Error GoTo Fin
    Application.ScreenUpdating = False
    varSearch = Array("14")
    intLastCol = ThisWorkbook.Worksheets("Sheet1").Range("A1").End(xlToRight).Column
    For intCount = LBound(varSearch) To UBound(varSearch)
        With ThisWorkbook.Worksheets("Sheet1").Range("O1:O1000")
            Set rngRange = .Find(varSearch(intCount), , xlValues, xlWhole)
            If Not rngRange Is Nothing Then
                strFirstAddress = rngRange.Address
                Do
                    With ThisWorkbook.Worksheets("Sheet1")
                    If rngTMP Is Nothing Then
                        Set rngTMP = .Range(.Cells(rngRange.Row, 1), _
                            .Cells(rngRange.Row, 15))
                    Else
                        Set rngTMP = Application.Union(rngTMP, .Range(.Cells(rngRange.Row, 1), _
                            .Cells(rngRange.Row, 15)))
                    End If
                    lngRow = lngRow + 2
                    .Range(.Cells(rngRange.Row, 1), .Cells(rngRange.Row, intLastCol)).Copy _
                        Destination:=Worksheets("Sheet2").Range("A" & lngRow)
                    Set rngRange = ThisWorkbook.Worksheets("Sheet1").Range("O1:O1000").FindNext(rngRange)
                    If rngRange Is Nothing Then Exit Do
                    End With
                Loop While rngRange.Address <> strFirstAddress
                If Not rngTMP Is Nothing Then rngTMP.Delete
            End If
        End With
    Next intCount
Fin:
    Application.ScreenUpdating = True
    If Err.Number <> 0 Then MsgBox "Error: " & _
        Err.Number & " " & Err.Description
End Sub

Case thanks for your reply
Your code is working fine but there are 2 issues.
The data is always copied to sheet2 A2 cell resulting previous data to be lost
After every record copied in sheet2 there is an empty row -this by the way is not an issue
 
Upvote 0
Hello,

Using (xlDown), finds the last cell before a blank, hence the reason for failing to resume. You can see this effect by selecting cell O2 on sheet1 and press the end key, then press the down arrow.


As for the results on sheet2, how would be best to place the copied lines?


The below code will go through about 1000 lines in sheet1 (changed from xlDown), alot like the original code. That should deal with the stopping of the macro at a blank in column O. Of course assuming there are less than 999 records.

As for placement onto sheet2, the below will start placing the copied records at row 2 and continue down, overwriting anything currently there. I will be glad to help make ammends to this if you would like, just explain what you would like to happen and I will do my best.



Code:
[FONT=Franklin Gothic Medium][COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
[COLOR=darkblue]Dim[/COLOR] c [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] myRow [COLOR=darkblue]As[/COLOR] Long: myRow = 2
      
        [COLOR=darkblue]For[/COLOR] c = 2 [COLOR=darkblue]To[/COLOR] 1000
            [COLOR=darkblue]With[/COLOR] Range("O" & c)
                [COLOR=darkblue]If[/COLOR] .Value = 14 [COLOR=darkblue]Then[/COLOR]
                    [COLOR=darkblue]With[/COLOR] .EntireRow
                        .Copy Destination:=Worksheets("Sheet2").Range("A" & myRow)
                        .Delete Shift:=xlUp
                    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                    c = c - 1
                    myRow = myRow + 1
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        [COLOR=darkblue]Next[/COLOR] c
        
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
 
Upvote 0
Please give this a try:

Code:
[FONT=Franklin Gothic Medium][COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
[COLOR=darkblue]Dim[/COLOR] c       [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wsD     [COLOR=darkblue]As[/COLOR] Worksheet: [COLOR=darkblue]Set[/COLOR] wsD = Worksheets("Sheet1") [COLOR=green]'Data Worksheet.[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wsR     [COLOR=darkblue]As[/COLOR] Worksheet: [COLOR=darkblue]Set[/COLOR] wsR = Worksheets("Sheet2") [COLOR=green]'Results Worksheet[/COLOR]
    [COLOR=darkblue]For[/COLOR] c = 2 [COLOR=darkblue]To[/COLOR] 1002 [COLOR=green]'Loop through 1000 records.[/COLOR]
        [COLOR=darkblue]With[/COLOR] wsD.Range("O" & c)
            [COLOR=darkblue]If[/COLOR] .Value = 14 [COLOR=darkblue]Then[/COLOR] [COLOR=green]'Test for 14.[/COLOR]
                [COLOR=darkblue]With[/COLOR] .EntireRow
                    .Copy Destination:= _
                            wsR.Range("A" & wsR. _
                            Range("A" & Rows.Count).End(xlUp).Row + 1) [COLOR=green]'Copy entire row to wsR.[/COLOR]
                    .Delete Shift:=xlUp [COLOR=green]'Delete record from wsD.[/COLOR]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                c = c - 1 [COLOR=green]'Offset loop when row is deleted.[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]Next[/COLOR] c [COLOR=green]'Loop.[/COLOR]
        
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Using the (xlup) should add the results to the bottom of the record list on sheet2.

I am thinking this one may have potential... :)
 
Upvote 0
Please give this a try:

Code:
[FONT=Franklin Gothic Medium][COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
[COLOR=darkblue]Dim[/COLOR] c       [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wsD     [COLOR=darkblue]As[/COLOR] Worksheet: [COLOR=darkblue]Set[/COLOR] wsD = Worksheets("Sheet1") [COLOR=green]'Data Worksheet.[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wsR     [COLOR=darkblue]As[/COLOR] Worksheet: [COLOR=darkblue]Set[/COLOR] wsR = Worksheets("Sheet2") [COLOR=green]'Results Worksheet[/COLOR]
    [COLOR=darkblue]For[/COLOR] c = 2 [COLOR=darkblue]To[/COLOR] 1002 [COLOR=green]'Loop through 1000 records.[/COLOR]
        [COLOR=darkblue]With[/COLOR] wsD.Range("O" & c)
            [COLOR=darkblue]If[/COLOR] .Value = 14 [COLOR=darkblue]Then[/COLOR] [COLOR=green]'Test for 14.[/COLOR]
                [COLOR=darkblue]With[/COLOR] .EntireRow
                    .Copy Destination:= _
                            wsR.Range("A" & wsR. _
                            Range("A" & Rows.Count).End(xlUp).Row + 1) [COLOR=green]'Copy entire row to wsR.[/COLOR]
                    .Delete Shift:=xlUp [COLOR=green]'Delete record from wsD.[/COLOR]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                c = c - 1 [COLOR=green]'Offset loop when row is deleted.[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]Next[/COLOR] c [COLOR=green]'Loop.[/COLOR]
        
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Using the (xlup) should add the results to the bottom of the record list on sheet2.

I am thinking this one may have potential... :)

First of all many thanks for your willingness to help

As for the result....

I bow before the superior!!!!:cool:

I works just great

Thank u
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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