VLOOKUP type question to copy part of a row once the value is hound in a range

Phantasm

Board Regular
Joined
Nov 2, 2007
Messages
58
So I have values such as "SAW", "BP", "MILL", "LATHE" in the range of G2:O250 in WORKSHEET1.

What I want to do is create a new worksheet tab called "SAW" & have it import the any row from A:O on WORKSHEET1 that has that has the cell value of "SAW"in it. This will basically show me all jobs olny that have a SAW operation. I will do the same on other tabs for the other values.

Any help is appreaciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
so i have values such as "saw", "bp", "mill", "lathe" in the range of g2:o250 in worksheet1.

What i want to do is create a new worksheet tab called "saw" & have it import the any row from a:o on worksheet1 that has that has the cell value of "saw"in it. This will basically show me all jobs olny that have a saw operation. I will do the same on other tabs for the other values.

Any help is appreaciated.

bump
 
Upvote 0
Does each cell in range G2:O250 contain either a single value or else is blank?
Is it always 250 rows or do you simply want all rows?
Can each operation appear in any column (G:O) or does "SAW" always appear in same column etc?
 
Upvote 0
Further to Yongle's questions, can saw appear multiple times in the same row?
 
Upvote 0
Further to Yongle's questions, can saw appear multiple times in the same row?


Does each cell in range G2:O250 contain either a single value or else is blank? - Yes, there are about 15 different operations that I use and all are single operations (no SAW/WELD). Also, there are empty cells.

Is it always 250 rows or do you simply want all rows? - I dont see it going more than 250, but using all rows would do no harm.

Can each operation appear in any column (G:O) or does "SAW" always appear in same column etc? - Each operation can appear in any of the columns & they may appear multiple times in the same row.

Thanks for the help guys!
 
Upvote 0
Try this and let me know

- place code below in the SHEET module
(right click on sheet tab \ view code \ paste in window on right)
- and then run CreateSheets

Place at the top -before all other procedures (makes variables available to all procedures in same module)
Code:
Option Explicit
Private Ops As Collection, Op As Variant
Private DataRange As Range, OpsRange As Range, Cel As Range, MatchRange As Range
Private LastRow As Long, r As Long
Private SheetName As String
Private ws As Worksheet

Main procedure calls other subs
Code:
Sub CreateSheets()
    Optimise True
    SetRanges
    CreateUniqueList
    AddSheets
    CopyRows
    Optimise False
End Sub

Other subs
Code:
Private Sub Optimise(OnOff As Boolean)
    With Application
        Select Case OnOff
            Case True:      .ScreenUpdating = False:    .Calculation = xlCalculationManual
            Case False:     .ScreenUpdating = True:     .Calculation = xlCalculationAutomatic
        End Select
    End With
End Sub

Private Sub SetRanges()
    Set DataRange = Me.Cells(1).CurrentRegion
    With DataRange
        Set OpsRange = .Offset(1, 6).Resize(.Rows.Count - 1, .Columns.Count - 6)
        LastRow = .Rows.Count
    End With
End Sub

Private Sub CreateUniqueList()
    Set Ops = New Collection
    On Error Resume Next        'required to get unique list this way
    For Each Cel In OpsRange
        If Cel <> vbNullString Then Ops.Add Cel, Cel
    Next Cel
End Sub

Private Sub AddSheets()         'deletes old one first if it exists with same name
    Application.DisplayAlerts = False
    On Error Resume Next        'sheet name may be invalid or sheet may not exist
    For Each Op In Ops
        SheetName = Op
        Sheets(SheetName).Delete
        Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
        ws.Name = SheetName
        Me.Rows(1).Copy ws.Cells(1)
    Next
    Application.DisplayAlerts = True
End Sub

Private Sub CopyRows()
    On Error Resume Next
    For Each Op In Ops
        SheetName = Op
        Set ws = Sheets(SheetName)
        For r = 2 To LastRow
            Set MatchRange = OpsRange.Resize(1).Offset(r - 2)
            If WorksheetFunction.CountIf(MatchRange, SheetName) > 0 Then
                Me.Rows(r).Copy ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End If
        Next r
    Next Op
