reference a cell but keep formatting of text from source cell - excel

I have a cell with some text in it.
Say cell A1.
This text is formatted in a certain way - First few words are bold, line breaks, varying font size, etc
When I reference this cell, say in cell B1:
=A1
In B1 I just get a long string of text that has none of the formatting that is present on A1
Is there a way to reference and keep the formatting?
I can use format painter and it will recognise the line breaks within the cell, but aspects like the partially bold writing are still not recognised.

As per my comment:
Private changing As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Address = [A1].Address Or changing Then Exit Sub
changing = True
[A1].Copy [B1]
changing = False
End Sub
The code above means that whenever any change is made to cell A1 (specifically A1 - that way the code doesn't execute every time a change is made on your sheet - doing that would slow everything down), whatever's in A1 is copied to B2
Usage
To use, simply
Right click on the name of your sheet (a tab along the bottom), and select "view code"
Paste the code in
Change any cell references to the ones you want (ie replace[A1] with [A3] or [A1:A4] or [blah] (i.e. a named cell/range) depending on what you need)
Close the window. To save the workbook you'll have to save as .xlsm or .xlb if you want to keep the macro
Notes
[A1] is shorthand for ThisWorkbook.ActiveSheet.Range ("A1"). Typically I would advise against using it as ActiveSheet means that if the code ran on any sheet in ThisWorkbook, it would copy and paste over the A1 and B1 of that sheet, whereas you probably only want the specific cells on a specific sheet.
However in this case, the code only applies to 1 sheet so that's not a problem.
All the changing stuff is necessary because copy/paste triggers a change event; i.e. the macro triggers itself over and over until Excel stops it - not ideal! The changing variable which I declare simply acts as a signal to stop the program executing itself.

Related

Excel formula to copy cell value without keeping reference to source cell

In my excel sheet, I need to copy the value of a cell A1 to another cell B1. When I change the value in cell A1, B1 should keep its original value.
Basically what would happen if you copy > paste special > Values
However, I cannot do this manually with mouse or keyboard input. I also cannot use macros/VBA.
Does a formula exist in excel that accomplishes this same task?
I tried playing around with =VALUE(A1) and =concat(A1) but these formulas all contain references to cell A1 and the result changes as soon the value in A1 changes.
As already mentioned in the comments, what you are asking can be done but at the cost of an error message and without the possibility of further calculations of the 'copied' cell, by letting the 'copying' cell refer to itself.
As shown in the screenshot above, the value returned in F3 is the value of E3 or itself, depending on the selection in F5.
Selecting "No" (or anything else but "Yes") in F5 and thereby having F3 refer to itself will show an error message, but the value it had will stay, even when saved and closed. However, no further calculation can be conducted on that cell:
Lastly, this solution is probably unstable.

Excel named range formula, for cell sequence

I'm trying to set up a sheet that will input data in a specific cell order. Basically I need the first enter to go to the next column, and then the second enter to go to the previous column but one row down.
I linked a picture to elaborate. I thought it would be possible with just defining a named range and selecting each cell individually, but I need for this to work for 500+ rows.
It's my understanding you can use a formula to define a named range, but I'm having difficulty figuring out how to do that.
edit:
More elaboration on the input method. In this case I can only use the enter key due to the input device. This sheet also has to be shared with people, so setup instructions are not ideal.
Based on the question as it stands (and without further qualification), the following code will do what you want:
Option Explicit
Private Sub Worksheet_Change(ByVal rgTarget As Range)
With rgTarget
Select Case .Column
Case 1: .Offset(0, 1).Select
Case 2: .Offset(1, -1).Select
End Select
End With
End Sub
The above goes into the VB of the Sheet where the data entry will occur.
If you don't know what that means, let me know and I'll provide details on how to implement.
Excel recognises data entry patterns when data is entered manually.
enter something into A1 and press Tab to confirm and go to B1
enter something into B1 and press Enter.
now the active cell is A2
enter into A2 and press Tab to go to B2
enter into B2 and press Enter to go to A3
etc.
No need to set up anything special or create named ranges for that.

Dynamically update a single cell with contents from a fixed column reference, but from the current, active row

I am trying to have a certain header cell in excel dynamically update its contents depend on which row I am currently using, but with an address that has a fixed column. So the column reference for the header's contents will always be the same, however, I would like to have the row address change depending on the cell I am editing (i.e. the active cell).
A B
1 (dynamic header cell)
2 John likes to eat apples
3 Mary never smokes
4 Peter tries too hard to be cool
5 David loves madonna
So, If I click on cell A2 ("John"), I want the header cell (A1) to update with the contents of B2 ("likes to eat apples"), and similarly if I click on cell A4 ("Peter"), I want the same header cell (A1) to update with the contents of B4 ("tries too hard to be cool").
I have done some research, and I think perhaps I might use some combination of the cell function or the indirect function, but I cannot manage to get this to work. I would prefer to use a simple formula, but if I need to do VBA, that is fine.
(If you suggest VBA, please include the whole function, because I don't know the language).
Can anyone help?
As Scott Craner suggests, you'll need the "Worksheet_SelectionChange" event in VBA. Since you indicate not being familiar with VBA, I'll talk you through it. First, open the VBA editor by entering Alt+F11. In the VBA editor, enter Ctrl+R to open or jump to the project explorer (it's usually a pane on the left side of the window) and double-click the name of the sheet where you want your function. Now enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not(Intersect(ActiveCell, Range("A:A")) Is Nothing) Then
Cells(1, 1).Value = Cells(Selection.Row, 2)
End If
End Sub
The first line tells VBA it should execute the code whenever the selection changes. The second line checks if your selection is in column A. The third line actually does the trick: it takes the value from the second column of the selected row and puts this value in A1.

Get value of a merged cell of an excel from its cell address in vba

How to get the value of a merged cell of an excel having range address like "$B$4:$B$11" in vba
Even if it is really discouraged to use merge cells in Excel (use Center Across Selection for instance if needed), the cell that "contains" the value is the one on the top left (at least, that's a way to express it).
Hence, you can get the value of merged cells in range B4:B11 in several ways:
Range("B4").Value
Range("B4:B11").Cells(1).Value
Range("B4:B11").Cells(1,1).Value
You can also note that all the other cells have no value in them. While debugging, you can see that the value is empty.
Also note that Range("B4:B11").Value won't work (raises an execution error number 13 if you try to Debug.Print it) because it returns an array.
Josh Brown gave (in a comment) what I think is the best answer:
When I don't know the bounds of the merged area, I get the value with
Range("B6").MergeArea.Cells(1,1).Value
This is helpful in VBA when, for example, you are looping through files that might have merged cells of unknown ranges, so you can be much more general with this method. Thanks Josh!
This can be done in 2 steps:
First name the range of the merged cells; highlight the merged cell then go on the Ribbon Bar: Formulas Tab --> Define Name;
Make sure there are no spaces in the name. Example: defined_Name. Go to the desired cell you wish to see the output/result. In that cell, type: =defined_Name.
Press enter because your done.
(Excel 2016)
I am hoping this will help somebody.
I have found that if you press delete on a merged cell,
the target.cells.count = the number of merged cells.
Whereas if you change the value on the same cell, the target.cells.count = 1
This caused me issues as I was skipping multi selections in my worksheet_change code by testing the number of cells selected.

Is there a way to automatically fill cells in a row when it is added in Excel

I don't really know much about excel especially VBA. But i need to write something(macro for ex.) that does the following:
if someone inserts a row and fills the first 4 cells of it, it needs to sort the rows (i have a macro for sorting called "sortingmacro") and then it needs to fill cells 7 and 8 by copying the cells above them.
for example if i insert a row to 4th line and fill A4,B4,C4 and D4 it needs to copy G3 to G4 and H3 to H4. (after sorting of course)
any help is appreciated.
edit:
i have formulas in the cells that are to be copied. the formulas for the first row(the top one according to sorting) is a different one and all the others are the same.
You can find out if a cell has been changed with the Worksheet_Change-Event.
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
Output (something like this):
$A$14
$B$17
$C$13
$C$21
$C$16
So if a matching cell is changed, look for the other 3 in the row and start your macro.
So put a button on the page, then go to the code for it (the click event). In that, use your sortingmacro to sort all the rows. Then you just do something simple like:
Rows(4).Cells(1,7).Formula = Rows(3).Cells(1,7).Formula
Rows(4).Cells(1,8).Formula = Rows(3).Cells(1,8).Formula
Of course instead of hardcoding 4 in there, you'd have a variable for the row (make sure and determine it after sorting).

Resources