X
XR8 Sprintless
Hi,
I'm currently trying to automate the addition of worksheets into a
spreadsheet and have a formula
=INDEX(Tables!$E$3:$E$28,$Q$3)
Which is located on the Home sheet and references the tables index and
returns the value dependent on the value in Q3 on the home sheet for the
sheet where the results are in row E of the tables sheet. There a
multiple sheets which have a table of results in the tables sheet and if
I need the result from another sheet the formula might be
=INDEX(Tables!$J$3:$J$28,$Q$3) for the sheet where the results are in
row J of Tables.
In trying to automate this i tried the following but it fails at the
activecell.formula = "INDEX(Tables!...) entry.
I'm only just starting to learn VBA so my coding is messy!
Is it possible to do this at all?
Here is the full macro so far.
Sub newplayer()
'
' New Player Macro
'
'
Dim sname As String
'Create new sheet with name of contestant unhide then hide the template
sheet
sname = InputBox(Prompt:="Enter contestants name")
Sheets("Sheet Template").Visible = True
Sheets("Sheet Template").Copy After:=Worksheets(Worksheets.Count)
ActiveWindow.ActiveSheet.Name = sname
Sheets("Sheet Template").Visible = False
'Copy the sheet name to Cell A1
Range("A1").Select
ActiveCell.FormulaR1C1 = sname
'Goto the tables sheet and find the next blank column after row E
Sheets("Tables").Select
Dim r As Range
If [F2].Formula = "" Then
Set r = [F2]
Else
Set r = Range("E2").End(xlToRight).Offset(0, 1)
End If
'Put the sheet name in the first column the copy from the template
row E then fill the next 26 rows with the formula
r.Select
ActiveCell.Value = sname
Range("E3").Select
Selection.Copy
r(2).Select
ActiveSheet.Paste
r(2).Select
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
'Copy the formula from E29 to the column then add a formula to copy
results from the spreadsheet to the table and fill the next 26 rows
Range("e29").Select
Selection.Copy
r(28).Select
ActiveSheet.Paste
r(30).Select
ActiveCell.Value = sname
r(31).Select
ActiveCell.Formula = "='" & sname & "'!N3"
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
'Copy another set of 26 results from the table
r(58).Select
ActiveCell.Value = sname
r(59).Select
ActiveCell.Formula = "='" & sname & "'!O3"
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
Range("e86").Select
Selection.Copy
r(85).Select
ActiveSheet.Paste
'get rid of the Templates column on the Tables sheet so it doesn't
figure in the results as not needed once first player entered
If Range("e2") = "Template" Then
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Else
End If
'Go to the home sheet where we sort the results adding each players
weekly result from column r2 onwards
Sheets("Home").Select
Dim z As Range
If [R2].Formula = "" Then
Set z = [R2]
Else
Set z = Range("Q2").End(xlToRight).Offset(0, 1)
End If
z.Select
'Label the column
ActiveCell.Value = sname
'get the result from the tables column depending on the value in Q3
on the home sheet
z(2).Select
'This line is where it fails. Everything works perfectly up to here....
ActiveCell.Formula = "=INDEX(Tables!r(2):r(27),$Q$3)"
z(3).Select
ActiveCell.Value = "=INDEX(Tables!r(59):r(84),$Q$3)"
z(4).Select
ActiveCell.Value = "=Tables!r(28)"
z(5).Select
ActiveCell.Value = "=Tables!r(85)"
End Sub
I'm currently trying to automate the addition of worksheets into a
spreadsheet and have a formula
=INDEX(Tables!$E$3:$E$28,$Q$3)
Which is located on the Home sheet and references the tables index and
returns the value dependent on the value in Q3 on the home sheet for the
sheet where the results are in row E of the tables sheet. There a
multiple sheets which have a table of results in the tables sheet and if
I need the result from another sheet the formula might be
=INDEX(Tables!$J$3:$J$28,$Q$3) for the sheet where the results are in
row J of Tables.
In trying to automate this i tried the following but it fails at the
activecell.formula = "INDEX(Tables!...) entry.
I'm only just starting to learn VBA so my coding is messy!
Is it possible to do this at all?
Here is the full macro so far.
Sub newplayer()
'
' New Player Macro
'
'
Dim sname As String
'Create new sheet with name of contestant unhide then hide the template
sheet
sname = InputBox(Prompt:="Enter contestants name")
Sheets("Sheet Template").Visible = True
Sheets("Sheet Template").Copy After:=Worksheets(Worksheets.Count)
ActiveWindow.ActiveSheet.Name = sname
Sheets("Sheet Template").Visible = False
'Copy the sheet name to Cell A1
Range("A1").Select
ActiveCell.FormulaR1C1 = sname
'Goto the tables sheet and find the next blank column after row E
Sheets("Tables").Select
Dim r As Range
If [F2].Formula = "" Then
Set r = [F2]
Else
Set r = Range("E2").End(xlToRight).Offset(0, 1)
End If
'Put the sheet name in the first column the copy from the template
row E then fill the next 26 rows with the formula
r.Select
ActiveCell.Value = sname
Range("E3").Select
Selection.Copy
r(2).Select
ActiveSheet.Paste
r(2).Select
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
'Copy the formula from E29 to the column then add a formula to copy
results from the spreadsheet to the table and fill the next 26 rows
Range("e29").Select
Selection.Copy
r(28).Select
ActiveSheet.Paste
r(30).Select
ActiveCell.Value = sname
r(31).Select
ActiveCell.Formula = "='" & sname & "'!N3"
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
'Copy another set of 26 results from the table
r(58).Select
ActiveCell.Value = sname
r(59).Select
ActiveCell.Formula = "='" & sname & "'!O3"
Selection.Copy
For x = 1 To 25
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next x
Range("e86").Select
Selection.Copy
r(85).Select
ActiveSheet.Paste
'get rid of the Templates column on the Tables sheet so it doesn't
figure in the results as not needed once first player entered
If Range("e2") = "Template" Then
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Else
End If
'Go to the home sheet where we sort the results adding each players
weekly result from column r2 onwards
Sheets("Home").Select
Dim z As Range
If [R2].Formula = "" Then
Set z = [R2]
Else
Set z = Range("Q2").End(xlToRight).Offset(0, 1)
End If
z.Select
'Label the column
ActiveCell.Value = sname
'get the result from the tables column depending on the value in Q3
on the home sheet
z(2).Select
'This line is where it fails. Everything works perfectly up to here....
ActiveCell.Formula = "=INDEX(Tables!r(2):r(27),$Q$3)"
z(3).Select
ActiveCell.Value = "=INDEX(Tables!r(59):r(84),$Q$3)"
z(4).Select
ActiveCell.Value = "=Tables!r(28)"
z(5).Select
ActiveCell.Value = "=Tables!r(85)"
End Sub