PDA

View Full Version : Best Approach to Do This Report



jazz2409
01-06-2020, 02:12 AM
I am creating a reporting file where I want to add new sheets and add tables in these new sheets, using vba. As shown in the image below, there are two column Main Category and Sub Category. I want to create new sheet for every Main Category and add tables for every Sub Category based on the sheet it belongs to. Additionally I may add new entries to Main Category and Sub Category, the vba code should add sheet and tables for those as well.

25751

So far I am able to add the new sheets , but couldn't add the tables , This is what I have:


Sub CreateSheetsFromAList()
Dim MyCell As Range, myRange As Range
Dim MyCell1 As Range, myRange1 As Range
Dim WSname As String

Sheet1.Select
Range("A2").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Set myRange = Selection
Application.ScreenUpdating = False

For Each MyCell In myRange
If Len(MyCell.Text) > 0 Then
'Check if sheet exists
If Not SheetExists(MyCell.Value) Then

'run new reports code until before Else

Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet

WSname = MyCell.Value 'stores newly created sheetname to a string variable

'filters consolidated sheet based on newly created sheetname
Sheet3.Select
Range("A:T").AutoFilter
Range("D1").Select
Range("D1").AutoFilter Field:=4, Criteria1:=WSname, Operator:=xlFilterValues

Range("A1:U1").Select
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:U" & lastRow).Select
Selection.Copy 'copies filtered data

'search and activate WSname
ChooseSheet WSname

Range("AH2").Select
ActiveCell.PasteSpecial xlPasteValues

Range("AJ:AJ").Select
Selection.NumberFormat = "hh:mm"
Range("B2").Select
End If
End If

Next MyCell

End Sub

Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet

If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing
End Function

Public Sub ChooseSheet(ByVal SheetName As String)
Sheets(SheetName).Select End Sub

The result should look like this

25752

What is the best approach in getting the above result? Please help

p45cal
01-06-2020, 04:17 AM
I dare you, attach a workbook with just 2 sheets and this code, then we

can see what columns and rows things are in
can be sure what's in those cells showing times
don't have to reproduce your set up (wrongly probably) so we can experiment

p45cal
01-06-2020, 04:47 AM
Oh groan.
You've cross posted this at multiple sites without providing links to them.
Have a look at http://www.excelguru.ca/content.php?184
Provide links at all sites to all cross posts and I'll revisit this thread.

jazz2409
01-06-2020, 04:53 AM
Hi, sorry I'm unaware of this. I'll just delete the post from the other site:(:(

Here's the sample workbook https://drive.google.com/file/d/16Im1bqapSz7UDuIBFdF58pnllWz_9S8Q/view?usp=sharing

jazz2409
01-06-2020, 04:56 AM
https://stackoverflow.com/questions/59580187/adding-tables-using-excel-vba/59581436#59581436
https://www.mrexcel.com/board/threads/best-approach-to-do-this-report.1119627/
https://www.reddit.com/r/excel/comments/ekrz85/adding_tables_using_excel_vba/

Paul_Hossler
01-06-2020, 07:25 AM
Welcome to the forum - please take a minute to look at the FAQ in my signature

1. You should be able to use Go Advanced and the paper clip icon to attach a file to a VBAx post

2. Nothing wrong with posting a question to multiple sites, but all sites have established an 'etiquette' for this


http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

https://www.excelguru.ca/content.php?184

p45cal
01-06-2020, 10:44 AM
This thread is showing as Solved; is it?

jazz2409
01-06-2020, 03:15 PM
Hi, I had to mark it as solved because my project manager changed the requirement.. I opened a new question..

Paul_Hossler
01-07-2020, 08:28 AM
Hi, I had to mark it as solved because my project manager changed the requirement.. I opened a new question..

That's what Project Managers do :banghead::banghead::banghead:

jazz2409
01-07-2020, 08:32 AM
If somebody can please shed a light on my question I'll appreciate it I've been working on it since before the holidays and nothing's working

SamT
01-07-2020, 12:37 PM
I opened a new question..

If somebody can please shed a light on my question
What question? Where? This thread is solved.