ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Tom Ogilvys Code (https://www.excelbanter.com/excel-programming/281398-tom-ogilvys-code.html)

Todd Huttenstine[_2_]

Tom Ogilvys Code
 
Hey Tom,

Its supposed to be set to A5:A53. Below is the code I am
currently using:

Private Sub CommandButton2_Click()

Dim rng As Range, rng1 As Range
Dim cell As Range, cell1 As Range
Dim rngStat As Range
Dim res As Variant

With Worksheets("Team Data")
Set rng = .Range("A5:A53")
Set rngStat = .Range("B4:U4")
End With

With Worksheets("Template")
Set rng1 = .Range("A5:A16")
End With

For Each cell In rng
For Each cell1 In rng1
res = Application.Match(cell1, rngStat, 0)
If Not IsError(res) Then
cell1.Offset(0, 1).Value = rngStat(cell.Row - 3,
res).Value
End If
Next
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(cell.Value).Delete
Application.DisplayAlerts = True
On Error GoTo 0
rng1.Parent.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.name = cell.Value
rng1.Offset(0, 1).ClearContents
Next
End Sub

heres the problem, It works like I need. The only thing
it does wrong is after it creates the last sheet based
on the last persons name in range A5:A53, it creates
another sheet called Template(2) and then I get a runtime
error. I click Debug and then it brings up the screen
with the 2nd to the last line of the code highlighted(that
is ActiveSheet.name = cell.Value. This line is in
yellow. What I would like is that when the code sees
there are no more names in Range A5:A53, the code quits
without error and does not create a page at the end called
Template(2).

Thank you

Todd Huttenstine

Tom Ogilvy

Tom Ogilvys Code
 
I couldn't reproduce the problem. I had names in A5:A53 and it worked fine.

Nonetheless, I have put in some code that exits if it hits a blank cell in
A5:A53:

Public Sub CommandButton2_Click()

Dim rng As Range, rng1 As Range
Dim cell As Range, cell1 As Range
Dim rngStat As Range
Dim res As Variant

With Worksheets("Team Data")
Set rng = .Range("A5:A53")
Set rngStat = .Range("B4:U4")
End With

With Worksheets("Template")
Set rng1 = .Range("A5:A16")
End With

For Each cell In rng
If IsEmpty(cell) Then Exit Sub
For Each cell1 In rng1
res = Application.Match(cell1, rngStat, 0)
If Not IsError(res) Then
cell1.Offset(0, 1).Value = rngStat(cell.Row - 3, _
res).Value
End If
Next
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(cell.Value).Delete
Application.DisplayAlerts = True
On Error GoTo 0
rng1.Parent.Copy after:=Worksheets(Worksheets.Count)
On Error Resume Next
ActiveSheet.Name = cell.Value
If Err < 0 Then
MsgBox "=" & cell.Value & "<= is an illegal name"
Err.Clear
End If
On Error GoTo 0
rng1.Offset(0, 1).ClearContents
Next
End Sub


--
Regards,
Tom Ogilvy


Todd Huttenstine wrote in message
...
Hey Tom,

Its supposed to be set to A5:A53. Below is the code I am
currently using:

Private Sub CommandButton2_Click()

Dim rng As Range, rng1 As Range
Dim cell As Range, cell1 As Range
Dim rngStat As Range
Dim res As Variant

With Worksheets("Team Data")
Set rng = .Range("A5:A53")
Set rngStat = .Range("B4:U4")
End With

With Worksheets("Template")
Set rng1 = .Range("A5:A16")
End With

For Each cell In rng
For Each cell1 In rng1
res = Application.Match(cell1, rngStat, 0)
If Not IsError(res) Then
cell1.Offset(0, 1).Value = rngStat(cell.Row - 3,
res).Value
End If
Next
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(cell.Value).Delete
Application.DisplayAlerts = True
On Error GoTo 0
rng1.Parent.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.name = cell.Value
rng1.Offset(0, 1).ClearContents
Next
End Sub

heres the problem, It works like I need. The only thing
it does wrong is after it creates the last sheet based
on the last persons name in range A5:A53, it creates
another sheet called Template(2) and then I get a runtime
error. I click Debug and then it brings up the screen
with the 2nd to the last line of the code highlighted(that
is ActiveSheet.name = cell.Value. This line is in
yellow. What I would like is that when the code sees
there are no more names in Range A5:A53, the code quits
without error and does not create a page at the end called
Template(2).

Thank you

Todd Huttenstine




Kevin Beckham

Tom Ogilvys Code
 
Todd

After the line
For Each cell In rng
insert the following line
IF cell = "" then Exit For
This will stop the code running as soon as an empty cell
is reached

Kevin Beckham

-----Original Message-----
Hey Tom,

Its supposed to be set to A5:A53. Below is the code I am
currently using:

Private Sub CommandButton2_Click()

Dim rng As Range, rng1 As Range
Dim cell As Range, cell1 As Range
Dim rngStat As Range
Dim res As Variant

With Worksheets("Team Data")
Set rng = .Range("A5:A53")
Set rngStat = .Range("B4:U4")
End With

With Worksheets("Template")
Set rng1 = .Range("A5:A16")
End With

For Each cell In rng
For Each cell1 In rng1
res = Application.Match(cell1, rngStat, 0)
If Not IsError(res) Then
cell1.Offset(0, 1).Value = rngStat(cell.Row - 3,
res).Value
End If
Next
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(cell.Value).Delete
Application.DisplayAlerts = True
On Error GoTo 0
rng1.Parent.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.name = cell.Value
rng1.Offset(0, 1).ClearContents
Next
End Sub

heres the problem, It works like I need. The only thing
it does wrong is after it creates the last sheet based
on the last persons name in range A5:A53, it creates
another sheet called Template(2) and then I get a runtime
error. I click Debug and then it brings up the screen
with the 2nd to the last line of the code highlighted

(that
is ActiveSheet.name = cell.Value. This line is in
yellow. What I would like is that when the code sees
there are no more names in Range A5:A53, the code quits
without error and does not create a page at the end

called
Template(2).

Thank you

Todd Huttenstine
.


Todd Huttenstine[_2_]

Tom Ogilvys Code
 
Thank you both very much.


-----Original Message-----
Hey Tom,

Its supposed to be set to A5:A53. Below is the code I am
currently using:

Private Sub CommandButton2_Click()

Dim rng As Range, rng1 As Range
Dim cell As Range, cell1 As Range
Dim rngStat As Range
Dim res As Variant

With Worksheets("Team Data")
Set rng = .Range("A5:A53")
Set rngStat = .Range("B4:U4")
End With

With Worksheets("Template")
Set rng1 = .Range("A5:A16")
End With

For Each cell In rng
For Each cell1 In rng1
res = Application.Match(cell1, rngStat, 0)
If Not IsError(res) Then
cell1.Offset(0, 1).Value = rngStat(cell.Row - 3,
res).Value
End If
Next
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(cell.Value).Delete
Application.DisplayAlerts = True
On Error GoTo 0
rng1.Parent.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.name = cell.Value
rng1.Offset(0, 1).ClearContents
Next
End Sub

heres the problem, It works like I need. The only thing
it does wrong is after it creates the last sheet based
on the last persons name in range A5:A53, it creates
another sheet called Template(2) and then I get a runtime
error. I click Debug and then it brings up the screen
with the 2nd to the last line of the code highlighted

(that
is ActiveSheet.name = cell.Value. This line is in
yellow. What I would like is that when the code sees
there are no more names in Range A5:A53, the code quits
without error and does not create a page at the end

called
Template(2).

Thank you

Todd Huttenstine
.



All times are GMT +1. The time now is 11:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com