• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

VBA Code Not Running On All Computers|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA Code Not Running On All Computers|VBA & Macros|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA Code Not Running On All Compute…
sp_PrintTopic sp_TopicIcon
VBA Code Not Running On All Computers
Avatar
Michael Rempel

New Member
Members
Level 0
Forum Posts: 2
Member Since:
April 21, 2021
sp_UserOfflineSmall Offline
1
April 21, 2021 - 12:20 am
sp_Permalink sp_Print

Some background. This file is being used by someone in Mexico, so I don't know if that has anything to do with the error she's receiving. I wrote this using Office 365. It works perfectly on my computer and another computer in my house using Office 365. I tried on Excel for Mac computer and it worked fine. I tried it on a friend's computer using Excel 2013 and I get the same error. The error says:

"An error has been produced 1004 in running time
This name is already used. Try with a different one"

When I click on debug, it highlights the line of code that is in bold and red below

I made sure the path was correct for any computer we tried it on. The error occurs right after it's copying the last of the 4 files.

What the code does is copies the worksheets from the other four files in the folder to the file called "BD - Inversion Comunitaria EDR", then adds two new worksheets at the end. It never gets to the point of adding the new worksheets. An image of the folder containing all 5 files is attached.

I have the following VBA code:

Sub ICEDR()

Dim Path As String
Path = "F:\Excel Help\Edna\Inversion Comunitaria EDR\"

Dim FileName As String
FileName = Dir(Path & "*.xlsx")

Dim ws As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While FileName <> ""
Workbooks.Open Path & FileName
For Each ws In ActiveWorkbook.Sheets
ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Next ws
Workbooks(FileName).Close
FileName = Dir()
Loop

Worksheets(1).Delete

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Peticiones"

Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Proyectos"

End Sub

sp_PlupAttachments Attachments
  • sp_PlupImage Code-Folder.png (223 KB)
sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
April 21, 2021 - 3:21 am
sp_Permalink sp_Print

Hi Michael,

The error means that you are trying to copy a worksheet that already exists in the destination.

You cannot copy Sheet1 from Book1 into Book2, if Book2 already has a sheet named Sheet1.

To avoid the error, check if the sheet you want to copy already exists in destination workbook:

Function SheetExists(Byval ShName as string, Wb as workbook) as boolean

On Error Resume Next

SheetExists=Not wb.Worksheets(ShName) is Nothing

End Function

Use it like this:

If SheetExists(ws.Name,ThisWorkbook)=False then

ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)

Else

Debug.Print "Sheet name exists: " & ws.name

End If

sp_AnswersTopicAnswer
Answers Post
Avatar
Michael Rempel

New Member
Members
Level 0
Forum Posts: 2
Member Since:
April 21, 2021
sp_UserOfflineSmall Offline
3
April 21, 2021 - 10:25 pm
sp_Permalink sp_Print

As it turns out, there were hidden worksheets in each workbook that did have the same name.  Deleting or changing the names of those solved the issue.  However, I noticed that with Office 365, Excel would automatically rename those worksheets with a parenthetical number after each and not result in the error. Is this a new feature in Office 365, or what version did this option appear in?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
April 21, 2021 - 11:10 pm
sp_Permalink sp_Print

That happens in all versions, if you copy the sheets MANUALLY. Copying sheets in VBA is another story, the usual user interface automation is not functional because ... there is no user action when vba code is running.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Misael Gutierrez Sr.
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Cathi Giard
Sarah Young
Henry Delgado
Alita Nieuwoudt
KL KOH
Joao Marques
Regi Hampton
Taffie Elliott
Paramita Chakraborty
David du Toit
Forum Stats:
Groups: 3
Forums: 24
Topics: 6358
Posts: 27805

 

Member Stats:
Guest Posters: 49
Members: 32335
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.