Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:10:42+00:00 2026-06-09T20:10:42+00:00

I am continuing working with data pulled from a mainframe. The data is largely

  • 0

I am continuing working with data pulled from a mainframe. The data is largely alphanumeric, and is a continuation of building functionality onto past efforts. The loop in which the sheets are moved was created on the basis of the function discussed in this SO article.

I have consulted this SO article and independently tested the general form of such functionality, but such code is failing in this specific case, perhaps due to version issues or the way I am referencing the new workbook.

I have written a subroutine that shifts worksheets from one workbook to another. It is used in conjunction with a public variable tied to a checkbox, and is intended to provide the user with an easy way to port the form data. The public variable is used in a conditional to call the subroutine if the checkmark is in a checked state.

The code is as follows:

Public Sub MoveSheets()

' This macro moves all sheets but those noted within the If statements,(1)
' (1) which are further nested within the For Each loop.

'See http://msdn.microsoft.com/en-us/library/office/ff198017.aspx for save file format types.
'The file format of this excel macro sheet is 52: xlOpenXMLWorkbookMacroEnabled
'The default save type for the use-case is excel 2003 is compatibility mode.
'The macro is to set the current save type to one compatible with 52.
'In this case, 51: xlOpenXMLWorkbook was selected as the selected type.

'Define Excel Format storage variables.
Dim FileFormatSet As Excel.XlFileFormat
Dim OriginalFileFormatSet As Excel.XlFileFormat
Dim TempFileFormatSet As Excel.XlFileFormat


'Define worksheet/workbook variables.
Dim IndividualWorkSheet As Worksheet
Dim NewWorkBook1 As Workbook

'Set variable to store current time.
Dim CurrentTime As Date

'The original file format.
OriginalFileFormatSet = Application.DefaultSaveFormat

'The file format to be used at the end of the procedure after all is said and done.
FileFormatSet = OriginalFileFormatSet

'The currently desired file format.
TempFileFormatSet = xlOpenXMLWorkbook

'The currently desired file format set as the set default.
Application.DefaultSaveFormat = TempFileFormatSet

'Disable application alerts. Comment the line below to display alerts (2)
'(2) in order to help check for errors.
Application.DisplayAlerts = False

'Save new workbook "NewWorkBook" as default file format.
Workbooks.Add.SaveAs Filename:="NewWorkBook", FileFormat:=Application.DefaultSaveFormat

'Set variable to new workbook "NewWorkBook", which is in .xlsx format.
Set NewWorkBook1 = Workbooks("NewWorkBook.xlsx")

'Activate Macro Window.
Windows("ShifterMacro.xlsm").Activate
'For each worksheet, shift it to the workbook "NewWorkBook" if (3)
'(3) it fails outside the criteria set listed below.
For Each IndividualWorkSheet In ThisWorkbook.Worksheets
    If IndividualWorkSheet.Name <> "Sheet1" And IndividualWorkSheet.Name <> "Criteria" And _
    IndividualWorkSheet.Name <> "TemplateSheet" And IndividualWorkSheet.Name <> "TemplateSheet2" And _
    IndividualWorkSheet.Name <> "Instructions" And IndividualWorkSheet.Name <> "Macro1" And _
    IndividualWorkSheet.Name <> "DataSheet" Then

            'Select each worksheet.
            IndividualWorkSheet.Select

            'Shift the worksheetover to the new workbook.
            '****NOTE: THIS CODE CURRENTLY RESULTS IN A '1004' RUN-TIME ERROR.****
            IndividualWorkSheet.Move After:=NewWorkBook1.Sheets.Count

    End If
Next

'An ugly set of If Then statements to clean the new workbook (4)
'(4) of its previous sheets, assuming entries are to be made.
If NewWorkBook1.Sheets.Count > 1 Then

    NewWorkBook1.Sheets("Sheet1").Delete

End If

If NewWorkBook1.Sheets.Count > 1 Then

    NewWorkBook1.Sheets("Sheet2").Delete

End If

If NewWorkBook1.Sheets.Count > 1 Then

    NewWorkBook1.Sheets("Sheet3").Delete

End If

'********** CODE CHECKED BUT UNTESTED WITHIN THESE BOUNDARIES **********

'Activate the Window for the new workbook if it is inactive.
Windows("NewWorkBook.xlsx").Activate

'If the number of sheets are greater than 1... (5)
If Sheets.Count > 1 Then

    '(6) The time value is parsed to remove unusual characters, following (5)
    '(6) the logic of this SO article: https://stackoverflow.com/questions/11364007/save-as-failed-excel-vba.

    'Formatted current time as per http://www.mrexcel.com/forum/excel-questions/273280-visual-basic-applications-format-now-yyyymmddhhnnss.html
    CurrentTime = Format(Now(), "yyyy-mm-dd-hh-nn-ss")

    '(5) Then go ahead and save the file with the current date/time information.
    NewWorkBook1.SaveAs Filename:="Form_Data_" & Now, FileFormat:=Application.DefaultSaveFormat

End If

'Set SaveChanges = True, as per https://stackoverflow.com/questions/9327613/excel-vba-copy-method-of-worksheet-fails
ActiveWorkbook.Close SaveChanges:=True

'********** END CODE BOUNDARY **********

'Activate the Window for the Macro.
Windows("ShifterMacro.xlsm").Activate

'Activate a specific sheet of the Macro.
ActiveWorkbook.Sheets("Instructions").Activate

'Change Display Alerts back to normal.
Application.DisplayAlerts = True

'Reset the view for the original data sheet.
With Sheets("Sheet1")

'For when this process is repeated.
If .FilterMode Then .ShowAllData

End With

'Return the Default save format to the original file format.
Application.DefaultSaveFormat = FileFormatSet

End Sub

The code fails at line 62, and results in a ‘1004’ Run-Time error:

IndividualWorkSheet.Move After:=NewWorkBook1.Sheets.Count

The worksheet referenced holds the correct test value of ‘100-AAA’. The Sheets.Count is equal to 3. The NewWorkBook1 variable holds the value NewWorkBook.xslx. The path of the generated workbook “NewWorkBook.xslx” is the same as the macro workbook. My version of excel is 2007, although the default for my users is 2003, even though they have the capacity and the installation for 2007.

Why is this run-time error with my Move occuring, and how do I correct this error in order to get my worksheets over to the newly generated workbook?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:10:43+00:00Added an answer on June 9, 2026 at 8:10 pm

    This:

    IndividualWorkSheet.Move After:=NewWorkBook1.Sheets.Count

    just specifies to move the sheet within the same workbook (and it will error if NewWorkBook1 has more sheets than its parent workbook)

    Maybe try:

    IndividualWorkSheet.Move After:=NewWorkBook1.Sheets(NewWorkBook1.Sheets.Count)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a iphone/Ipod touch project involving saving and fetching data from
I'm working with Core Data and a many-to-many relationship: a building can have multiple
I am working with some code that uses an OleDbConnection to load data from
I'm working on an application that opens a database connection and retrieving data from
So i am working with some email header data, and for the to:, from:,
I'm working with SlickGrid, binding data directly to the grid from an Ajax call.
I am working on a project where I read data from a file and
I'm working on an app which is creating a data bank of questions from
Hi I'm working on trying to cluster network data from the 1999 darpa data
I am currently working on a python script that pulls data from a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.