Loop trough cell is column, and copy values to new sheet

prasz

New Member
Joined
Sep 12, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
  2. MacOS
Hi, i like to loop trough column d, and copy range a:e to a new sheet where d cell values are equals, with sheet name is d cell value i attached an image.
that would still be useful if the a1:e1 range (the header) is also copyed to the sheets.
 

Attachments

  • sample.jpg
    sample.jpg
    150.4 KB · Views: 8

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
VBA Code:
Sub AddSheets()
    Application.ScreenUpdating = False
    Dim rng As Range, v As Variant, i As Long, srcWS As Worksheet
    Set srcWS = Sheets("cikk20220908")
    v = srcWS.Range("D2", srcWS.Range("D" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                With srcWS
                    .Range("A1").CurrentRegion.AutoFilter 4, v(i, 1)
                    If Not Evaluate("isref('" & v(i, 1) & "'!A1)") Then
                        Sheets.Add(After:=Sheets(Sheets.Count)).Name = v(i, 1)
                        .AutoFilter.Range.Copy Range("A1")
                    End If
                End With
            End If
        Next i
    End With
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
There are also a few non-VBA ways to do this.

1. You could simply use "Advanced Filters", using the filter to new location option, selecting to post the results to another sheet. So you would just have a sheet for each option.

2. In 365, you can make use of the new "Filter" function. So if you set up each sheet with headers, you could then add this dynamic spill formula to it.
So, if you set up a sheet named "EGYA" with headers in row 1, you can put this formula in cell A2 and it will return all the "EGYA" data:
Excel Formula:
=FILTER(cikk20220908!A1:E7,cikk20220908!D1:D7="EGYA")
and repeat the process for the other sheets.
 
Upvote 0
Try:
VBA Code:
Sub AddSheets()
    Application.ScreenUpdating = False
    Dim rng As Range, v As Variant, i As Long, srcWS As Worksheet
    Set srcWS = Sheets("cikk20220908")
    v = srcWS.Range("D2", srcWS.Range("D" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                With srcWS
                    .Range("A1").CurrentRegion.AutoFilter 4, v(i, 1)
                    If Not Evaluate("isref('" & v(i, 1) & "'!A1)") Then
                        Sheets.Add(After:=Sheets(Sheets.Count)).Name = v(i, 1)
                        .AutoFilter.Range.Copy Range("A1")
                    End If
                End With
            End If
        Next i
    End With
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
Excellent, thank you very mutch
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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