Need Help with this macro

hjason315

New Member
Joined
Jul 11, 2011
Messages
19
Hi all,

This macro was made to find all values 20 steps before everytime it meets the first empty cells(after a bunch of filleds) in Column M. And I would like to copy all these values found and paste them on Column H in order. Can anyone help me simplify this code?

Also, This line/function
"Selection.Copy Destination:=Cells(i, 8)" doesn't really copy and paste, but cut and paste instead. I do not want to cut off any of the cells, can anyone help me to find out why this is happening?


Code:
 Dim i As Long, LastRow As Long
 
    LastRow = Range("B65536").End(xlUp).Row - 200
    If Not LastRow < 1 Then
    For i = 14 To LastRow
    If Not IsEmpty(Cells(i - 1, 13)) And IsEmpty(Cells(i, 13)) Then Cells(i - 20, 13).Select
    Selection.Copy Destination:=Cells(i, 8)
 
    Selection = ""
    Next i
    End If
 
    Columns("H:H").SpecialCells(xlCellTypeBlanks).Delete (xlShiftUp)
 
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
There's no need to Select but it is copying and pasting.
You are removing the values however in your code.

Remove this line:

Selection = ""
 
Upvote 0
Code:
Sub test()
On Error Resume Next
Dim i As Long, LastRow As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row - 200
If LastRow > 0 Then
    For i = 14 To LastRow
        If Cells(i - 1, 13) <> "" And Cells(i, 13) = "" Then Cells(i - 20, 13).Copy Cells(i, 8)
    Next i
End If
Range("H:H").SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
End Sub
 
Upvote 0
There's no need to Select but it is copying and pasting.
You are removing the values however in your code.

Remove this line:

Selection = ""

No, if I remove this line
Selection = ""

Then the macro will paste the value found in Col M all the way down as "i" is increasing.
 
Upvote 0
Code:
Sub test()
On Error Resume Next
Dim i As Long, LastRow As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row - 200
If LastRow > 0 Then
    For i = 14 To LastRow
        If Cells(i - 1, 13) <> "" And Cells(i, 13) = "" Then Cells(i - 20, 13).Copy Cells(i, 8)
    Next i
End If
Range("H:H").SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
End Sub


This code works perfect! Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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