End Sub

I avoided filtering - with only 250 rows copying one row at a time should be very quick
 
Last edited:
Upvote 0
Try this and let me know

- place code below in the SHEET module
(right click on sheet tab \ view code \ paste in window on right)
- and then run CreateSheets

Place at the top -before all other procedures (makes variables available to all procedures in same module)
Code:
Option Explicit
Private Ops As Collection, Op As Variant
Private DataRange As Range, OpsRange As Range, Cel As Range, MatchRange As Range
Private LastRow As Long, r As Long
Private SheetName As String
Private ws As Worksheet

Main procedure calls other subs
Code:
Sub CreateSheets()
    Optimise True
    SetRanges
    CreateUniqueList
    AddSheets
    CopyRows
    Optimise False
End Sub

Other subs
Code:
Private Sub Optimise(OnOff As Boolean)
    With Application
        Select Case OnOff
            Case True:      .ScreenUpdating = False:    .Calculation = xlCalculationManual
            Case False:     .ScreenUpdating = True:     .Calculation = xlCalculationAutomatic
        End Select
    End With
End Sub

Private Sub SetRanges()
    Set DataRange = Me.Cells(1).CurrentRegion
    With DataRange
        Set OpsRange = .Offset(1, 6).Resize(.Rows.Count - 1, .Columns.Count - 6)
        LastRow = .Rows.Count
    End With
End Sub

Private Sub CreateUniqueList()
    Set Ops = New Collection
    On Error Resume Next        'required to get unique list this way
    For Each Cel In OpsRange
        If Cel <> vbNullString Then Ops.Add Cel, Cel
    Next Cel
End Sub

Private Sub AddSheets()         'deletes old one first if it exists with same name
    Application.DisplayAlerts = False
    On Error Resume Next        'sheet name may be invalid or sheet may not exist
    For Each Op In Ops
        SheetName = Op
        Sheets(SheetName).Delete
        Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
        ws.Name = SheetName
        Me.Rows(1).Copy ws.Cells(1)
    Next
    Application.DisplayAlerts = True
End Sub

Private Sub CopyRows()
    On Error Resume Next
    For Each Op In Ops
        SheetName = Op
        Set ws = Sheets(SheetName)
        For r = 2 To LastRow
            Set MatchRange = OpsRange.Resize(1).Offset(r - 2)
            If WorksheetFunction.CountIf(MatchRange, SheetName) > 0 Then
                Me.Rows(r).Copy ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End If
        Next r
    Next Op
End Sub

I avoided filtering - with only 250 rows copying one row at a time should be very quick

Thank you for this. I will get back to you tomorow on if it works, right now Im trying to slay bigger dragons...lol.
 
Upvote 0
If you have any follow up question, please post them on the thread.
Here are the answers to your PM questions

Basicly, it seems like its grabbing too many columns because its looking past column "O"
.
Try replacing
Code:
Set OpsRange = .Offset(1, 6).Resize(.Rows.Count - 1, .Columns.Count - 6)
With
Code:
Set OpsRange = .Offset(1, 6).Resize(.Rows.Count - 1, 9)

Also, is there any way that I can get it to keep my formatting of the worksheet it grabs from (column sizes ect..)
Try adding
Code:
Me.Rows(1).Copy
ws.Cells(1).PasteSpecial (xlPasteColumnWidths)
[B]
After this line[/B]
Code:
Me.Rows(1).Copy ws.Cells(1)

And can I have the first row sortable like on the initial worksheet...
What does this mean?
 
Last edited:
Upvote 0
And can I have the first row sortable like on the initial worksheet...

To add Data Filter to each new sheet

add this line
Code:
ws.Cells(1, 1).AutoFilter
after this line
Code:
Me.Rows(r).Copy ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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