2 questions, the program I have made copy and pastes a table from another worksheet to the cell you have selected. What I want to do is name this selected table using a variable, how do I do this? In other words I have down the bottom (Selection.Name = Ans) but this will not name the selected what ever the variable Ans is
and for some reason the Vlookup is also not working correctly, I think it has something to do with how I set the range "MyRange".
'VLOOKUP
Dim Ans As Variant
Dim myVal As String
Dim MyRange As Range
Set MyRange = Sheets("Program (H)").Range("A1:B50")
myVal = Sheets("Program (H)").Range("D2")
Ans = Application.VLookup(myVal, MyRange, 2, False)
If IsError(Ans) Then
Ans = "Not Found!"
Else
'do nothing
End If
Selection.Name = Ans
If it helps the whole code is:
Sub New_Table()
' New_Table Macro
'Reveals the Worksheet "Program (H)"
With Worksheets("Program (H)")
.Visible = True
End With
'Add 1 to the counter
Sheets("Program (H)").Select
Range("D2").Value = Range("D2").Value + 1
'Paste the sheet
Sheets("Caclulator (H)").Select
Range("B3:P17").Select
Selection.Copy
Sheets("SOR Data").Select
ActiveSheet.Paste
'VLOOKUP
Dim Ans As Variant
Dim myVal As String
Dim MyRange As Range
Set MyRange = Sheets("Program (H)").Range("A1:B50")
myVal = Sheets("Program (H)").Range("D2")
Ans = Application.VLookup(myVal, MyRange, 2, False)
If IsError(Ans) Then
Ans = "Not Found!"
Else
'do nothing
End If
Selection.Name = Ans
'Hide the sheet again
With Worksheets("Program (H)")
.Visible = False
End With
End Sub