I noticed that when I'm typing text into a cell that has word wrap off, if the text exceeds the width of the cell, Excel automatically turns word wrap on.
I don't think it happens all the time, and I haven't investigated exactly the conditions, but I found it annoying enough that I wrote a simple macro to solve the problem.
But if there's another approach, I'd be glad to know.
My macro uses the Change event:
Private Sub Worksheet_Change(ByVal Target As Range)
'Replace <...> with an actual condition if desired.
If <target cell is one I want to keep unwrapped> Then
Target.WrapText = False
End If
End Sub
The If statement is optional. I use it to restrict the macro to working in a particular column.
You could also limit it to Targets that are single cell ranges, but it didn't seem necessary in my situation.
Related
I have a worksheet containing a column of numbers. The column is formatted with a background color, number formats, etc. The column is unlocked. I protect the sheet manually by right-clicking on the tab and selecting Protect. In the Protect Sheet dialog, 'format cells' is unchecked. I interpret to mean that the user should not be able to format cells. Yet when the user pastes into the column, formats are pasted along with values.
If I protect the sheet in VBA using
sh.Protect UserInterfaceOnly:=True
I get the same result: formats are pasted. I do not specify AllowFormattingCells:=False because the default is False.
I have seen posts suggesting that formats can be restored by copying and pasting them from a shadow area. I have used this solution before I started protecting worksheets and found it overly complex. I had hoped this was something protection would handle. If there is a way to handle it, I'd like to do it in VBA.
This question is a little old, but I had the same one, and so have many people in the past. With a bit of browsing I came up with a solution which seems quite clean and appears to work. Am I missing something?
Private Sub Worksheet_Change(ByVal rngTarget As Range)
Dim vPaste As Variant
With Application.CommandBars("Standard").Controls("&Undo")
If Not .Enabled Then Exit Sub
If .ListCount < 1 Then Exit Sub
If .List(1) <> "Paste" Then Exit Sub
End With
vPaste = rngTarget.Value2
On Error Resume Next
Application.EnableEvents = False
Application.Undo
rngTarget.Value2 = vPaste
Application.EnableEvents = True
On Error GoTo 0
End Sub
This could go in Workbook_SheetChange, but not if you already have code in Worksheet_Change, because the worksheet event handler gets called before the workbook event handler. But this code can go in a module to keep things tidy.
There is no built-in protection option you can use to achieve your desired result.
The only thing that works in this case is the clunky workaround that you mention, i.e. use a Worksheet_Change event that ensures the correct format after a cell has been modified.
Since there are many different ways to paste content, i.e. via various menus, ribbon commands, keyboard shortcuts, etc., any VBA solution that tries to intercept pasting will become very complex, much more complex than the change event that restores the format to its original state.
Another option might be user education and training (so they know to paste values only), although user behaviour may be the toughest element to change in the whole scenario.
We all know that Excel has some counter-intuitive behaviours and it is, I believe, one of them:
When you select a range of few cells, starting your selection with the cell with data validation list and choose value from drop-down list: only one cell changes (the one containing drop-down list) instead of all selected.
Sometimes a few magic keyboard shortcuts such as CTRL+d, or combination of CTRL+' and CTRL+ENTER can fix this behaviour, but from my experience clients doesn't like to learn some new hacks, they just want to work everything in as simple way as possible.
I found even similar questions on SO e.g. here:
Adding same drop-down value to multiple cells simultaneously
I know that this is very simple code, but following few lines of code make my life easier, and I am sure this will help somebody too. Code in Worksheet module of course:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' MACRO FILLS THE WHOLE SELECTED RANGE
' WITH THE SAME VALUE USING DROP-DOWN LIST
' IN JUST ONE ACTIVE CELL
' change to false if all selected cells should be filled with value
Const FILL_VISIBLE_CELLS_ONLY As Boolean = True
' detecting if dropdown list was used
'
' I am using very clever solution by JvdV from SO
' ~~~~> stackoverflow.com/questions/56942551/
'
' If after edit we're in the same cell - drop-down list was used
' I know that may be also drag&drop or copy-paste
' but it seems no matters here.
' Warning! Should be add one more check if someone used
' 'accept OK character' next to formula bar, not implemented here.
'
If ActiveCell.Address <> Target.Address Then Exit Sub
' preventing error which sometimes occurs
If IsEmpty(ActiveCell.Value) Then Exit Sub
' fill a range or visible range with activeCell value
If FILL_VISIBLE_CELLS_ONLY Then
Selection.Cells.SpecialCells(xlCellTypeVisible) _
.Value = ActiveCell.Value
Else
Selection.Value = ActiveCell.Value
End If
End Sub
When you select a Range of more than one Cell, is is important to distinguish between the Active Cell (the single highlighted cell) and the Selection (the entire selected range, including the Active Cell).
Then:
Any Content (such as Values or Formulas) that you enter into the Formula bar is input into the Active Cell only, not the entire Selection.
Any Formatting changes you make are applied to the entire Selection, not just the Active Cell.
An exception is when entering an Array formula which applies to the Selection.
Since in this case you are making a change to content not formatting, it is applied to only the Active Cell.
When considering the above, it is not counterintuitive but entirely consistent with the design and operation of the software.
However, from a UX experience this may seem counterintuitive simply because it defies your expectation. This is kind of like a "customer is always right" type situation, which can be very frustrating for programmers, but is essential that it be understood. You can read more about the concept in a series of well-written articles on the topic at https://www.joelonsoftware.com/2000/04/10/controlling-your-environment-makes-you-happy/ (disclosure: the author of these articles happens to be integral to the development history of both Excel and SO, but it is linked here on merit). It was written more than 20 years ago and is still every bit as relevant today.
I have a workbook written by someone else and a particular cell is being changed by some code. I cannot figure out the code that is changing the cell value.
I cannot even figure out a strategy to narrow down the code possibilities apart from setting a breakpoint in hundreds of procedures. So I would be happy with strategy ideas.
I am very experienced using VBA in Microsoft Access and a little bit experienced using VBA in Excel.
EDIT
Gotta larf. I cannot even F8 throught the code by starting at the start. What I mean is..
The cell in question changes after I change a value (a year actually) in the worksheet. So I set up a subroutine and breakpoint as below.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub '<== set breakpoint here
Then I press F8 and no other code is entered by the debugger and yet the cell changes.
The problem may be because there is an awful lot of dynamic formula creation going on in the code. Not sure. Don't know enough about Excel quirks.
EDIT 2
There is a missing formula in the cell in question. Sorry to have wasted everyone's time.
So how can a missing formula change a cell's value. It can't!
The question should have been "How to determine why a cell is NOT changing."
So why didn't I ask that?
I did not notice that other cells in the same column as this cell contain a formula. Because the other cells were changing values correctly and this cell was not, I presumed it was some VBA code not working so I tried to track down the rogue code. I guess not being an experienced Excel person I did not rule out the bleeding obvious and went straight to VBA. Phew!
Add a change event handler on the sheet containing the cell you want to track.
In that Event Handler, add code to break when a change in the tracked cell takes place
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Cells(1, 1)) Is Nothing Then
Stop
End If
End Sub
Chnage Me.Cells(1, 1) to cell of your choosing.
When the code breaks, open the Call Stack, to see where the change came from
Here's a proof of concept. I ran ZX. Call stack shows the Change event at the top. The next function is what changed to cell.
I have the following code to simulate some cells behave like buttons
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.ScreenUpdating = False
If Target.Cells.Count = 1 Then
'~~~~~~ pseudocode ~~~~~~
If {select cell is one of the chose ones}
{do some stuff}
End If
'~~~~~~ pseudocode ~~~~~~
End If
Range("A1").Select
'Application.ScreenUpdating = True
End Sub
It doesn't matter whereas I use the ScreenUpdating code, the "Selection box" is flying around from A1 to the selected cell and back and it make the sheet much slower.
Can this flying (animation) selection box be stoped?
So far I have found this, not possible to hide, but noting about the flying efect:
Hide the cell selection box in Excel
Edit:
I need (I think so) edit capabilities on the sheet, therefore the option of not changing selection cell is not applicable. Due to:
most of the sheet is informative, and should be available for copy (not edited)
some cells are input forms (free text thing), selection as usual
some cells should behave like buttons (plus/minus for a numeric value, sclaes, simple stuff, but thousand of then, so much easier do/maintain by code), and user must not edit them
grouping should be available, (so that's complicate protecting the sheet)
I am not closed to the option : Range("A1").Select after each (most) of user interaction, but no other method comes into mind to me now.
An example:
I know some would say: "you should make this out from excel", and I agree with you, but this is a mandatory thing, I do not have the power to raise this question
As you can see, I got the "flying selection" that I try to get rid off
cell A1 is already hodden, that will do most of the trick
final version sure will go with hidden gridlines and headlines
rows groups exist, and are important, so no protection possible
all the functionality, I can do easy with vba, just problem with the animation
Maybe this is not the answer that you have been waiting for, but as Mathieu mentioned in his comment, please try to avoid using Selection.
It does make things slower and often causes errors (in example try selecting cell from hidden sheet). Instead just do something with the range that you define with your if statements directly. Every property of Cell or Range can be accessed directly.
Hope it helps.
Not sure how you can achieve you "flying select box" problem, but at least you could add this code, so opening/closing groups are available on protected sheets:
'Password Protect Current Sheet
ActiveSheet.Protect Password:="Add_here_your_password", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
How about trying to remove the "back to A1" as much as possible?
Maybe do it only on absolutely necessary, or move back to the changed value (the 33 in your example), or to the question title (in your multi-option example)
I use MS Excel 2007 to make Bills and Quotation. Often I need to reuse previous quotation.
After I copy a quotation and paste it to new location the row height needs to be readjusted for each row.
is there any solution to this?
I'm not totally sure if this is what you are after. But have you tried something like this?
Private Sub Worksheet_Change(ByVal Target As Range)
' Here you enter a matrix with the cells you want to trigger a Autofit
' If you just want one column to do it, take Range("A1:A200")
Range("A1:C11").EntireRow.AutoFit
End Sub
This code gets the row-size auto-fitted everytime you change the value in the choosen cells.
It's hard to come up with a good solution when you do not give to much information.
Check here :
Excel Worksheet_Change Event not working if my code didn't help you.
Best Regards