Workbook Allow Paste Special Values Only - vba

I'm in need of some expert help, i have a workbook where i need to only allow Paste Special Values. This particular workbook as special formatting ect. that I don't want overwritten when a user does a standard paste vs. paste values. I tired some VBA but couldn't get that to work quite right, and also tried the custom ui route and was able to disable the entire idMso PasteGalleryMini but i need to keep these paste special values here.
Any help or suggestions is appreciated

There are (at least) a couple of ways you could try.
Have 2 sheets in your workbook, one is for user-entered data, and the other is for formatted display of data. The formatted display sheet is protected so cannot be amended by the user - it picks up the data from the user-entered sheet which is not protected but formatting is less important. So the user cannot interfere with the formatting or layout of the protected sheet.
This approach may require quite a bit of code to ensure validation and management of data between the input sheet and the display sheet, for example if you expect the user to insert rows etc., but if your requirement is simple then it's quite acheiveable.
Allow the user to paste with formatting but then reset the formatting after the paste has been done. I have found a good approach is to use your own custom styles, where the style can define all aspects of the appearance of the cells. The user can paste data into the sheet, then when finished have a macro trigger to run code that cleans up all the formatting by setting the styles of the different areas of your sheet to the appropriate custom styles. This approach is very effective, specially if you might want to alter the styles later - you may be able just to change the style definition and don't have to change the code. You can make this easier if you have a separate sheet on which all your custom styles are listed with their names and example values in cells which are formatted with that named style. The custom styles sheet can be hidden/protected to avoid users making changes.

Related

Protecting a worksheet but still allowing columns to be resized

I have been trying all combinations of protection options on a worksheet, but I don't seem to be able to find a combination that will allow columns to be resized but also lock cell value edits. The documentation is not clear on this either.
I have used format objects to lock individual cells and then called worksheet.protect with no password.

Control Button to initiate the writing of a new VBA macro

Would it be possible to have a control button (or any mechanism) that once clicked on, a textbox would appear with fields like macro name Sub "NewMacroTitle"(), a field for the date, a field for reference sources ("Getting Links/URL from a webpage-Excel VBA"found this here"), a brief description of what the macro is to do. All of these fields, except Sub "NewMacroTitle"(), would start with a ' to show a comment and all of this would be inserted into the VBA editor, or enter it into cells on a spreadsheet that could then be copied and pasted into the VBA editor.
My problem is this. I'm new to VBA. I'm also the world's worst at documenting macros. I get an idea for a macro, I'm off into the VBA editor writing away, maybe even get the macro complete. A week later I'm reviewing the macro and the first thing is "what the heck does this thing do?"
I hope you see what I'm trying to do, essentially some way to force me to document the macro before even starting the actual code.
There are tools that will add pre-defined text to the header and/or footer of a sub. I use the one from MZ Tools for error handling code and adding my name and date to the header of each proc. It is worth a look.

How do you persistently format a TextBox to display a numeric value?

I'm sure this is a simple question, with a simple answer, but I can't find it.
I've inherited a spreadsheet that I have to fix. Whoever wrote it made extensive use of VBA and VBA UserForms for inputting data. On one form, textual and numeric information is entered and then saved to a record on a specific worksheet. This spreadsheet is used to log project information. It gets copied, and re-copied again and again. With each re-copy, it is cleared and used for the next project.
A user has sent me a spreadsheet of one of these major projects. The VBA, data-entry UserForm has a problem. One TextBox, which accepts either text or numbers, is always reformatting the numbers as dates when you exit the field!? I've tripled-checked the VBA code. There is no special OnEnter or OnExit code related to this field that reformats the data. Furthermore, I can't find a Format property that is associated with the TextBox from within the designer.
I'm a C/C# developer, not a VBA developer. Still, this "simple" IDE has me stumped. I can't find the property that re-formats the TextBox display value.
How can I fix the TextBox so that it persistently interprets numbers as numbers and not as dates?
FURTHER NOTE
It is worth mentioning two things. First, user's can't modify these forms or VBA code. The underlying modules are password protected and only myself and a couple managers know the password. Furthermore, no one touches the code because I'm the only developer within the company and everyone is a bit scare that they might break something.
Second, something in the file may have been corrupt. When this file was sent to me, the user also mentioned that the worksheet that he was working on was renamed. It appears that something didn't save properly because the WorkSheet tab was renamed a random hex string value.
Everything appears to be functioning as normal on this form, other than these two issues. Any help or guidance would be greatly appreciated.
You can force the proper format by using cLng:
First enter =TODAY() in cell A1 and then run:
Sub TextBoxIssue()
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = Range("A1").Value
End With
MsgBox "However when we use cLng......"
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = CLng(Range("A1").Value)
End With
End Sub
There is most certainly code that is formatting that text on exiting the textbox. You just haven't found it yet. If you go into the Userforms class module and select the textbox from the left dropdown, you can see all of the events in the right dropdown. The bolded events are in use. I assume you've already checked all those, but that's the place to start.
Next, look for custom class modules that use the WithEvents keyword. They can trigger events outside of the userform's class module.
Finally, search the code for all instances of =Format(tbxName.Text,"mm/dd/yyy") or some such code. Somewhere the code is probably using the Format function to fill that textbox.

Multiple instances of a single Userform / the need for pointers

I have created a Userform that lets the user pick a worksheet from a list of open worksheets. I need this Userform in various places of my macro, for various purposes; for example, to specify the worksheet containing "sales" data, and to specify the worksheet containing "stock" data. The objects wsSales and wsStock should point to the appropriate worksheets, so that my macro can later work on these.
How can I give each instance of my Userform a different behavior? One instance should set wsSales to the user-selected worksheet, the other instance should set wsStock to the user-selected worksheet.
I have thought about putting several "OK" buttons on my Useform, and making only one visible per instance. Each OK button would have different code. But that's not very elegant, because I would have to put as much "OK" buttons as there are possible uses for my Userform and it would become quite a mess in more complex situations. So, ideally, I would use a property of my Userform as a pointer to either wsSales or wsStock, but I've read that pointers don't exist in VBA. Then, what's the best way to have two instances of a Userform behave differently?
Are you actually declaring variables as instances of the userform? If not, you should. You might want to look at this post: yoursumbuddy.com/a-flexible-vba-chooser-form.
I am using a kind of common approach by means one can practically use any kind of information to create and uniquely identify one of multiple instances of a UserForm - which doesn't even require a variable for an instance. See https://warbe-maker.github.io/vba/common/2021/11/09/UserForm-Instances.html.

How to assign themes and Quickstyles in Word VBA

Where I work a department has created several theme files and quickstyle colours they want used in different circumstances.
I am wanting to create a macro that when run would save the theme automatically, and then assign a quickstyle colour group.
I have tried the macro recorder does not provide any hints as the macro is blank after attempting to record the actions, this is using either the mouse or keyboard. Searched everwhere and no solutions.
I can create the ribbon tab and buttons where we will call the macros from, I have already created macros and forms to populate documents with addresses etc.
But I cannot find any where I can set and save a theme or quick style and or colours.
A good idea for VBA is always to look into the Object Explorer in the IDE. Just press F2 while in the VBA Editor and type in the search field "theme".
There are the methods ActiveDocument.ApplyDocumentTheme(Filename as string) and ActiveDocument.ApplyTheme(Name as string). For quick styles you'll find also methods.
I don't have the time to test it, but I guess here you can go on.