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.
Related
I'd like to run a macro that's reading all sheet names from an XLS file, and then being able to run the macro from the Run Command.
How is it possible?
For the moment I have the following code but I'd like to generalise it to other files (basically inputting the filename as a parameter in this macro and command).
Sub FnGetSheetsName()
Dim mainworkBook As Workbook
Set mainworkBook = ActiveWorkbook
For i = 1 To mainworkBook.Sheets.Count
'Either we can put all names in an array , here we are printing all the names in Sheet 2
mainworkBook.Sheets("Sheet2").Range("A" & i) = mainworkBook.Sheets(i).Name
Next i
End Sub
Create a macro enabled workbook.
Use one of the cell in Sheet1 to specify the path to excel file whose sheet names you want.
In Sheet1, insert an Active X Control button using developer mode.
In the code behind of the button (double click on the button keeping design mode enabled in developer tab -> VBA window opens), add following VB code and save it:
Private Sub CommandButton1_Click()
Dim sheetCount As Integer
Dim mainWorkBook, workbook1 As Workbook
Set mainWorkBook = ActiveWorkbook
Set workbook1 = Workbooks.Open(Range("A2"))
'Clear the contents of sheet 2, starting from row 2 - verify the last cell in sheet
Selection = mainWorkBook.Sheets("Sheet2").Range("A2:ZZ104857").ClearContents
If mainWorkBook.Sheets("Sheet1").Range("A2") = "" Then
MsgBox ("Enter Excel path")
GoTo exit1
End If
For sheetCount = 1 To workbook1.Sheets.Count
'Put sheet names of excel file in sheet 2 of macro book - you can find sheet names in Sheet2 starting from A2 cell.
mainWorkBook.Sheets("Sheet2").Range("A" & sheetCount + 1).Value = workbook1.Sheets(sheetCount).Name
Next sheetCount
exit1:
workbook1.Close (False)
End Sub
In the Sheet1 (of macro workbook), enter the excel path (whose sheetnames you want) in cell A2.
Click the Active X button (keeping design mode disabled).
You can find the result in Sheet2 of the macro workbook (Add suitable column names in first row of Sheet1 and Sheet2 of macro workbook for readability, if needed).
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
i can do simple row deletions etc in single sheets in VBA, but this one has me stumped.
I need to search for ANY data (likely to be an email address, but in reality could be any text or number) in N1:N100, across all open worksheets...except Sheet1 (i.e. from Sheet2 to whatever the last open worksheet is)...
...if any data is found in the N1:N100 range for that particular worksheet (i.e. Sheet2) then do nothing and search the next worksheet (i.e. Sheet3)...if no data is found then enter "NONE" in cell N1 (on i.e. Sheet2) and then move onto the next worksheet (i.e. Sheet3).
Ive seen IF/ELSEIF/THEN code from other people, but it all seems to be sheet specific...and as i said im a bit out of my depth with this particular part.
This will feed into a larger bit of VBA code that ive got thats already spread over several sheets of A4, thought i had worked out all the bugs by now :D
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
' Insert your code here.
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub
Or you can loop over all of the worksheets using a For Each loop:
Sub WorksheetLoop2()
' Declare Current as a worksheet object variable.
Dim Current As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
' Insert your code here.
' This line displays the worksheet name in a message box.
MsgBox Current.Name
Next
End Sub
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
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.