how to copy a sheet from external to current workbook and saving that sheet as a new work book - vba

I am working on an excel automation. I have a main workbook which has two sheets i.e. dashboard and data1. I need to fetch a sheet from another workbook(whose name can be anything i.e its not fixed. consider it as filex.xls) placed in the D: drive and paste the entire sheet to the data1 sheet. I need to perform some operations on that sheet then save data1 sheet as a new workbook in C: drive with the same name as the original file followed by modified (i.e. filexmodified.xls)

To import the data:
Workbooks.Open PATH
Worksheets(1).UsedRange.copy data1.cells(1) 'data1 should be a Worksheet Variable
ActiveWorkbook.Close False
For saving as new Workbook:
ThisWorkbook.SaveAs PATH

Related

Copy sheet using copyToEnd()

I am trying out Keikai Spreadsheet. After importing an excel file, I wanted to copy a sheet using copyToEnd() but it deletes my original sheet. Am I doing it wrong?
Workbook srcBook = spreadsheet.imports("book1", new File(BOOK_FOLDER, "book1.xlsx"));
srcBook.getWorksheet().copyToEnd(spreadsheet.getWorkbook());
Currently,spreadsheet.import() will change active Workbook to the just imported book; i.e. in this case book1.xlsx. So the original sheet is not deleted but just not active ( invisible).
Therefore, the srcBook actually equals to spreadsheet.getWorkbook() after importing. When you copy the srcBook.getWorksheet() to spreadsheet.getWorkbook(), you copy a sheet in the same book. You will see 2 duplicate sheets in the active book.
The workaround is:
final Workbook book = spreadsheet.getWorkbook();
Workbook srcBook = spreadsheet.imports("book1.xlsx", new File(BOOK_FOLDER, "book1.xlsx"));
srcBook.getWorksheet().copyToEnd(book);
spreadsheet.setActiveWorkbook(book.getName());
System.out.println("currentBook: spreadsheet.getWorkbook():" + spreadsheet.getWorkbook().getName());
In the future version, spreadsheet.imports() should not change active Workbook automatically unless it is the only workbook that bound to the application.

VBA - Copying cells across Workbooks

I'm writing a VBA program that changes the visuals of an excel database. At the end, I need to add a "header" (5 rows of text) at the top. The problem is, I cannot insert the text with VBA, since it contains letters (for ex. á, é...) that aren't compatible with VBA, I need to insert that text from another excel file.
The macro I have is stored in a standalone excel workbook, that also contains the cells of the header I need to copy into my database. The problem is, the name of the excel files I am working with varies. Is there a way I could switch between those 2 files and copy cells across them. Can I store the name of the excel file I am working with and later use it in the VBA code to switch between the workbooks?
Not sure if this 100% answers your question but hope it helps, you can open and store both workbooks as objects using:
Dim wb as Workbook, wb2 as Workbook
Set wb = Workbooks.Open("C:\User\Sample_Workbook_File_Path_1.xlsx")
Set wb2 = Workbooks.Open("C:\User\Sample_Workbook_File_Path_2.xlsx")
From there you can call values from either workbook using things like:
'to get the second workbooks excel file name into a worksheet: "Sample_Workbook_2"
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Name
'to copy files
wb2.Worksheets("Second_Workbooks_Worksheet").Range("A2:A100").Copy _
wb.Worksheets("Sample_Worksheet").Range("A2")
'Alternatively you can store the entire workbooks path name instead of the file name using:
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Path

Excel Personal Macro Workbook reference Book1

