In excel 2010, I want to copy sheet 1, including all formatting and data, page setups, page breaks etc onto sheet2. In summary I want to do an exact copy of sheet1 on sheet2 through to sheet7. Ideally I want to insert a command click button on sheet1, so that once I have finished inputting data I can click the button and duplicate the information on each sheet. Any ideas?
Try this ,
Sub test()
Dim i As Integer
For i = 2 To 7
Sheets("Sheet1").Cells.Copy Sheets("Sheet" & i).Cells
Next i
End Sub
As an alternative solution I think you should create a button and assign the code below to it.
This will select all the sheets while you're on sheet1 and then whatever changes you make to sheet1 will automatically be available on all sheets instantly.
Sub test()
Dim ws As Worksheet
For Each ws In Sheets
If ws.Visible Then ws.Select
Next
End Sub
Related
Let's say I have 10 sheets in an Excel workbook and Sheet2 through Sheet10 are uniquely formatted.
Let's say I recorded 9 unique macros.
Sheet1 is a central location to house nine buttons. One button for each recorded macro so that when the user goes to sheet 1 and clicks a button called "sheet 2" it will run the macro for sheet 2 against sheet 2, even if the user is on the active sheet 1. Or if the user clicks the button called "sheet 10" it runs the recorded macro for sheet 10 against sheet 10, etc.
Here is a simplified version of one of the macros.
Sub Sheet2()
'
' Sheet2 Macro
'
'
End Sub
Sub Sheet2Macro()
'
' Sheet2Macro Macro
'
'
Range("A2:C2").Select
Selection.AutoFill Destination:=Range("A2:C10"), Type:=xlFillDefault
Range("A2:C10").Select
End Sub
I found this code online that I added at the top and it runs to success but only on the active sheet:
Dim WkSheets As Variant, SheetName As Variant, ws As Worksheet
'** SET The Sheet Names - MUST Reflect Each Sheet Name Exactly!
WkSheets = Array("Sheet 2")
For Each SheetName In WkSheets
'MsgBox SheetName
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = SheetName Then
'Your Code Here
If you are using 9 unique macros that you recorded, those will be set specifically for the sheets they are recorded from, and then you simply tie in the code to be called when the button for each specific one is clicked. As stated before it would be helpful to know if they were Form Controls or ActiveX Controls, but you would change the text in your button to say "Sheet 2 Button" for instance, and the code tied to that button would be something along the lines of
Private Sub Sheet2Button_Click()
Call Sheet2Macro
End Sub
So from there when you click the button that says Sheet 2 Button, it will call Sheet2Macro and that macro was recorded on Sheet2 so will run for Sheet2 no matter where the button is stored. Hopefully that makes sense.
I am new to using VBA. I have an Excel document that has 500 sheets worth of data each sheet only has at most 45 rows and up to column W. I am trying to have a macro that copies the data from each of the 500 sheets and pastes it into a sheet named "Master". I am able to use the following code to perform this successfully.
Sub CopyPaste()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Master" Then
wks.Range("A1:W" & wks.Cells(Rows.Count, "A").End(xlUp).Row).Copy _
Destination:=Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
The issue I am running into is that some of the sheets have a row that is blank below the header before the data begins and as my code is written currently it seems like the macro thinks this is the end of the page and moves to the next sheet and is missing data that should be copied and pasted. I am looking for some assistance/guidance on how to account for these blank cells/rows so that the copy and pasting continue for the sheet through the end. Any help would be much appreciated, thank you.
I have the following simple Excel spreadsheet:
A
1 Sheet1 =MID(CELL("filename",Sheet1!K1),FIND("]",CELL("filename",Sheet1!K1))+1,255)
2 Sheet2 =MID(CELL("filename",Sheet2!K1),FIND("]",CELL("filename",Sheet2!K1))+1,255)
3 Sheet3 =MID(CELL("filename",Sheet3!K1),FIND("]",CELL("filename",Sheet3!K1))+1,255)
4 Sheet4 =MID(CELL("filename",Sheet4!K1),FIND("]",CELL("filename",Sheet4!K1))+1,255)
5 Sheet5 =MID(CELL("filename",Sheet5!K1),FIND("]",CELL("filename",Sheet5!K1))+1,255)
6
7
In Column A there is a list of all sheets in the Excel file. I list the sheets using the formula that you can see next to it.
All this works fine so far.
Now it can happen that some sheets in my spreadsheet are invisible (hidden). In this case I want that those sheets do not appear in the list above. Therefore, I wonder if there is a formula that can identify if a sheet is visible or not. Something like this:
IF MID(CELL("filename",Sheet1!K1),FIND("]",CELL("filename",Sheet1!K1))+1,255) = Invisible THEN ""
Do you have any idea how to solve this issue?
I don't really understand the purpose of this list... it seems like you're just duplicating the list of worksheet "tabs" shown at the bottom of the screen.
I sense something being made more complicated than necessary; either an XY Problem, or some duplicate data that you're trying to manage without reorganizing it. :-)
Regardless, based on my understanding of your question, there are no built-in functions to do what you need, but these VBA examples should give you some ideas:
This procedure lists all visible worksheets, in the Immediate Window (Hit Ctrl+G from VBA to view it):
Sub ListSheets() 'list in immediate window
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Visible = xlSheetVisible Then Debug.Print sht.Name
Next sht
End Sub
This procedure is similar excep lists them on the active worksheet, leaving blanks for hidden sheets (like your example would imply):
Sub ListSheets() 'list on worksheet
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Visible = xlSheetVisible Then Range("A" & sht.Index) = sht.Name
Next sht
End Sub
This function can be called from a worksheet formula and lists the name of the worksheet you specify by index number. It returns "" (empty string/nothing) if the worksheet is not set to Visible, or if it doesn't exist.
Function listSheet(shtNum As Long) As String
On Error Resume Next
If Sheets(shtNum).Visible = xlSheetVisible Then listSheet = Sheets(shtNum).Name
End Function
Related note:
There are three types of worksheet visibility.
More Info:
MSDN: Worksheet.Visible Property (Excel)
I am completely new to VBA script. My objective is to copy a row of data from one workbook from a hidden Sheet name "Tracking" and post the row into another workbook sheet name "PDOTracking"
I want the user to point to the row to be pasted and click paste, but when I do this each time after the first, the data is shifting to the left and falling out of line with the columns.
This is my script
Sub CopyRow()
ActiveWindow.ScrollWorkbookTabs Sheets:=1
Application.ScreenUpdating = False
Sheets("Tracking").Visible = True
Sheets("Tracking").Select
Range("$A$6:$Hy$6").Copy
Sheets("Tracking").Visible = False
End Sub
Keep in mind, when copying, I do not want the user to see the sheet being copied from, hence I want it to stay hidden.
Its the paste step that is an issue.
Try,
Sub CopyRow()
dim dr as long
dr = activecell.row
workSheets("Tracking").Range("$A$6:$Hy$6").Copy _
destination:=activesheet.cells(dr, "A")
End Sub
If you don't want the user to see the sheet, don't make it visible. There is nothing from stopping you from copying data from a hidden worksheet.
BTW, to remove the worksheet from the list of hidden worksheets that the user can make visible, make the Tracking worksheet xlsheetveryhidden.
worksheets.visible = xlsheetveryhidden
Is it possible to let Excel automatically select the first empty cell in column A, whenever I open the document?
I have got the following to find the the first empty line:
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Value + 1.Count, 1).End(xlUp).Value + 1
In order to get Excel to select the first empty cell in column A when you open your workbook, you need to place the following code in the ThisWorkbook module.
Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
End Sub
This will select the first empty cell in the ActiveSheet. If you got multiple sheets in your workbook and you want to select the first empty row in a specific sheet, say Sheet1, you should change the second line of code to:
Set ws = ActiveWorkbook.Sheets("Sheet1")
You can do that.
You need write VBA(macro) program to realize.
Code you need is as follow
Private Sub Workbook_Open()
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select
End Sub
Meaning of code is:
"Private Sub Workbook_Open()" is predefined name subroutine which will be executed when the workbook be opened.
"ActiveWindow.Range("A65536").End(xlUp)" will find last cell with data in A column ("last cell")
"ActiveWindow.Range("A65536").End(xlUp).Offset(1,0)" will move to cell next to "last cell", that will be first blank cell.
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select will select tha first blank cell.
I assumed that you use Excel 2003 or older, OR number of rows with data in your worksheet is less than 65536.
If you use Excel 2007 or newer and you have rows with data in your worksheet more than 65536, please modify 65536 to the value large enough to cover rows in your worksheet.