So I have an Excel workbook that contains a template sheet for products.
I am creating new products on a Main Page via a UserForm which creates a new sheet, copies the cells from the template sheet and pastes them onto the new sheet and then fills in the rest with the information given from the UserForm.
The problem I am having is that I want each one of these newly created sheets to contain code for Worksheet_Change. I do not know of a way in which to create the new sheet and also give it the necessary code which is currently in the template sheet.
Any help will be very appreciated!
Don't copy the cells to the new sheet.
Instead, copy the entire sheet.........any sheet code will copy with it.
EDIT#1:
For example, if worksheet "Template" contains some worksheet code, then:
Sub marine()
N = Sheets.Count
Sheets("Template").Copy After:=Sheets(N)
End Sub
will copy that sheet, code & all.
Related
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.
I'm trying to copy certain sheets from a workbook into a new book.
Copying the relevant sheets works fine, however in the new workbook all of the charts are still linked to cells from the old workbook.
Does anyone know of an intuitive way of copying over sheets to a new book but having the charts source data from the new book?
Here is an example of how I'm copying the sheets over:
Sub copySheets()
' Copy over other sheets
oldBook.Sheets("Chart Workings").Copy Before:=newBook.Sheets(1)
With newBook.Sheets(1).UsedRange
.Value = .Value 'converts formulas to values
End With
End sub
[EDIT] Ideally a method that doesn't involve going into each chart and pointing the series at the new workbook as I have over 65 charts and this will take a while...
I am trying to create a cover sheet for an excel workbook, the user will populate these cells then a macro will paste them across all worksheets inside the workbook.
The cover sheet asks for information in cells from column D and column I which could be common across the whole workbook. I am trying to make filling out this workbook faster and easier.
I have a basic understanding of Excel VBA but require help to write this, thank you.
You code use something like this
Sub Main
Dim cellsToCopy As Range
Dim shtNames as Variant, shtName As Variant
Set cellsToCopy = Worksheets("CoverSheet").Range("D5, D11, I3, I12") '<--| change both "CoverSheet" name and its cells address list to fit your needs
shtNames = Array("firstSheet", "secondSheet", "thirdSheet") '<--| change sheets names to paste cellsToCopy values into
For Each shtName in shtNames
Worksheets(shtName).Range(cellsToCopy.Address).Value = cellsToCopy.Value
Next shtName
End Sub
I have written a macro to change a sheet name depending on what a user inputs. This works fine, my problem is that usually when you change a sheet name manually, the formulas that relate to the changed sheet all update.
This isn't happening when the macro changes the sheet name. Is there anything I can do to make this happen?
Since the sheet you are renaming is not the active sheet, you will need to reference the new sheet in your macro. Try this:
Sheets(NewName2).EnableCalculation = False
Sheets(NewName2).EnableCalculation = True
So I do this with formulas/advanced lookups embedded in a template sheet using the following two-step process:
Sheets("Template").Copy After:=Sheets(Sheets.Count) 'this is putting it at the very end of my document based upon the count of tabs
Sheets(Sheets.Count).Name = sName ' we then rename the sheet, because you want it created, then you want to rename it, as it will then allow for all formulas to refresh.
Im wondering if it's possible to reference an excel sheet from another work book without making a copy of that sheet?
The situation : I have some very large worksheets filled with various data, but i don't want to keep a copy of them open in my workbooks because while each workbook uses the same data source, they're slightly different.
I have a vba routine that takes this data and creates input files for other codes, vba expects this data to be available on the defined sheet names.
Is it possible to make either excel or vba to know that when i request worksheet("Example_1") it instead knows that i mean example_1 from a different workbook?
Thanks
Yes, it is possible.
You need to add those lines to your code:
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkb = Excel.Workbooks("name_of_workbook.xlsx")
Set wks = wkb.Worksheets("Example_1")
Now, every time you want to refer to a range from this other workbook, you need to add wks. before, i.e.:
'Printing value in cell.
wks.Range("A1") = "x"
'Selecting range.
call wks.Range(wks.Cells(1,1), wks.Cells(2,2)).Select
=SUM('C:\test\[test.xlsx]sheet_name'!A1:A25)
is an example of a formula which references sheet sheet_name in workbook C:\test\text.xlsx.
Note that when the other workbook is opened, the formula automatically changes to
=SUM([test.xlsx]sheet_name!A1:A25)
and then when it is closed, the formula will change back.