Here is what I need to do: When I have written something into a cell in the sheet, my Worksheet_Change code should check if the cell contains certain characters and then replace those characters. That part of the code is working fine.
However there is a slightly odd behavior. Here's the code so far:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Value = Replace(Target.Value, "ß", "ß")
MsgBox "This is the value: " & Target.Value
End Sub
What's happening is that when I have the characters that need to be changed in my clipboard (using Ctrl+C). When I double-click onto the cell, paste the characters into the cell using Ctrl+V, and then press Enter, the code works just fine, the characters are changed. (Note: Without the double-click, you can't see the cursor.)
However, if I just go to the cell with my arrow keys and paste over whatever else is in the cell, nothing happens. I suspect the Worksheet_Change isn't even triggered, or else it would at least display my MsgBox.
(I don't know if it's relevant to this, but i am using Excel 2010 in a Mac)
You can use Worksheet_Calculate on a trigger sheet.
Create a new sheet which only contains a link to the range you want
to watch (i.e. =Sheet1!A1:B3)
Add a macro called Worksheet_Calculate to your trigger sheet
Now whenever any date in the linked range changes a recalculation of your trigger sheet gets triggered which in turn sets off your macro
This also works when the user enters a formula in the corresponding cell and one of the predecessors changes.
With manual calculation the recalculation of the trigger sheet and the macro only happen when the user presses F9.
Always use EnableEvents property when working with Worksheet_Change event. For more details link
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo err_rout
Application.EnableEvents = False
If Target.Count > 1 Then Exit Sub
Target.Value = Replace(Target.Value, "ß", "ß")
MsgBox "This is the value: " & Target.Value
err_rout:
Application.EnableEvents = True
End Sub
If Target.Column = 3 Then
Dim startRow As Integer
Dim endRow As Integer
startRow = Target.Row
endRow = Target.Row + Target.Count - 1
For i = startRow To endRow
' blah blah blah
maybe it helps
Related
I have the below code. I want to have my cursor active in a cell, run the macro, and have the vba put the string of periods onto the clipboard then paste them into the cell where my cursor is active. Can this be done?
Dim obj As New DataObject
Dim txt As String
'Put some text inside a string variable
txt = ".............................."
'Make object's text equal above string variable
obj.SetText txt
'Place DataObject's text into the Clipboard
obj.PutInClipboard
Looking at your last question you may be able to use a Worksheet_Change event to simply add your periods at the end of the string after every edit.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge = 1 Then
If Len(Target) > 0 Then
Application.EnableEvents = False
Target = Target & "......"
Application.EnableEvents = True
End If
End If
End Sub
You can customize this to a degree if this route is feasible. For instance, right now the macro ignores any changes that result in the cell being blank (i.e. you can still clear a cell with normal functionality). As per #BigBen's suggestion, the macro also ignores changes that apply to multiple cells at once (i.e. pasting a range to your sheet) You can also amend this to only fire if the cell changed is in a certain row/column. etc.
I want my macro to stop if there's no value entered in any of the four cells I need. But i want it to run if there's at leats one value in those four cells.
This is what i have so far:
If Range("e12,h12,k12,d12").Value = "" Then
MsgBox ("Por favor introducir dimensiones")
Range("e12").Select
Exit Sub
End If
If you introduce a value in cell e12, it will run. But if you introduce a value in any other cell, the msgbox will pop out and the macro will stop.
Could you help me find the problem?
Thank you!
Please install the event procedure below in the code sheet of the worksheet on which you want the action.
Private Sub Worksheet_Change(ByVal Target As Range)
Const Triggers As String = "E12,H12,K12,D12"
Dim Rng As Range
Dim Cell As Range
' skip if more than one cell was changed (like paste)
If Target.Cells.CountLarge = 1 Then
Set Rng = Range(Triggers)
For Each Cell In Rng
If Cell.Value = "" Then
Cell.Select
Exit For
End If
Next Cell
End If
End Sub
Now, if the user enters something in E12 the macro will select H12. If the user enters something in D12 next the macro will take him back to K12. That's all very nice.
But if the user changes something in A3 (anywhere, in fact) he will be taken to the first empty cell of the trigger range. Therefore the system must be tweaked to accommodate your workflow. Perhaps the code should be made to run only when D12 is entered, or when the user clicks on the cell he shouldn't click on before completing the trigger range.
In short, the scope of the procedure may have to be trimmed to suit your workflow. This can be done by either including specific cells in the trigger range, or by excluding other cells.
you coudl use WorksheetFunction.CountA() function to count the number of not empty cells:
If WorksheetFunction.CountA(Range("e12,h12,k12,d12")) <> 4 Then
MsgBox ("Por favor introducir dimensiones")
Range("e12").Select
Exit Sub
End If
I need to stop the user from pasting over my validation drop down cell. I have read and tried various solutions, none of which work just right. This code I have checks if the pasted value follows validation rules, but it doesn't work if the entire cell is pasted over my validation cell (it seems that this event fires after the paste, so the validation gets erased together with the previous cell):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Range("D2:F13")
If Not Cell.Validation.Value Then
MsgBox "Value violates validation rule"
Application.Undo
Exit Sub
End If
Next
Ideally the code would check if the value of the cell that's being pasted matches validation dropdown options and only allows to paste the value (not the formatting) into the cell.
Thanks!
You can disable the Cut\Copy in the specific cell:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' TARGET IS YOUR VALIDATION CELL
If Target.Column = 1 And Target.Row = 1 Then
Application.CutCopyMode = False
End If
End Sub
Or more complex you can try to check the clipboard of the user on SelectionChange
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' TARGET IS YOUR VALIDATION CELL
If Target.Column = 1 And Target.Row = 1 Then
Set MyData = New DataObject
MyData.GetFromClipboard
'In MyData.GetText you have the clipboard data in text format
If MyData.GetText <> "what you want" then
'...
End if
End If
End Sub
In this case you must add a reference to Microsoft Forms 2.0 Object Library. You can find it in this path: C:\Windows\System32\FM20.DLL
How to insert a character in Excel cell by simply clicking on it? Clicking again on the cell would remove the character from the cell.
Example:
I have a cell range, A1:A10. If I click on a cell on this range a "X" is inserted on that specific cell, clicking again on it would remove the "X".
Can this be made through a button assigned to a macro? (the button would be assign to a specific cell)
I have a cell range, A1:A10. If I click on a cell on this range a "X" is inserted on that specific cell, clicking again on it would remove the "X".
You have to be very careful when you are using Worksheet_BeforeDoubleClick. If you do not take care of that in the code then the code will fire whenever there is a double click. Also use Cancel = True appropriately else you may get undesired results.
Is this what you are trying?
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
'~~> Check if the double click happened in Range A1:A10
'~~> Also notice the placement of `Cancel = True`
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
'~~> Check if the cell is empty
If Len(Trim(Target)) = 0 Then
Target.Value = "X"
Cancel = True
'~~> Check if the cell already has X. This is to ensure that
'~~> the cell is not cleared if the user has typed something
'~~> else in the cell.
ElseIf UCase(Trim(Target)) = "X" Then
Target.ClearContents
Cancel = True
End If
End If
End Sub
I would suggest to use double click to enter/remove value. If you're ok with using double click..then here's the code for that:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If IsEmpty(Target.Cells) Then
Target.Cells.Value = "x"
Else: Target.Cells.Clear
End If
Cancel = True
End Sub
The code needs to be placed in ThisWorkbook object
This does not seem like a good approach. Why not use dropdowns?
Go to Data->Validation select a list. You can either select your list of options from an Excel range or simply specify the list like this "Item1;Item2" etc.
Using any VBA will prevent the usage Undo function in Excel meaning - any time a macro executes in Excel all you undo history is cleared. This is very inconvenient when making changes.
I am trying to use a button in Excel to copy a certain range of cells from the active workbook to another workbook. The copying works perfectly when I specify a fixed range for each button in its assigned Macro but I'm stumped as to how to use the same Macro on each button and for the button's row number to indicate the row of data to be copied.
Every row contains 6 or so cells with the 7th containing the button. When the user presses this button the 6 cells on the same row as the row containing the pressed button need to be copied.
I am a complete novice when it comes to VBScript but much googling has got me this far:
Sheets("SurfaceThreats").Range("A4:F4")Copy_
Sheets("ORBAT").Cells(Rows.Count,1).End(x1Up).Offset(1,0)
Surely there is a more elegant solution than assigning a different, fixed range, Macro to every button.
See this screenshot
And this is the code for the Select Row(s) button
Option Explicit
Private Sub CommandButton1_Click()
Dim Ret As Range, rng As Range
Dim lRow As Long
On Error Resume Next
Set Ret = Application.InputBox("Please select the row", Type:=8)
On Error GoTo 0
If Not Ret Is Nothing Then
For Each rng In Ret.Rows
'~~> Get the last row in sheets "ORBAT" where you want to copy
With Sheets("ORBAT")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
End With
'~~> Copy the rows
Sheets("SurfaceThreats").Range("A" & rng.Row & ":F" & rng.Row).Copy _
Sheets("ORBAT").Cells(lRow, 1)
Next
End If
End Sub
You can't create a generic button handler, but you can dynamically create the buttons and the macros to handle them. But it's quite a lot of work and you'll have to start creating macros dynamically. (this can cause problems with anti-virus software sometimes i think).
You could use the Worksheet_OnBeforeDoubleClick event to create "fake" buttons
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Target.Worksheet.Columns(1)) Is Nothing Then
MsgBox "You Clicked " & Target.Address
' call your generic handler here passing the Target.Address or Target.Row
Cancel = True
End If
End Sub
Using this code, if you double click any cell in the "A" column you'll get a message box saying what cell you clicked.
Its not the best of ideas, some users might not understand that it is a button.