Copying cell ranges instead row?

Mike2502

Board Regular
Joined
Jan 19, 2020
Messages
143
Office Version
  1. 2010
Hey All,

I'm trying to select the data from column A till P instead of copying, pasting and deleting the whole rows? please see code below and thanks in advance :)!

VBA Code:
Sub Sort_Data()

    Cells.Select
    ActiveWorkbook.Worksheets("ADO").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("ADO").Sort.SortFields.Add(Range("A1:A100"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
        , 0)
    ActiveWorkbook.Worksheets("ADO").Sort.SortFields.Add(Range("B1:B100"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
        , 0)
    With ActiveWorkbook.Worksheets("ADO").Sort
        .SetRange Range("A1:N100")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


    With Sheets("ADO")
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A1:B" & .Range("A" & Rows.Count).End(xlUp).Row).AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
    .AutoFilter.Range.Offset(1).EntireRow.Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
    .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row).AutoFilter 2, RGB(255, 0, 0), xlFilterCellColor
    .AutoFilter.Range.Offset(1).EntireRow.Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
  End With
 
   Range("A2").Select
  
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Untested but can't you just do something like the below

VBA Code:
    With Sheets("ADO")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:P" & .Range("A" & Rows.Count).End(xlUp).Row)
            .AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
        With .Range("A1:P" & .Range("B" & Rows.Count).End(xlUp).Row)
            .AutoFilter 2, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
    End With
 
Upvote 0
Untested but can't you just do something like the below

VBA Code:
    With Sheets("ADO")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:P" & .Range("A" & Rows.Count).End(xlUp).Row)
            .AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
        With .Range("A1:P" & .Range("B" & Rows.Count).End(xlUp).Row)
            .AutoFilter 2, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
    End With

No mate, sorry I didn't explain it clear - So the code I posted selects cells that are the color red and copy pastes the entire row to 'TBD_Manually', however instead of the whole row I want to limit it up to Column A to P?

Hope that makes sense
 
Upvote 0
Always better to test first before replying ;)



Doesn't bring my form control buttons over on my workbook. You can try changing the buttons properties to don't move or size with cells.

Mark mate, you are great, cheers mate..

Bit off topic right, but I want to only allow Columns A and N for other users to input data into but other cells are associated with my macro and codes won't work... is there an alternative mate?
 
Upvote 0
Lock all the cells except those columns, protect the sheet, Put code at the start of the macro to unprotect the sheet and code at the end of the macro to protect the sheet again.
 
Upvote 0
Lock all the cells except those columns, protect the sheet, Put code at the start of the macro to unprotect the sheet and code at the end of the macro to protect the sheet again.
Thanks mate,

.. I have an absolute vLookup for 50 rows but then lets say 5 rows are deleted and pasted to the next worksheet the last 5 rows don't have no vlookup coding no more.

Any way to keep the vlookups fixed in their cells even when rows are copied, deleted and pasted over without unlocking the sheet and fill all?
 
Upvote 0
Are you saying that you have 50 rows of data with 50 vlookups, you delete 5 rows, you are left with 45 rows with data and the next 5 blank rows have no vlookups in them?

If yes then that is what should happen. If you want it to always have 50 vlookups after deleting then reapply the formula to the range in the VBA.

If it is anything else then please re-explain what is happening.
 
Upvote 0

Forum statistics

Threads
1,215,402
Messages
6,124,708
Members
449,182
Latest member
mrlanc20

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