nburaq
Board Regular
- Joined
- Apr 2, 2021
- Messages
- 222
- Office Version
- 365
- 2019
- Platform
- Windows
Hi Gents,
I am quite new to VBA and still busy with improving my codes. I have simple sheet called movies and in the sheet I try to create sheet based on the value in the sheet. However, if the sheet already exists then it will not create any new sheet. Thanks for your lep and comments! Here is the simple sheet and code as well;
Debugging occures at line : Sheets.add,name=filmrating
I am quite new to VBA and still busy with improving my codes. I have simple sheet called movies and in the sheet I try to create sheet based on the value in the sheet. However, if the sheet already exists then it will not create any new sheet. Thanks for your lep and comments! Here is the simple sheet and code as well;
Movies of 2022 | |||
ID | Name | Date | Length |
1 | Avatar | 22/01/2001 | 25 |
2 | Ede | 16/03/2004 | 130 |
3 | Hura | 09/09/2012 | 180 |
4 | Lola | 25/08/2012 | 143 |
5 | Storm | 14/02/1997 | 160 |
6 | Horas | 15/06/1998 | 100 |
7 | Soras | 14/02/1996 | 90 |
8 | Life | 14/02/1997 | 75 |
9 | Terminator | 03/02/2006 | 20 |
10 | Panda | 12/12/2012 | 30 |
11 | Gravitiy | 12/12/2013 | 45 |
VBA Code:
Sub CopyLoop()
Dim FilmLength As Integer
Dim FilmRating As String
Dim Wb As Workbook
Dim Ws As Worksheet
Set Wb = ActiveWorkbook
Worksheets("Movies").Activate
Range("A3").Select
Do Until ActiveCell.Value = ""
FilmLength = ActiveCell.Offset(0, 3).Value
If FilmLength < 100 Then
FilmRating = "Short"
ElseIf FilmLength < 150 Then
FilmRating = "Medium"
Else
FilmRating = "Long"
End If
Range(ActiveCell, ActiveCell.End(xlToRight)).Copy
For Each Ws In Wb.Worksheets
If Ws.Name = FilmRating Then
Worksheets(FilmRating).Activate
Else
Sheets.Add.Name = FilmRating
End If
Next Ws
ActiveCell.PasteSpecial
ActiveCell.Offset(1, 0).Select
Worksheets("Movies").Activate
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Debugging occures at line : Sheets.add,name=filmrating