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.
Related
I have an excel workbook which I am creating with the help of Alteryx. Alteryx populates the cells with values instead of formulas. Now when I open the workbook, I want just one of the columns in Sheet 1 to be populated with formulas (not just pasted values from alteryx). How do I do that using MACROS?
Try this, with the event "Workbook_Open".
the below code will insert when you open the workbook, the formula =SUM(D2,E2) in column A from row 1 to 20
Private Sub Workbook_Open()
Dim CheckRange As Range
Dim x As Range
Set CheckRange = Range("A1:A20")
For Each x In CheckRange
x.Formula = "=SUM(D2,E2)"
Next
End Sub
I'm trying to add some simple VBA functions to excel files at work (i work for a marketing company) and am struggling to code a button that can copy data from one sheet to another (the position it needs to copy to changes daily)
I'm using VBA to try and select the range of values i need copied (and it works) but i'm struggling to paste them in the daily-changing cells required.
I've tried writing a formula that presents a pathway to the correct cells and then introducing an indirect function to the VBA code for pasting but i can't seem to get it to work.
Sub Gross_Wrap_Click()
Sheets("AUTOM").Select
Range("C1:C5").Select
Selection.Copy
Sheets("Daily").Select
Range([indirect("'AUTOM!O7'")]).Select
Selection.Paste
End Sub
I expect the end product to be the contents of cells C1:C5 in the AUTOM sheet copied and pasted to the required cells in the DAILY sheet (the required cells are derived from a function and included in VBA with an Indirect pathway).
However, i'm just getting a load of different error messages
If I didn't messed it reading your code, this should do it:
Option Explicit
Sub Test()
Dim rng As Range 'Range object, means a cell or a range of cells
With ThisWorkbook 'here you reference the workbook, so you don't need to write Thisworkbook everytime, just "."
Set rng = .Sheets("Daily").Find(.Sheets("AUTOM").Range("O7")) 'we set the object rng to the cell in sheets Daily which has the Cell O7 from AUTOM sheet value.
.Sheets("AUTOM").Range("C1:C5").Copy 'copy the range from AUTOM
rng.PasteSpecial xlPasteValues 'and paste it's values to the range object we declared and defined above
End With
End Sub
I have an excel workbook that contains multiple sheets. In this workbook sheets occasionally are removed, renamed, or added. This workbook features a sheet (Sheet1) that is supposed to be used as a command center for printing copies of the other sheets in the workbook. Unfortunately, this printing command center does not work as intended.
The command center has only two columns. In column A is a list with the names of the other Sheets. In column B the user can specify how many copies to print of the respective sheet in column A.
This is the current code:
Sub PrintSheets()
Dim mysheets As Range
For Each mySheets In Sheet1.Range("A2:A100")
If mySheets.Offset(0, 1).Value <> "" Then Sheets(mySheets.Value).PrintOut Copies:=mySheets.Offset(0, 1).Value
Next mySheets
End Sub
The first two sheets are printed as intended and then I get a "Run-time error '9': Subscript out of range"
1) How can I fix the error?
2) Can the list with the sheet names be generated automatically and sorted by their position (i.e., left to right in the sheet tab -> top to bottom in the column)?
If you rename, add and remove sheets, you will have to update your list before printing. This sub will clear Range("A2:A100") and it will insert all sheets names in your first sheet. Make sure your "command center" sheet is the first one (or change the index reference to the name of that special sheet).
Sub LIST_SHEETS()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim i As Byte
ThisWorkbook.Sheets(1).Range("A2:A100").Clear
i = 2
For Each ws In ThisWorkbook.Sheets
ThisWorkbook.Sheets(1).Range("A" & i).Value = ws.Name
i = i + 1
Next ws
Application.ScreenUpdating = True
End Sub
I'm afraid you will have to figure out where to put the code. I don't know your needs, so maybe after renaming/adding/deleting all sheets, or maybe when Workbook opens or something like that. Try it!.
It appears that a name in the list of sheets, does not exactly match the actual name. You can use either of the two options below, to loop through each sheet
For Each sht in ThisWorkbook.Sheets
'Do Something
Next
Or
Dim sht as Worksheet
For i= 0 to ThisWorkbook.Sheets.Count
Set sht=ThisWorkbook.Sheets(i)
'Do something
Next
The second option is not reliable when the sheets are deleted (their integer index may not be sequential then) so I would prefer the first option.
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
Thank you in advance for the help with this question. I have a set of workbooks who have varying numbers of worksheets. On each worksheet, there are 6 cells of information that I need to have compiled onto one master worksheet in that workbook. The 6 cells of information are on the same page of every worksheet. My final output would be a table on one worksheet that has all of the data from the other worksheets in that book. I can do it manually but was hoping that a macro would help. I've tried using the activeworkbook.worksheet identifier but am stuck. Thanks for any help.
I was going to suggest the same thing #TheEngineer did because your question sounded too general and the only answer seemed to be for someone to code a complete solution for you.
However, reading a little closer, I think what you are asking for is the loop I've given you below.
Sub Macro1()
'
' Macro1 Macro
'
'
' 6 "working variables" to temporarily hold the values from each worksheet
Dim sCell1 as String
Dim sCell2 as String
Dim sCell3 as String
Dim sCell4 as String
Dim sCell5 as String
Dim sCell6 as String
Dim iTargetRowIdx as Integer 'counter to point at next empty row in target sheet
iTargetRowIdx = 1 'NOTE: change this if your 1st target row is not 1
For Each Sheet In ActiveWorkbook.Sheets
'avoid copying from the target worksheet itself
If Sheet.Name <> "<your target sheet name>" Then
'copy value from each of 6 cells to our working variables
'INSTRUCTION: replace each pair of "<row>" and "<col>" with row and column numbers of each of your 6 cells
sCell1 = Sheet.Cells(<row>, <col>) ' 1st cell to copy
sCell2 = Sheet.Cells(<row>, <col>) ' 2nd cell to copy
sCell3 = Sheet.Cells(<row>, <col>) ' 3rd cell to copy
sCell4 = Sheet.Cells(<row>, <col>) ' 4th cell to copy
sCell5 = Sheet.Cells(<row>, <col>) ' 5th cell to copy
sCell6 = Sheet.Cells(<row>, <col>) ' 6th cell to copy
'NOTE: if you want to see the value of what's in a cell while the macro is running, you can use Debug.Print, which prints to your Immediate Window (the little panel) at the bottom of the VBA editor) e.g.
Debug.Print (Sheet.Cells(1, 1))
'this prints what is in cell A1
'NOTE: Debug.Print is just a debugging tool to help you see what is going on inside your macro - you can safely remove the statement altogether
End If
'then put code here to paste those values to the next row in your target worksheet
'assumes you want the 6 values pasted to cols A to F in each new row
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 1) = sCell1
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 2) = sCell2
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 3) = sCell3
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 4) = sCell4
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 5) = sCell5
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 6) = sCell6
iTargetRowIdx = iTargetRowIdx + 1 'point to next empty row
Next Sheet
End Sub
This loops through every sheet in your workbook. Inside the loop, you do all the work you described. You need to edit the code for your specifics.
You didn't say if you want to run one macro for ALL workbooks or if just on one workbook. This macro runs for only one workbook and that is the workbook where you place this macro. You can put this macro in every workbook that you want it to work in OR you can post a separate question about how to run one macro on many workbooks.