0

I am a complete beginner at macros. I frequently have to create and rename hundreds of tables and I recorded the macro to try and automate the process, but I cannot figure out how to adjust it so that it runs only on the selected cells. The name of the table is always the same as the header. Can someone please help me?

Here is my recorded Macro:

Sub Table_Creation()
'
' Table_Creation Macro
' Makes tables easier
'
' Keyboard Shortcut: Ctrl+t
'
    Range("CV1:CV6").Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$CV$1:$CV$6"), , xlYes).Name = _
        "Table432"
    Range("Table432[[#All],[dayton_oh]]").Select
    ActiveSheet.ListObjects("Table432").TableStyle = "TableStyleMedium1"
    Range("Table432[[#Headers],[dayton_oh]]").Select
    Selection.Copy
    ActiveSheet.ListObjects("Table432").Name = "dayton_oh"
End Sub

A similar post offered the solution of changing the range to selection, but I guess I did it incorrectly and received an error message. Any help is appreciated!!

Thank you

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
DusTy
  • 1

1 Answers1

0
Sub Table_Creation()
' Makes tables easier
'
' Keyboard Shortcut: Ctrl+t
    
    'declaration of variables
    Dim MySelection As Range
    Dim MyTableName As String
    
    'fill variables
    Set MySelection = Selection
    MyTableName = MySelection.Cells(1, 1).Value 'first cell in your selection will be the table name
    
    'creating table with your table name and style
    ActiveSheet.ListObjects.Add(xlSrcRange, MySelection, , xlYes, , "TableStyleMedium1").Name = MyTableName
End Sub

Try on a copy of the workbook first, so you are sure it will not mess up your sheets, just in case it doesn't do what you want.

WeAreOne
  • 1,310
  • 1
  • 2
  • 13