Dreamteam
New Member
- Joined
- Feb 22, 2018
- Messages
- 28
- Office Version
- 365
- Platform
- Windows
Hi all,
First post and pretty excited...
I'm new to VBA and I'm really just trying to learn it correctly from the start (whatever that actually means). I have a pretty straightforward piece of code that I have written - which works - and that's great. However, I am wondering if this is almost perfect. By this I mean I guess that there are plenty of ways to complete this task but I am keen to know if this way is as good as it can get.
Oops, I better mention what I am trying to do here. I have a list of currency pairs in col A of a worksheet (AUD USD, USD CAD, GBP AUD etc...) and I am creating new worksheets based on these values.
It has taken me quite a while to write as I had a few issues with it - i.e. if I ran it when I had another workbook open then the worksheets would be added to the wrong workbook etc.
I think what I am also keen to find out is the view of using object references as opposed to statements like .select / .activesheet etc
Any advice/comments would be much appreciated because as alluded to above I want to begin the correct way.
Many thanks
Dt
First post and pretty excited...
I'm new to VBA and I'm really just trying to learn it correctly from the start (whatever that actually means). I have a pretty straightforward piece of code that I have written - which works - and that's great. However, I am wondering if this is almost perfect. By this I mean I guess that there are plenty of ways to complete this task but I am keen to know if this way is as good as it can get.
Oops, I better mention what I am trying to do here. I have a list of currency pairs in col A of a worksheet (AUD USD, USD CAD, GBP AUD etc...) and I am creating new worksheets based on these values.
It has taken me quite a while to write as I had a few issues with it - i.e. if I ran it when I had another workbook open then the worksheets would be added to the wrong workbook etc.
I think what I am also keen to find out is the view of using object references as opposed to statements like .select / .activesheet etc
Any advice/comments would be much appreciated because as alluded to above I want to begin the correct way.
Many thanks
Dt
Code:
Sub CreateWorksheetsAddNames()
Dim wb As Workbook
Dim ws As Worksheet
Dim MyCell As Range
Dim MyRange As Range
Set wb = Workbooks("What is Trending xlsm")
Set ws = wb.Worksheets("Data")
Set MyRange = ws.Range("a2")
Set MyRange = ws.Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
wb.Worksheets.Add After:=wb.Worksheets(wb.Worksheets.Count)
wb.Worksheets(wb.Worksheets.Count).Name = MyCell.Value
Next MyCell
End Sub