I've finally figured out why my code was crashing. I have this set up as part of my Personal Macro Workbook so when I open a default Book1 I can run it. However, the issue is that since it's running the macro from the PMW the "Sheet.Copy After:=ThisWorkbook.Sheets(1)" is crashing.
How can I make it that the code below running from the PMW would copy the sheets into the default Book1?
Original code below;
Sub GetSheets()
Application.AutoRecover.Enabled = False
LInput:
PL = Application.InputBox("Threshold Report Path", "", "C:\Users\")
Path = PL
Filename = Dir(Path & "*.csv")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
ThisWorkbook refers to the workbook with the macro.
You can refer to it by name:
Sheet.Copy After:=Workbooks("Foo").Sheets(1)
I think you misunderstand the purpose of the Personal Macro Workbook; it shouldn't be auto-running anything. It's not a template. It's a place to store macros that you use often, so that instead of copying the macros to different workbooks, you can leave it in one place an run it from there.
I think what you want is a Personal Template that includes the template worksheet already, so nothing needs to be copied every time you create a new document.
Create a workbook, copy the worksheet in manually, and save it as a template. Avoid auto-run code in the template as well.
See links below for more information.
More information:
What you are trying to use:
Office.com : Create and save all your macros in a single workbook
Office.com : Create and save all your macros in a single workbook
What you should be using:
Office.com : Save a workbook as a template
Makeuseof : How to Quickly Create a Custom Excel Template to Save Time

Find Seprate Sheet name from 100 Workbook and paste into the Master File

Respected All,
My question is below
I have a 50 Workbook in a folder and in every workbook there is a separate Sheet name Call "Data". here i would like to loop form multiple workbook throw loop and find seprate "Data" sheet name in the workbook if there is a sheet called "Data" then Copy that sheet and paste into the Master Work book. is it possible in in VBA if possible please guide me how to do this.

Excel VBA - copy&paste a whole row from a closed workbook to my active workbook if certain criteria are met

Currently I have a workbook with a macro that imports data regarding information about active notifications for outstanding work on aircraft from a text file that is automatically saved to a location every 2hrs. Each row is for a different notification and in each column is data such as notification number, aircraft Registration, description of work, required completion time etc.
A separate macro then re-organises the data into the correct format.
The user can then write remarks about the progress of the work next to the notification in another column.
A final macro then archives this by saving a copy of this file to a folder with a specific name dependent on the date/time called "WORK PLAN hhmm_ddmmyyyy".
Because not all of these notifications are completed in the 2hrs between text file dumps the newly imported data will contain the active notifications from the previous text file that have not been fully completed as well as a bunch of new notifications. The problem is that when the new text file is imported by the user into the active workbook all of the remarks are lost. To save copying and pasting manually the remarks for each notification that is still active from the archived file I would like a macro to be able to do this.
I need a macro that can recognise any notification numbers in column I that are present in both the most recently archived workbook and my active workbook, and then copies that row from the archived workbook and pastes it over the row with the same notification number in the active workbook.
Here is some code that Opens a readyonly version of a file.
Determines the rows and columns and adds the data from the readonly file to the first empty row on the sheet.
Sub OpenWB()'You will need to open the workbook as readonly. that way the original one is not busy when the other macro tries to update it.
Dim Your_2_hr_WB As Workbook
Workbooks.Open Filename:="FilePath & name", ReadOnly:=True
Dim Number_of_Rows As Long
Dim Number_of_Columns As Long
Dim This_Num_Rows As Long
Dim This_Num_Cols As Long
For Each Your_2_hr_WB In Workbooks
If Your_2_hr_WB.Name = "What ever name it is " Then
Exit For
End If
Next
Number_of_Rows = Your_2_hr_WB.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
Number_of_Columns = Your_2_hr_WB.Sheets(1).Cells(1, Columns.Count).End(xlUp).Column
This_Num_Rows = ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Sheets(1).Cells(This_Num_Rows + 1, 1).Resize(Number_of_Rows, Number_of_Columns).Value = Your_2_hr_WB.Cells(1, 1).Resize(Number_of_Rows, Number_of_Columns).Value
'You can replace Your_2_hr_WB.Cells(1, 1) with Your_2_hr_WB.Cells(2, 1) if you have a header row
End Sub