I can usually find all the answers I need through Google searches. This one has me stumped; maybe it isn't possible.
My sheet is set up with B5 as its first unfrozen cell. How can I enter a formula in cell A3 that refers to the first cell that is currently showing in the top right pane? So in the worksheet's initial state, A3 would refer to B3, but if I scrolled right 10 rows, for instance, A3 would then refer to K3.
Excel uses cell references in formulas. A cell reference does not change when you scroll. You would need VBA to determine the top left cell of a pane. This cell value can be written into a helper cell and you could reference that helper cell in the formula in the frozen pane.
The VBA would run as a Selection Change event, i.e. when the user clicks in a different cell or uses the keyboard to navigate. It would not work for scrolling with the mouse wheel, since that is not an event in the Excel object model. (Chip Pearson has some code for that, though, here).
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer, pane As Integer
Dim PaneTop As String
pane = ActiveWindow.Panes.Count
PaneTop = ActiveWindow.Panes(pane).VisibleRange.Address
i = InStr(PaneTop, ":")
PaneTop = Left(PaneTop, i - 1)
Range("A3").Formula = "=" & PaneTop
End Sub
This function puts a cell reference to the top left cell of the pane into A3. Change it to include your formula, or write it to a different cell and let your formula refer to that cell.
Related
In Excel, I would like to select a cell and then copy the contents to n number of cells below. Instead of using the "fill" drag option since the number of rows will be fairly large and require scrolling and then stopping at the correct cell I was looking for other options.
I am currently doing the the following:
In excel I select a cell and then on the top left corner it shows the cell (i.e. A1). Then to select the number of cells below it, I modify the top left box to a range such as A1:A10 which selects the range of cells. See attached image. I then use the shortcut key "Ctrl-D" which copies the first cell to the other cells.
Is there a way instead of mentally calculating the ending cell number, I can do something that effectively works "A1 + 10" in this box to select 10 cells below to select the A1:A10 range?
How about this way? Enter your formula in cell 1. Enter the number of copies (including the original) you want of it in the cell below it. Double-click on the number you entered and the formula is filled down.
To try, install the procedure below in the code module of the worksheet on which you want the action. (That's a module Excel set up when you created the sheet. Don't install the code in a standard code module that you have to insert yourself.)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const TriggerClm As String = "A"
Dim R As Long
With Target
If (.Column = Columns(TriggerClm).Column) And (.Row > 1) Then
R = Val(.Value)
If R Then
With .Offset(-1)
If .HasFormula Then
.Resize(R).FillDown
.Select
Cancel = True
End If
End With
End If
End If
End With
End Sub
You may wish or have to tweak the code. For example, the action is limited to column A. Change the constant at the top of the code to identify the column you prefer, or change the limits to better suit your requirements. The code also checks if Cell #1 contains a formula. If that isn't what you want to fill down that condition will have to be revised.
Last, but not least, the code selects Cell#1 (the one that contained the number is over-written). That may not be the best choice. You can modify the code to select a place in your worksheet where you will continue working.
A bit of context:
I recently discovered that the following formula returns the address of the cell that is currently selected (or if a range is selected, returns the address of the upper-left most cell in the range):
= CELL("address")
At first, I thought this formula would be useful for conditional formatting, since it could be used as part of a condition to only format the cell that is selected (e.g. the conditional formatting rule could be something like = CELL("address")=ADDRESS(ROW(),COLUMN())), but I am facing an obstacle.
The formula is volatile, but volatile functions only update either when:
A cell in the worksheet is changed
F9 is pressed on the keyboard
All that said, my question is: Is there a way to have a cell automatically recalculate whenever a different cell is selected with a mouse click? Even volatile cells won't update from this condition, because selecting a different cell, in itself, won't cause any data in the cells to change.
Of course, it could be updated manually by pressing F9 after selecting a different cell, but I am wondering if there is a way to automate this.
You can use the Worksheet_SelectionChange() event in VBA.
Open your VBE (Alt+F11), find your workbook in the VBAProject pane (upper left) and double click your worksheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Force this cell to recalculate any time any cell is selected/highlighted
Range("A1").Calculate
End Sub
Now anytime moves around on the worksheet Cell A1 will recalculate.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1:D4"), Target) Is Nothing Then
Range("A1:D4").Interior.Color = xlNone
Target.Interior.ColorIndex = 6
End If
End Sub
This will now highlight the cell chosen only if the cell chosen is in A1:D4
Here is the problem:
My table is very large and the column width is not enough to show all the text in it.
Since the text/value is generated by a formula in the cell, if I click on the cell, the Formula Bar will display the formula and not the value. Of course.
However, I think it is very useful to quickly know the content of the cell generated by the formula. If I expand the column width every time is not quick and clean.
Do you know if there is a way to solve the problem?
No there is not a way to show it in the formula bar if there is a formula in the cell.
The best thing I can suggest is to leave row 1 empty and merge A1:N1 together or similar, ,freeze row 1 if necessary
then put this formula into A1
=INDIRECT(CELL("address"))
When you select a cell on the sheet , press F9 on the keyboard and you can read the full value of the active cell in row 1
With a little VBA in the sheet you can also have it automatically update the cell
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Calculate
End Sub
Here's another solution. Simply assign a key to the following macro:
Sub DislayCellValue()
Dim outp As String
outp = "The value in the active cell is:" & Chr(13) & Chr(13) & ActiveCell.Value
MsgBox Prompt:=outp
End Sub
To display the value in a cell, click on the cell and hit Ctrl-(key).This might be helpful.
I have created a simple excel formula to know what is the color of the font of the cell A1
Function GetFontColorIndex(elrango As Range) As Integer
Application.Volatile
GetFontColorIndex = elrango.Cells(1, 1).Font.ColorIndex
End Function
In excel-2010:
File > Options > Formulas > Workbook Calculation > Automatic is checked
In cell A1 I have a number, and in cell A3 I have
=GetFontColorIndex(A1)
but when I change the font of cell A1, the formula does not update automatically on cell A3. I have to click shift+F9 and then it works.
Any idea why is not showing the number of the font automatically?
As simoco has mentioned.
If you are using your 'udf' in Sheet1 then in the module associated with that sheet add the following:
If you just change the colour the formula does not update but as soon as you press enter or select another cell in the sheet it updates
My scenario is , I have one Excel sheet. On the same sheet in cell A2 I am inserting the data through code and cell B2 I already put the data manually before inserting the data in cell A2, in cell C2 I written the formula =EXACT(A2,B2).
Now when I run the code, the code put the value in cell A2,and the Status should be change according to the condition given and the data on both cells. But it doesn't change the status according to the formula in cell C2 and the status gets changed when I double click on that particular cell and enter then the condition work. Please tell me how it will be possible without double click on the particular cell?
This works (I've tested it) if automatic calculation is switched on (Excel Options->Formulas->Calculation Options).
Here's the code (I'm using A1, B1, C1):
Private Sub CommandButton1_Click()
Cells(1, 1).Value = 2
End Sub