So im supposed code a sub that creates a sheet for each unique category found in column b in the excel spread sheet I have. In these sheets i need to include the name and price of the product (in that category) in column A and B respectively. I should note that the product names are in column A and prices in C on my excel sheet.
There are other variables to this such as putting the product/price four rows down and titling each sheet / column but I wont bother anyone with this as I appreciate the fact you have read this far. I am stumped, and any help is appreciated. Thanks, heres what i have thus far.
There are other variables to this such as putting the product/price four rows down and titling each sheet / column but I wont bother anyone with this as I appreciate the fact you have read this far. I am stumped, and any help is appreciated. Thanks, heres what i have thus far.
Code:
Option Explicit
Sub Categorize()
Dim nCategories As Integer
Dim rowOffset As Integer
Dim category As String
Dim ws As Worksheet
Dim isNewCategory As Boolean
Dim product As String
Dim price As Currency
Dim colAWidth As Single
Dim colBWidth As Single
Dim topCell As Range
Dim newSheet As Worksheet
Set topCell = Worksheets("AllProducts").Range("A3")
nCategories = 0
rowOffset = 1
' Capture the column widths of columns A and C (to be used for columns A and B in new sheets).
With Worksheets("AllProducts").Columns("A")
.ColumnWidth = colAWidth
End With
With Worksheets("AllProducts").Columns("A")
.ColumnWidth = colBWidth
End With
' Go through all products until encountering a blank cell.
Do Until topCell.Offset(rowOffset, 0).Value = ""
' Capture the information on this product.
With topCell
product =
category =
price =
End With
' Check whether this is a new category (i.e., if there is already a sheet with its name).
isNewCategory = True
For Each ws In Worksheets
Next
' If it's a new category, add a new sheet and enter labels, as well as the first row of data.
If isNewCategory Then
nCategories = nCategories + 1
Worksheets.Add After:=Worksheets(nCategories)
Set newSheet = ActiveSheet
With newSheet
End With
' Otherwise, just add the next row of data.
Else
With Worksheets(category).Range("A3").End(xlDown)
End With
End If
' Get ready for the next product.
rowOffset = rowOffset + 1
Loop
' Activate the AllProducts sheet.
Worksheets("AllProducts").Activate
End Sub