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 7815285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:28:37+00:00 2026-06-02T05:28:37+00:00

Despite many posts I have looked through being of along the same lines as

  • 0

Despite many posts I have looked through being of along the same lines as my question, none of the answers satisfy what I am looking for. If you can link me to one I’d gladly read it.

I have a workbook with worksheets. For simplicity, let’s say my workbook has a worksheet. And in my worksheet which is called “Sheet1”, there is data in cells A1 to A4.

What I want my VBA code to do is:

  1. Copy row 1 (or specifically cells A1 to A4) of Workbook ‘A’ into Range variable ‘myRange’
  2. Create a new workbook, let’s call this one Workbook ‘B’
  3. Give Workbook ‘B’s default “sheet1” a new name to “Test Name”
  4. Open Workbook ‘B’ (though I realise that VBA code “Workbooks.Add” opens a new book so this step may be redundant since Workbooks.Add covers half of point 2 and 3)
  5. Paste ‘myRange’ into first row of ‘Workbook B’
  6. Save ‘Workbook B’ with name “Test Book” and a timestamp enclosed in square brackets. The file must also be of the file extension “xls”
  7. Close ‘Workbook B’ and return to ‘Workbook A’

What I have so far is this:

Sub OpenAndSaveNewBook()
    'Declarations
    Dim MyBook As String
    Dim MyRange As Range
    Dim newBook As Workbook

    'Get name of current wb
    MyBook = ThisWorkbook.Name
    Set MyRange = MyBook.Sheets("Sheet1").Range("A1,F1")

    'Create/Open new wb
    newBook = Workbooks.Add

    'Save new wb with XLS extension
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "/" & "TEST-BOOK", _
                            FileFormat:=xlNormal, CreateBackup:=False

    '===NOTE: BEFORE THE FOLLOWING RUNS I NEED TO PERFORM ACTIONS ON CELLS VIA VBA ON
    '===WORKBOOK 'A'. DOES THE NEWLY CREATE WORKBOOK BECOME THE PRIMARY/ACTIVE WORKBOOK
    '===? AND SO THEN DO I NEED TO ACTIVATE WORKBOOK 'A'? 
    ActiveWorkbook.Close savechanges:=True

    'Return focus to workbook 'a'
    MyBook.Activate
End Sub

As you can see, I am lacking the code that will handle:

  • the pasting of my copied data to the new workbook
  • the changing of the new workbook’s sheet1 name to something else
  • adding a timestamp to the filename string on save

Lastly, I have included a question in my code as I think I may have a misunderstanding of the ActiveWorkbook method. AFAIK when the code “Workbooks.Add” runs this becomes the Active Workbook, i.e. one with the focus. Does this effect how the VBA code running on Workbook ‘A’? Does this mean that if I wanted to add code to manipulate cells of Workbook ‘A’ then I would need to use “MyBook.Activate” where ‘MyBook’ holds the string of Workbook ‘A’s actual title?

Any help will be greatly appreciated.

Thanks,
QF

  • 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-02T05:28:39+00:00Added an answer on June 2, 2026 at 5:28 am

    Instead of copy pasting the way you mentioned above, you can directly do this. This will also negate the use of a variable.

    MyBook.Sheets("Sheet1").Rows("1:4").copy _
    newBook.Sheets("Sheet1").Rows("1")
    

    EDIT

    I just noticed an error with your code.

    newBook = Workbooks.Add
    

    This line will give you an error as you have to use Set

    Your code can be written as

    Option Explicit
    
    Sub OpenAndSaveNewBook()
        Dim MyBook As Workbook, newBook As Workbook
        Dim FileNm As String
    
        Set MyBook = ThisWorkbook
    
        FileNm = ThisWorkbook.Path & "\" & "TEST-BOOK.xls"
        Set newBook = Workbooks.Add
    
        With newBook
            MyBook.Sheets("Sheet1").Rows("1:4").Copy .Sheets("Sheet1").Rows("1")
    
            'Save new wb with XLS extension
            .SaveAs Filename:=FileNm, FileFormat:=xlNormal, CreateBackup:=False
    
            .Close Savechanges:=False
        End With
    End Sub
    

    MORE EDIT

    Elaborating on the use of SET

    I would recommend you to see this post.

    LINK: Worksheets does not work

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

Sidebar

Related Questions

Despite I have been using MVC in PHP many times, I found out that
Despite the other excellent answers here to similar posts, I cannot see the error
I think this has been done many times before but despite reading some posts
Despite substantial research and many posts that seem to indicate this isn't too tough,
Despite there being many questions on nested hashes I've not found any solutions to
Despite being a Project Euler program, the following code doesn't actually concern it much.
Disclaimer Despite the title, this is a genuine question, not an attempt at Emacs/Vi
Despite some posts on this forum and others I cannot find something that tells
I have seen many of website are displaying RSS Feeds on their website. Example:
I've been pondering this question awhile now... many 3d engines support advanced terrain rendering

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.