VBA Help

Kyosti

Board Regular
Joined
Jun 2, 2008
Messages
84
I have tried to adjust a VBA Code that was built for me by @Fluff and I thought I was close to getting it to work, but I was mistaken.

Sub Kyosti()
Dim Cl As Range
Dim Ws As Worksheet
Dim Ky As Variant

Set Ws = Sheets("Data")
With CreateObject("scripting.dictionary")
For Each Cl In Ws.Range("O2", Ws.Range("O" & Rows.Count).End(xlUp))
.Item(Cl.Value) = Empty
Next Cl
For Each Ky In .Keys
Ws.Range("A1:O1").AutoFilter 15, Ky
Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
Ws.AutoFilter.Range.SpecialCells(xlVisible).EntireRow.Copy Range("A1")
Next Ky
Ws.AutoFilterMode = False
End With
End Sub

The original code was written for a different file but I am attempting to use the same logic on the file outlined below



Agent #Agency NameAgent First nameAgent Last nameAgency Location AddressAgency Location CityAgency Location CodeAgency Location StateAgency Location Zip CodeAgency Termination DateAgency StatusNew OwnerSale DateEntry DateSale TypeRegionDaysPolicy Count
NJ Region
NY Region
FL Region

<colgroup><col width="64" span="18" style="width:48pt"> </colgroup><tbody>
</tbody>


The new logic would essentially create a new tab for every region with the filtered information. I made adjustments to the original code to match the new file, but I get a debug at:

Sheets.Add(, Sheets(Sheets.Count)).Name = Ky

Can anyone help me understand the language or what it is doing so I can adjust accordingly!? Any help is greatly appreciated.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
Is the region column O or column P?
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
2,920
Code:
Option Explicit


Sub Kyosti()
Dim Cl As Range
Dim Ws As Worksheet
Dim Ky As Variant


Set Ws = Sheets("Sheet1")


With CreateObject("scripting.dictionary")


    For Each Cl In Ws.Range("P2", Ws.Range("P" & Rows.Count).End(xlUp))
        .Item(Cl.Value) = Empty
    Next Cl
    
    For Each Ky In .Keys
        Ws.Range("A1:R1").AutoFilter 16, Ky
        Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
        Ws.AutoFilter.Range.SpecialCells(xlVisible).EntireRow.Copy Range("A1")
    Next Ky
    
    Ws.AutoFilterMode = False
    
End With
End Sub
 

Kyosti

Board Regular
Joined
Jun 2, 2008
Messages
84
I swear I tried that code too, but I must have been off somewhere! Thank you so much @Logit, however I am still getting an error on the same line. But it did create half of the tabs.
 
Last edited:

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
2,920
.
The edits in the macro function as expected here. Something else going on with your workbook ?

Is there additional code were aren't aware of ?
 

Kyosti

Board Regular
Joined
Jun 2, 2008
Messages
84
Correction the new error is in this line

Ws.Range("A1:R1").AutoFilter 16, Ky
 

Kyosti

Board Regular
Joined
Jun 2, 2008
Messages
84
The file has several different macros, but they are not connected to each other.
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
2,920
.
The only thing I can suggest is to :

Quit Excel.

Shut down your computer.

Turn it back on.

Try the workbook again.


I have found here .... oftentimes when an error has appeared more than once on a workbook I am building, the above steps are the only means of
clearing the cache or memory. I've edited the code and know it is correct but still receiving errors. When re-booting computer the error goes away.
 

Forum statistics

Threads
1,081,989
Messages
5,362,581
Members
400,683
Latest member
LogChief

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top