I have used an excel macro that automatically pastes all data as values, by undoing the pasting and pasting it as value. This is the code:
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim UndoList As String
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo Whoa
'~~> Get the undo List to capture the last action performed by user
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
'~~> Check if the last action was not a paste nor an autofill
If Left(UndoList, 5) <> "Paste" And UndoList <> "Auto Fill" _
Then GoTo LetsContinue
'~~> Undo the paste that the user did but we are not clearing
'~~> the clipboard so the copied data is still in memory
Application.Undo
If UndoList = "Auto Fill" Then Selection.Copy
'~~> Do a pastespecial to preserve formats
On Error Resume Next
'~~> Handle text data copied from a website
Target.Select
ActiveSheet.PasteSpecial Format:="Text", _
Link:=False, DisplayAsIcon:=False
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
On Error GoTo 0
'~~> Retain selection of the pasted data
Union(Target, Selection).Select
LetsContinue:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
I am using the English Excel version where it works fine, however when other users, who e.g. have a German Excel version paste something, they get a procedure error in this line:
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
I guess the Undo command is named differently in German. Is there a way to define the Undo list independent of the language the user is using?
Use the Control’s Id number instead.
debug.Print Application.CommandBars("Standard").Controls("&Undo").Id
=> 128
debug.Print Application.CommandBars("Standard").FindControl(Id:=128).caption
=> &Undo
Answer:
Use the index as name:
undoname = Application.CommandBars("Standard").FindControl(ID:=128).Index
UndoList = Application.CommandBars("Standard").Controls(undoname).List(i)
Answer:
You should also substitute in the code the words “Paste”, “Auto fill” and “Text” for your local language words.
Tags: excel-vbaexcel, list, vba