Stopping the Clipboard from being cleared in Excel VBA

How to check if the clipboard is empty

It is common knowledge (or it should be) that running a macro clears the undo list. In general you can’t undo a macro. However some macros also clear the clipboard which can stop you copying and pasting. I have found a workaround for the clipboard problem.

Thank You Matthew Harris

Recently I learned about the passing of Chip Pearson an Excel legend.

He was so generous with his content and I had thanked him for his contributions years back, but it got me thinking about thanking others who had helped me along the way.

My third thank you goes out to Matthew Harris. Here is a link to his website

Back in the 90’s I taught myself VBA using his book – Teach Yourself Visual Basic For Applications in 21 Days. It has pride of place on my bookshelf.

The book really made a difference to the way I used Excel and opened my eyes to so many possibilities.I found I really enjoyed programming when working in VBA and Excel.

I have learned a lot since, but his book gave me a great grounding in VBA.

Thank you so much Matthew for your book, it has made a huge difference in my life.

Name Box and Macros

I have found out yet another feature of the Name Box.

The Name Box is on the left of the Formula Bar and above the column letters – see image below.

During a recent macro webinar I tried to create a new range name called Test using the Name Box.

But I also has already created a macro called Test as part of the training.

As soon as I pressed Enter after typing Test into the Name Box to create the Test range name, I was magically transported to the VBA window to the Test macro – Wow!

This means you can’t create a range name in the Name Box that is the same as a macro name.

You have to use the Define name icon on the Formulas tab to do that.

 

Free Excel Webinar – recording Macros Tips Tricks and Traps

Getting started with macros

It is easy to create a recorded macro. It is not so easy to create a flexible and re-usable recorded macro. Click the materials Button below to download the pdf manual and example file.

Learn the techniques that can allow you to record effective macros that can handle different ranges and changes to sheet names.

Macros can speed up your work and reduce the time taken for tedious tasks, as well as adding functionality to Excel.

This is the first in a series of webinars dedicated to macros. Future paid sessions this month will expand on the techniques taught in this session.

Macro Webinar Materials

 

VBA If statement tip

When creating long VBA code it is common that the start of an If statement and the matching End If statement may not be visible on the same screen.

When scrolling around trying to understand your code it can be useful to include the If statement itself as a comment following on the same line as the End If command – see examples below.

If x=0 Then
 
  'lots of code
 
  If y =1 Then
 
    'lots of code
 
  End If   'If y =1 then
 
End If   'If x=0 Then

The apostrophe is used to specify the start of a comment – you can have a comment following a line of code.

This structure can assist when trying to identify which End If statement relates to which If statement.

Replace Merged Cells with Center Across Selection

Let the macro do the work

Unfortunately lots of people use the Merge & Center format in their spreadsheets. When working with other people’s files that contain Merged cells I will often remove the Merged cells format and apply Center Across Selection which is the preferred format to use. The macro below will convert Merged cells to Center Across Selection.

Make Excel VBA Pause

Sometimes when running a macro you need to make sure Excel has had time to do something before progressing.

This is typically in large models were it can take time (a few seconds) to do a specific task eg removing a filter or updating an external data source.

You can pause a macro to allow Excel to do something by using the Wait command.

Application.Wait (Now + TimeValue("0:00:02"))

The above code will pause the macro for 2 seconds.

VBA to Clear a Filter

Using Excel’s built-in filtering can speed up your VBA code.

It is important if you are applying filters that you clear any existing filters before you apply a new filter. Otherwise the existing filters will usually affect a new filter you apply.

The line of code below will remove filters on Sheet1 (Sheet1 is the sheet code name that you see on the left side of the VBA screen – it may not be the sheet tab name).

If Sheet1.FilterMode Then Sheet1.ShowAllData

The .FilterMode property is True if a filter is in place on the sheet and False if not.

The .ShowAllData method will return an error if no filter is in place – hence the use of the If statement.

Data Validation Search – Free Add-in

Jon Acampora
2017-04-27

A common Excel request is to be able to type characters and see the in-cell data validation drop down list reduce, based on what you have typed.

This free add-in from Jon Acampora (Excel MVP) does just that. He has recently added a few new features.

If you have long drop down lists this add-in is a great addition to Excel.

This link has a video of how it works and the new features like Auto Open when a data validation list cell is selected.

Export a sheet as a PDF

It takes a few clicks but it is possible

I have previously posted about using CutePDF to create pdfs from Excel sheets. There is another way, but it takes a few clicks and it only works in Excel 2010 and later versions. (It may work in Excel 2007 but I have taken that version off my PC so I can’t test it.)

Excel Cell Comments and Documentation

Macro to Create a Comment Report for a File

I was watching a video a while back and some Excel experts were lamenting the lack of a documentation standard in Excel. They mentioned that the cell comments system could be used for documentation, but there was no way to centralise all the comments. Well, I have written a macro to do just that.

Ron DeBruin

Ron DeBruin is an Excel MVP and has been for a long time.

His site has lots of excellent macro code and free Add-ins.

He has tips and macro examples for the Mac as well as Windows.

I have used his pdf creator code to create pdfs of certain sheets in a file.

If you want to learn about modifying the ribbon he has some great tips and examples.

He also has code examples for amending Excel’s pop-up menus.