Excel macro error

jodyg64

New Member
Joined
Jun 26, 2023
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hi, I'm trying to format data in an Excel file using a macro-enabled workbook and I'm getting an error 1004 The worksheet data for a table needs to be on the same sheet as the table. I'm not sure where it is the location is being changed in this code. Thanks so much!

VBA Code:
Private Sub workbook_open()
    Dim src As Range
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    Workbooks.Open Filename:= _
        "C:\Merge Data Sources\callforsubmissions tab.xlsx"

    Set src = Range("A1").CurrentRegion
    Set ws = ActiveSheet
    ws.ListObjects.Add(SourceType:=xlSrcRange, Source:=src, _
                      xlListObjectHasHeaders:=xlYes, _
                      tablestyleName:="TableStyleMedium28").Name = "Table1"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\jjones\McDow\Devo Team - General\Call for submissions data file\callforsubmissions tab.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    Application.DisplayAlerts = True

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Your macro specifies "ActiveSheet" rather than a named sheet :

Set ws = ActiveSheet vs Set ws = Sheet1

Try changing ActiveSheet to the sheet name where TABLE1 is located.
 
Upvote 0
I can't get your code to fail. Try fully specifying the references.

Rich (BB code):
    Dim wb As Workbook
    Set wb = Workbooks.Open(Filename:= _
        "C:\Merge Data Sources\callforsubmissions tab.xlsx")
    Set ws = wb.ActiveSheet
    Set src = ws.Range("A1").CurrentRegion
 
Upvote 0
Solution
Your macro specifies "ActiveSheet" rather than a named sheet :

Set ws = ActiveSheet vs Set ws = Sheet1

Try changing ActiveSheet to the sheet name where TABLE1 is located.
Thanks very much for your help. When the file (callforsubmissions tab.xlsx) is opened, it's the file name that becomes the name of the sheet (minus .xlsx) Do I set the worksheet to that name? If I do and put the name in quotes, I get a type mismatch error.
 
Upvote 0
That was not the answer then. I was brainstorming ....

Did [B]Alex Blakenburg[/B]'s suggestion work for you ?
Thanks for your brainstorm! I thought I had tried Alex's suggestion, but I had missed the wb. before activesheet in the set ws=.wb.activesheet. Now that I caught that, it does work!! Thanks for your help!
 
Upvote 0
I can't get your code to fail. Try fully specifying the references.

Rich (BB code):
    Dim wb As Workbook
    Set wb = Workbooks.Open(Filename:= _
        "C:\Merge Data Sources\callforsubmissions tab.xlsx")
    Set ws = wb.ActiveSheet
    Set src = ws.Range("A1").CurrentRegion
Thank you very much, you made it work!
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top