Close Window
Free Ebook
Get Your Free Excel
Top 15 Excel Tutorials from
Instant Access!

Vba Code Copy And Replace Sheet


The code I use copies "Blad6" of workbook Names, and replce the page"Blad6" in Workbook "Form_transfertest".
The problem is that the code insert a new page, I want that the old sheet "Blad6" in the workbook Form_transfertest is replaced by the copied one from workbook Names.
The code:

Sub copy() 
 Workbooks.Open Filename:= _ 
 Application.EnableEvents = False 
 Sheets("Blad6").copy Befo =Workbooks("Form_transfertest.xls").Sheets("Blad6") 
 Application.EnableEvents = True 
End Sub 
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines



Free Excel Courses

Similar Excel Tutorials

Sub Convert_Html_Entities() ''''''''''''''  '''''''''''''' 'Convert HTML Entities into readable tex ...

Code: Sub Convert_Html_Entities() ''''''''''''''  '''''''''''''' 'Convert HTML Entities into readab ...

Excel macro that converts all HTML entities into their readable equivalent. This includes things like   an ...

The Find & Replace works much the same way as Find and is located in the same place. (Find & Select drop do ...

Helpful Excel Macros

- This Excel Macro allows you to save a specific worksheet within the Excel Workbook to its own new file. You will be a

- This Excel Macro will save the currently visible/active worksheet (the one that you see when you run the macro) to a

- This free Excel macro illustrates all of the possible parameters and arguments that you can include in the PrintOut Meth

- This is a very simple Message Box, pop-up window, macro for Excel that illustrates how to put new lines, the same thi

- This is the ultimate Lookup Macro for Excel. It will search every worksheet in the workbook and return all of the mat

Similar Topics

We have a number of Excel users in our office who cannot copy and paste
between Excel workbooks. They can copy and paste between worksheets. When
you highlight the section to copy and then go to the new workbook both the
and paste special are "grayed out". This is true whether you right-click the
mouse, go to the edit menu, or use control keys. This occurs with any data
type and the most simple workbooks. I have seen some suggestions here but
none have worked for this particular problem. I have reset the menus and
renamed the .xlb files and neither helps. You can open the clipboard and the
paste will work, but there is no paste special option. Any help would be
greatly appreciated. Thanks!

Hello, I'm trying to count the number of times a name appears across multiple worksheets but I can't seem to figure it out.

I have 6 sheets to count and all the names are in column A (from A1:A100) on each sheet. The names are not in a particular order.

On sheet 7 I want to have a cell beside each persons name that counts the number of times their name appeared on the other 6 sheets.

For example Sheet 7 (called total) would have

Joe 5 (where 5 is the outcome of the formula I'm looking for to count all the times "Joe" appears on sheets 1-6).

I have it counting per sheet with =COUNTIF(A1:A130,"name") but this is not quite what I am looking for.

Thanks in advance for any advice.

Edit: I am using Excel 2007 w/ windows XP

Hi, I'm trying to get some macros to combine data from two open workbooks, and I can't figure out the commands to switch from the active workbook to the other open workbook and back. I'm new to VBA and just learning the ropes. All I can figure out is how to switch to another workbook with an exact name. If I record the macro to go to a recent file, for example Window > 1 (filename), the code that I get in VBA is Windows("filename").Activate. This doesn't translate to when I have two different workbooks with different filenames open.

Make sense? I'm a bit confused myself. Maybe just a list of basic workbook-switching techniques or commands would be useful.

- Michael

Hi All,

I got the below macro which uses IE and open the URLs. I want to create a userform with listbox with radio button and commandbutton on the same which will help me to connect to each url when I select the same in listbox and click on the commandbutton.

Sub DoBrowse1()
 Dim ie As Object
 Set ie = CreateObject("Internetexplorer.Application")
 ie.Visible = True
 ie.Navigate ""
End Sub

Any suggestions..

I have workbook in which I want to save a specific worksheet to a new file with only the values saved - all data in this worksheet are references to cells on another worksheet, which is using VLOOKUP to pull data from a database.

Found the following code and it gets me close, but it copies the cell references, not the values. It also allows me to specify the file name from a cell reference.

I want the new workbook file to simply be saved, not opened, and a message box to display stating where the file was saved (will always be in the same location on the LAN).

What modifications do I make to this to get this to work per above requirements?

Sub CopyMe()
Dim SaveMeAs As String
SaveMeAs = Sheets("Sheet1").Range("B2").Text
ActiveWorkbook.SaveAs Filename:="C:\My Documents\" & SaveMeAs
End Sub

I'm trying to come up with a solution to calculate the man hours and days worked over multiple sheets.

I have a file that has 8 sheets. the first seven sheets are the days of the week and the 8th sheet is where I want to calculate the hours.

The days of the week consists of "Name" "Date" Hours"

The 8th sheet consists of "Name", "Days of the week in their own column (7 columns in total)", "Total Hours" and "Total Days Worked"

Now... each day is always a different group and what I'm hoping for is to create a formula that can grab each name with their hours and throw them into the 8th sheet. So when the end of the week is done I have all the Employees weekly hours and days worked all finished.

I hope this makes sense. This file is to track about 100 employees that are on a rotational work schedule.

I have attached a sample file.

Thanks in advance for your help.

Is it possible to apply a filter to multiple sheets within the same Excel workbook, using the same filter criteria?

For example, suppose you have tables of data on 3 different tabs, all having the same headings. Suppose one of the headings is "gender". If you apply a filter to the table on Sheet 1, to only show the rows containing the word "female", is it possible to have the tables on Sheets 2 and 3 automatically update to only show the "female" rows? Or would you have to manually update the filter criteria in Sheets 2 and 3?

I hope this makes sense. Thank you in advance for any help.

I have a requirement to change the panes in excel.
My excel file will contain more than one tab.
I have to free the first two lines in all the tabs except the first one.
I tried the following code.


ActiveWindow.FreezePanes = True

But in this I have to change the Active Worksheets in VBA, Which I want to avoid.
Is there any method to achieve this without selecting the cells.
Something like

xlsobj.Worksheets(1).Rows("1:2").FreezePanes = True

I am using XL2007 and have a macro that refreshes microsoft query connections. The issue is the refreshes only happen if you step through the macro using the debugger. When you run the macro normally, everything else functions properly, but the data is not refreshed.

Any help is appreciated. Here is an excerpt of the code:

Workbooks.Open Filename:="C:\Profile.xls"
Range("F3").Value = SNR

ActiveWorkbook.Connections("Query from C_Profile").Refresh
ActiveWorkbook.Connections("Query from C_Profile1").Refresh

Hi all.

I have set up a workbook that is sent out to lots of different users. They each keep and use their own copy.

I have set it up so that everything looks OK and is visible on MY screen, but I'm conscious that some users may have different screen sizes, different toolbars set up, and so on, which might make some parts not immediately visible to them.

I have set up an auto-execute macro which automatically sets the zoom factor to best fit, for several of the worksheets, and this works fine.
Here's the code that does it.

 ActiveWindow.Zoom = True

By repeating this code for each worksheet, I can make each one be zoomed just right.

However, the file contains 8 sheets that are all identically laid out, except the number of rows is different.
What I want to do is go to the worksheet that has the largest number of rows (it's always the same worksheet, so I know which one it is), set the zoom factor for THAT worksheet (which I can do, and it always has the same number of rows), and then take THAT zoom factor, whatever it is - and it will vary depending on the user - and apply that to the other worksheets that have a similar layout.

I could just go through each worksheet and zoom it automatically, but that would mean that some of the sheets looked very large, others very small, and I'd like them to have a consistent appearance.
I could also specify a range on each worksheet that was similar to the appropriate range on the longest worksheet, and zoom that automatically, but that's not ideal either, because some of the row heights vary from sheet to sheet, and again I'll end up with different font sizes.

Anyone know how to do this ?

I have never really used VBA and so am completely stuck at this problem. I need to create a macro which auto-populates a master worksheet from the individual user sheets in a shared workbook.

Sheet 1 is the master sheet "Team Stats". There will be an undetermined number of individual worksheets to accomodate new staff.

Each worksheet will be identical, using columns A-I with row 1 having the headings:

Date, Name, Reference, Value, Price, Age, Purchased?, Destination, Add. Products (the last 3 columns will have a drop-down list which will be used to enter data into the cell).

There will be a varying number of rows in each of the individual sheets.

If possible I would like the macro to run every time data is entered into one of the individual worksheets. If this is not then it would be fien to update every time the workbook is opened.

If anyone can help it would really cut down the time I spend collating these stats every day!

Hello, what I would like to do is take a portion of a sheet..... So the cells I want to mirror are in the proposal sheet cells B32 x F51. And then mirror the exact duplicate copy to another sheet. The other sheet should have those same values at A20 X G39. What I am trying to do is have a sheet that has skus, prices, and so forth and then when I add,delete, merge, color, or do anything to the cell on the "proposal creation" sheet I want it to mirror only a specific cells to another sheet that is "salesman copy" that we will print to clients. So this way ont he proposal copy a salesman can insert rows or even change the color of the cells and then have it mirror exactly the same on the "salesman copy" which is what we will print for clients. Thank you.

Hi all,

The following code is placed in workbook 'A' and is used to open workbook 'B'. These workbooks will now always be housed in the same directory and i want to change the code to use a relative path reference by determining the path of workbook 'A'. here's what I had:

Sub income_statement()

Application.WindowState = xlMaximized
Application.Workbooks.Open "C:\Documents and Settings\.....\workbook B.xls", UpdateLinks:=xlUpdateLinksAlways

End Sub

could you suggest how to change this to use a relative path reference?

I have a co-worker's file that he is having trouble with. He is using Excel 2000 SP3. When copying a cell with a formula in it of "=D6+C6" and pasting it into the next cell down, it will display the same value in the cell as the calculated value from above, but has the correct formula displayed in the formula bar of "=D7+C7".

A1: 50
A2: 10
B1: 60
B2: 20

A3: Formula: =A1+A2 Displays: 60
Right click A3, Copy, right click B3, paste
A3 displays 60

When I click save, it will change the display value to 80.

I am trying this on his workstation and mine. Mine has Office 2010, so I think there might be an issue with the file itself.

Also, not just copy and paste. I can also just click the top cell after filling in the formula and then drag the bottom right of the cell downward and it will do the same of filling in the correct formula, but have the incorrect value.

I know that I could get him to just click save each time before really looking at the results, but that is just a band aid to the problem.

Any ideas how to fix this?


I need to open a password protected workbook using VBA.

I've tried the code below but I still get prompted for a password.


Workbooks.Open Filename:="\\HOME\Working\Report.xls" _
, Password:="xxxx"

How do I get it to open automatically???

Hi all,

I have written some code that when a button is selected will Refresh a Data Query Table and all of it's information. However, I seem to be getting issues, can some one point me in the right direction with the code?


.Sheets("Sheet 1").ListObject.QueryTable.Refresh BackgroundQuery:=False

When I recorded myslef doing this process it looked like this:


Sheets("Sheet 1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False

Many thanks!

I was wondering if there is a way of getting a macro to open the "save as" dialogue box and then have it fill in the "file name" field.

I dont want it to actually make the save though as I want the user to have the chance to check that the save location is correct and the option to change the filename if required.


If I want to activate a workbook, I would do this:



But what if the File.xls was a variable named File?

I have tried this:



Without success. Please advise.

I get the error message "reference is not valid" each time I open my spreadsheet. I get this message three times, and once I am done clicking ok on all three of them, my spreadsheet works just fine. The problem is, I have to send it to a bunch people.

I have a sheet with raw data a sheet with pivot tables a sheet with a dashboard and a simple macro
I don't have any #REF cells either.

Someone has any idea of what could be the problem?



I have a weird one here. I cannot scroll up or down on a particular worksheet. All the other sheets in the workbook operate successfully, but this one appears to be locked. I have tried both the right hand scroll bar and the mouse. The indicator on the scroll bar moves but the page is "stuck". I have copied it out to a new workbook and this is consistent behavior here too.



I need the necessity do delete a sequence of sheets in my workbook.
If I use this code:

With ActiveWorkbook
If .Worksheets.Count >= 5 Then
For n = 5 To .Worksheets.Count
Next n
End If
End With

I receive a confirmation message box with this message:

"Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete" [DELETE] [CANCEL]

I wish to delete all sheets without receiving any message.

Is it possible?

Many thanks in advance for your kind support.



I run a small business. I want to start keeping track of inventory using a barcode system.

I know Excel "ok", but I don't know programming.'

I'd like to be able to do something very simple (for now)...

For example...if I had PRODUCT / CODE / INV VALUE

A - CODE A - 5
B - CODE B - 11
C - CODE C - 14

I would want to be able to click a button (or scan a code for) : ADD +1 or REMOVE -1

...then scan the barcode on the product (A) and have it add or remove one, respectively. So, if the row for A is:

A - CODE A - 5

I choose "ADD" (with a button or scan code), then scan the barcode, and the spreadsheet automatically finds the code in the sheet, and changes the corresponding inventory value:

A - CODE A - 6

Does that make sense? How might I go about doing that?

I added a print button to my userform


Private Sub btn1_Click()
End Sub

This prints out the userform as I would like, however it autoprints to the default printer and doesn't allow any printing options so I can't select to "print to one page". So as of now it is only printing out half my form and cutting off the rest.

Any suggestions?

Hi everyone

I need some help in this:

In a excel workbook when I copy a worksheet (to duplicate with other name) there is always a name conflict and so I have to say yes (maintain the name) or say no (and excel ask for other names) many times (sometimes more than 50 times pressing the enter button. Its possible to disable this feature?

My best regards and Thanks in advance.

Melnik Kuhn

I have searched and read all the help files. I find the properties of
an object, I see how I can "lock", "size and move with cells" or "not
move with cells". No matter what I select, the object moves off the
screen, when the user, scrolls to the right of the spreadsheet.

Is there a way to lock the position, let's say, in the upper right
corner and have it stay there?

This would be quite useful for an EXIT button, that I have created,
that will close the program without saving (it's a read-only file.)

Thanks to all the wonderful people here that have been so helpful and
give us their valuable insight and time.


Related pages

filedialog filtercustom validation excelcheckbook spreadsheetexcel vba calculatehow to freeze multiple panes in exceldelete duplicate records in excelpower function in excelexcel 2007 compare two columnscube formulas excellean status symbolsexcel unmerge cellsmicrosoft project subtaskhow to multiply percentages in excelgrouping in excel pivot tablehvac formulas excelworking out a golf handicapfootball fixture creatorqueuing formulasauto format date in excelhow to calculate drawdownvba count distinctminutes per kilometer calculatorexcel vba undosplit excel vbahow to multiply two columns in excelmonthly compound interest calculator excelusing vlookup to compare two listscalculating age in excelexcel sort buttonvertical bar chart excelefficient frontier excel1099 excel templatemultiple conditions sumifexcel macro lookupexcel adjust row heightexcel delete all blank rowsexcel overtime templatethe mailbox worksheetsexcel unhide rowsexcel calculate compound interestclear sheet vbameter to foot formulavlookup picturesgini coefficient formulaspine chartslottery combinations formulaportfolio variance excelexcel vba count rowsexcel function isnaswitch last name and first name in excelexcel temp folderexcel formular1c1access subscript out of rangecreating monthly calendar excelsemi monthly timesheet template excelxlsm converter to xlsexcel colour palettehow to open xlsbhow to say hi in binary codemacro to unlock excel spreadsheetcontainer number check digit formulapercent difference calculator excelcalculating doubling timepassword vba excelschema inioffset command excelvlookup leftexcel pound signssharepoint vlookuprandom number table generator excelsave excel workbook as pdf