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
 
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.

You're on the money mate,

Comes up with a syntax error below?

Code:
Range("H2").Formula = "=TEXT(IF(VLOOKUP($A2,DataSheet!$A$1:AB$700,25,FALSE)="""","""Currently Working""",VLOOKUP($A2,DataSheet!A$1:AB$700,25,FALSE)),"""dd mmmm yyyy""")"
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
What is the formula as it appears in H2 in the spreadsheet?
 
Upvote 0
What is the formula as it appears in H2 in the spreadsheet?
Code:
=TEXT(IF(VLOOKUP($A2,DataSheet!$A$1:AB$700,25,FALSE)="","Currently Working",VLOOKUP($A2,DataSheet!A$1:AB$700,25,FALSE)),"dd mmmm yyyy")
 
Upvote 0
Probably
VBA Code:
Range("H2:H52").Formula = "=TEXT(IF(VLOOKUP($A2,DataSheet!$A$1:AB$700,25,FALSE)="""",""Currently Working"",VLOOKUP($A2,DataSheet!A$1:AB$700,25,FALSE)),""dd mmmm yyyy"")"
 
Upvote 0
I run it and get no error
VBA Code:
Sub DDHDH()
Range("H2:H52").Formula = "=TEXT(IF(VLOOKUP($A2,DataSheet!$A$1:AB$700,25,FALSE)="""",""Currently Working"",VLOOKUP($A2,DataSheet!A$1:AB$700,25,FALSE)),""dd mmmm yyyy"")"
End Sub
 
Upvote 0
I run it and get no error
VBA Code:
Sub DDHDH()
Range("H2:H52").Formula = "=TEXT(IF(VLOOKUP($A2,DataSheet!$A$1:AB$700,25,FALSE)="""",""Currently Working"",VLOOKUP($A2,DataSheet!A$1:AB$700,25,FALSE)),""dd mmmm yyyy"")"
End Sub
Beautiful mate, if I have any other errors am I okay to PM you?
 
Upvote 0
if I have any other errors am I okay to PM you?

You're welcome but no if you have any questions then ask them in a new thread or you run foul of rule 4 ...

Rule #4:

Members should not use the Private Message (Conversations) system to request specific assistance. All members are volunteers, contributing their time and expertise where and when they can, and such requests may be deemed harassment (see Rule #1).


Do not invite another member to take the question off the forum (i.e. do not suggest that they post to a different forum, do not suggest that they email or private message you the problem, do not simply post a link to another forum, unless it is to a specific, relevant, thread). This applies equally to members asking or answering questions.
 
Upvote 0
You're welcome but no if you have any questions then ask them in a new thread or you run foul of rule 4 ...

Hi Mark,

The code you gave me stopped copying data to worksheet "TBD_Manually", really got me baffled .. Any idea?
VBA Code:
Sub Sort_Data()
'
' Sort_Data Macro
' Sorts data by duplicates and leave reasons as Dismissals
'
Worksheets("ADO").Unprotect Password:="lol"

      With Sheets("ADO")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:P50" & .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 Sheets("ADO")
        With .Range("A1:P50" & .Range("B" & Rows.Count).End(xlUp).Row)
            .AutoFilter 2, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A2" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
    End With
    
    Worksheets("ADO").Protect Password:="lol"
    
End Sub
 
Upvote 0
To start with remove the extra With Sheets("ADO") that you have added (in red below). Then test again.

Rich (BB code):
Sub Sort_Data()
'
' Sort_Data Macro
' Sorts data by duplicates and leave reasons as Dismissals
'
Worksheets("ADO").Unprotect Password:="lol"

      With Sheets("ADO")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:P50" & .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 Sheets("ADO")
        With .Range("A1:P50" & .Range("B" & Rows.Count).End(xlUp).Row)
            .AutoFilter 2, RGB(255, 0, 0), xlFilterCellColor
            .Offset(1).Copy Sheets("TBD_Manually").Range("A2" & Rows.Count).End(xlUp)(2)
            .Offset(1).Delete
            Sheets("ADO").ShowAllData
        End With
    End With
    
    Worksheets("ADO").Protect Password:="lol"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

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