I am trying to run a report. Basically I use an active x control to initialise a sequence of looking through column B in "MASTER" and creating new sheets for each value that is in column B. This part works fine. What I want to do is for each line i want to copy the data in MASTER to its relevant sheet as per its designation in column B.
This is my code so far it runs but doesn't paste any information:
Any ideas on this would be greatly appreciated
This is my code so far it runs but doesn't paste any information:
Code:
Sub CreateSheetsFromAList()Dim MyCell As Range
Dim MyRange1 As Range
Dim MyRange As Range
Dim WasVisible As Boolean
Dim wsTEMP As Worksheet
Dim wsMASTER As Worksheet
Dim ws As Worksheet
Dim wsRng As Range
Dim i As Long
Dim NextRow As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim LastRow As Long
Application.ScreenUpdating = False 'Speeding up Macro
Application.DisplayAlerts = False 'Turning off alerts
For Each ws In Sheets
If ws.Name <> "MASTER" And ws.Name <> "Template" Then
ws.Delete
End If
Next
Application.DisplayAlerts = True
Set wsTEMP = Sheets("Template") 'Sheet to be copied
WasVisible = (wsTEMP.Visible = xlSheetVisible) 'Checking if sheet is visible
If Not WasVisible Then wsTEMP.Visible = xlSheetVisible 'If NOT visible making the sheet visible
Set wsMASTER = Sheets("MASTER")
Set MyRange = Sheets("MASTER").Range("B3")
Set MyRange1 = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange1
If CreateSheetIf(MyCell.Value) Then 'creates a new worksheet
wsTEMP.Copy Before:=Sheets(wsMASTER)
Sheets(Sheets.Count).Name = MyCell.Value 'renames the new worksheet
End If
wsTEMP.Select
Rows("1:2").Select
Selection.Copy
Sheets(MyCell.Value).Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Sheets(MyCell.Value).Activate
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 1 To LastRow
If Sheets("MASTER").Cells(i, 1).Value = MyCell.Value Then
ActiveSheet.Row.Value.Copy _
Destination:=Sheets(MyCell.Value)
End If
Next i
Next MyCell
wsMASTER.Activate 'Returning to main sheet
If Not WasVisible Then wsTEMP.Visible = xlSheetHidden 'Making template invisible
Application.ScreenUpdating = True 'Returning Screen updating
End Sub
Function CreateSheetIf(strSheetName As String) As Boolean
Dim wsTest As Worksheet
CreateSheetIf = False
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo 0
If wsTest Is Nothing Then
CreateSheetIf = False
Worksheets.Add.Name = strSheetName
End If
End Function
Any ideas on this would be greatly appreciated