Despite the many posts I looked through on the same questions as my question, none of the answers satisfy what I'm looking for. If you can connect me with one, I would love to read it.
I have a book with worksheets. For simplicity, suppose there is a worksheet in my book. And on my sheet called "Sheet1", cells A1-A4 have data.
I want my VBA code to execute:
- Copy line 1 (or, in particular, cells A1-A4) of book "A" to the range variable "myRange"
- Create a new book, call this book "B"
- Give the default workbook “B” “sheet1” a new name “Test Name”
- Open book "B" (although I understand that the VBA code "Workbooks.Add" opens a new book, so this step may be redundant because Workbooks.Add covers half of items 2 and 3).
- Insert 'myRange' into the first line of "Workbook B"
- Save Workbook B with the name Test Book and the time stamp enclosed in square brackets. The file must also have the file extension "xls"
- Close Workbook B and return to Workbook A
What I still know:
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 miss the code that will handle:
- Paste my copied data into a new book
- change the name of the new book book1 to another.
- add timestamp to file name string when saving
, , , -, ActiveWorkbook. AFAIK, "Workbooks.Add", , .. . , VBA "A"? , Workbook 'A', "MyBook.Activate", "MyBook" " ". ?
.
,
QF