I am new to VBA and I want to write an open event which shift the excel window to a particular column when ever I open the excel.
I am able to scroll the window with the help of module and button, the code used by me is:
Sheet9.Activate
ActiveWindow.ScrollColumn = temp
Here temp is a variable whose value changes.
When you Open the Workbook, it triggers the Workbook_Open event. Alternatively, when you Activate a Worksheet, it triggers the Worksheet_Activate event.
To scroll the screen to show a specific Cell on the Worksheet, you can use the Range.Show Method. For example, to show Cell F10 on the ActiveSheet, you can use either of the following lines:
ActiveSheetRange("F10").Show
ActiveSheet.Cells(10, 6).Show
If you want to scroll to a Column without changing the Row, you will need to use Window.ScrollRow to work out which cells are on screen, like so:
ActiveSheet.Cells(ActiveWindow.ScrollRow, 6).Show 'Scroll to the 6th column, Column F
Related
I need a formula in MS Excel to get the last updated timestamp everytime any cell in a particular row is updated.I am using =if($2:$2="","",NOW()) but when one cell is updated, every cell even of other row having similar formula is updated with the latest time-stamp. What can be the problem?
I need only formula not a macro/excel program.
You will need to create a VBA macro in the worksheet change event. You can easily input a timestamp as a string into your row depending on the row of the changed cell. There is no formula that can do what you're asking.
To get to the worksheet change event, go to File, and then Options. Then go to Customize Ribbon. Check the box next to Developer in the column on the right. Under the newly added Developer tab in the Ribbon, select Visual Basic on the far left. Find you worksheet in the Project window on the left hand side of the Editor that opens up. You may need to expand your Workbook and a folder called Excel Objects. Double click on the sheet to get to its code. A window will open with two drop downs at the top. On the left, go to Worksheet. On the Right, select Change. In between Private Sub Worksheet_Change(ByVal Target As Range) and End Sub, you can add the code that will run whenever the worksheet is changed.
This is the code you would use, replacing "5" in Cells() with the column you want to put a timestamp in.
Dim i As Integer
i = Target.Row
Dim t As String
t = VBA.Now
Cells(i, 5) = t
If the cell to contain the timestamp is B3 and the cell to watch is C3 the formula is:
=IF(C3<>"",IF(B3="",NOW(),B3),"")
I want a macro to run automatically everytime I navigate to a specific worksheet. I know this is possible because there is another macro on the worksheet that updates information automatically, but I can't seem to figure out how to set it so that my new macro will run automatically. Note all of the macros are in the same folder "Modules."
I think this might be along the lines of what you need. If you are in the VBA editor, on the left navigator pane, double click the sheet you are interested in having run the code automatically. In the window that opens, on the upper left change the drop down to "Worksheet". Then in the upper right drop down, change the event handler to "Activate"
You should see a new blank sub generate:
Private Sub Worksheet_Activate()
'put some code here
End Sub
Now anytime you activate that sheet, any code you enter in that sub will be run automatically. You can look at the event handlers in the right drop and try other ones if they suite your needs better.
I am using =Hyperlink in a series of cells in a worksheet to call a public function that changes the value of a particular cell. It works very well. But I want the function to hide a row when I mouse over a particular cell. Can anyone help?
The code is:
Public Function highlightcell(seriesName As Range)
Range("valSelOption") = seriesName.Value
'enter code here to hide Row 1
End Function
You could try
Rows("1:1").RowHeight = 0
As an easy way to hide row 1
Simple Answer
There is no On Mouseover event in Excel.
This thread shows how you can achieve something similar using selection change, but it wont work for what you want.
http://www.ozgrid.com/forum/showthread.php?t=147219
Hiding a Row
Use this code:
ActiveSheet.Rows(2).Hidden=True
This will hide row 2.
Explaining Events in Excel
If you want the worksheet to react to the user (clicking a cell, changing a cell, calculating the sheet, opening the workbook, etc) you want to use Excel Events. This is a good explanation of Excel Events:
http://msdn.microsoft.com/en-us/library/office/hh211482(v=office.14).aspx
ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).
I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.
I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?
And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.
To reproduce this bug, do the following:
Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
Close the properties window, and double click on the combobox to define its change() event.
Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
Exit design mode
important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.
Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.
My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox
Private Sub Workbook_Open()
Sheet2.ListBox1.Clear
Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value
End Sub
Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex >= 0 Then
Sheet2.Range("A1").Value = Me.ListBox1.Value
End If
End Sub
I have a few options available that I am aware of thus far. The best I can come up with is this:
Avoid directly using formula-based named ranges. Instead, define a subroutine that will check whether the defined range "demoRange" should be changed from what its current value is. Run this subroutine on the workbook_open and sheet3_deactivate events. If needed, prompt the user to ask if it's all right to update the named range. [edit] The macro that updates "demoRange" could probably just copy from a "demoRange_FormulaBased" named range into "demoRange" which would be static. [/edit]
This solution works well because you can keep using the linkedcell property, you don't have to use VBA to populate the comboboxes, and the named range can still be used for whatever other purposes it already had. Avoid using the onchange event to run this new subroutine, since it might end up being triggered thousands of times if a user opens the Find/Replace dialog and chooses "Replace All".
I need to copy a set of rows from one tab to another tab of the same Excel document by just clicking a button.
Also, can I also get information on how can I copy a set of rows that are hidden and paste it in the same tab without copying the "hidden" format?
If 'Copystart' is your original rows, and 'Copyend' is where you want to paste them, then using named ranges:
Sub Copybutton_Click()
Range("Copyend").value = Range("Copystart").value
Range("Copyend").visible = True
End Sub
If you have multiple named ranges with the same name, then add [Sheetname]. in front of the range, where Sheetname is the name of the sheet that the named range is in that you want to reference.
There are no native functions in Excel that will allow you to do this. You will need to write a macro and assign that to a button control (which you can drop onto your worksheet by using the Control Toolbox toolbar - View > Toolbars > Control Toolbox).
You would usually then assign the macro to that button by double-clicking the button (while it's still in Design View) and calling your macro in the newly-generated `CommandButton_Click` event. As Lance says, named ranges would be the easiest to work with.
To answer the last part of your question, programmatically copying a range doesn't copy the formatting or formula as well. It only takes the value of the cell. So regardless of whether your source range is hidden, the destination will not need to have its `visible` property explicitly set - the hidden attribute is ignored when copying.