Looking to get Name value offset to set dynamic sheet names

nutbolt

New Member
Joined
Apr 2, 2021
Messages
17
Office Version
  1. 2019
I have a small table and some code which shouls extract the Generic Category value for each row and then use that Name value to greate new worksheets with.

My table:

Screenshot 2023-01-06 221352.jpg


and my code:
VBA Code:
Sub CommandButton1_Click()

 Dim cProduct As String
 Dim myRange As Range
 Dim sCell As Range
 Dim copySize As Long
 Dim colCount As Integer
 Dim lCount As Integer
   
   
    Set myRange = Range("A3").CurrentRegion
   
    cProduct = myRange.Cells(1, 1)
    Set sCell = myRange.Cells(1, 1)
   
    colCount = myRange.Columns.Count
   
    Dim dWs As Worksheet, cWs As Worksheet, cfRange As Range
   
    Set dWs = ActiveSheet

    lCount = 1
   
    'deleteSheets
   
    Do While Len(cProduct) > 1
        dWs.Activate
        copySize = Range(sCell, sCell.End(xlDown)).Rows.Count
       
        Range(sCell, sCell.Offset(copySize - 1).Offset(, colCount - 1)).Copy
       
        Set cWs = Sheets.Add
       
        cWs.Range("A3").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
                 
       
       
        'code for autofitting columns
       
        cWs.Columns("C:P").EntireColumn.AutoFit
       
       
       
        cWs.Name = Format(cProduct.Value.Offset(, 1)) 'this is where im getting error
        lCount = lCount + 1
       
        Set sCell = sCell.Offset(copySize + 1)
        cProduct = sCell.Value
    Loop


End Sub

So my question are how do I fix this line cWs.Name = Format(cProduct.Value.Offset(, 1)) 'this is where im getting error
and is this the best way to loop through 100k rows?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Alright managed to solve it myself with
VBA Code:
cWs.Name = Format(sCell.Offset(0, 1))
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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