Excel VBA — Preparation before recording first macro | Introduction to macros and VBA 02

In this post, we will work on some­thing we should con­sid­er before start­ing record­ing our first macro. 

Excel VBA — Prepa­ra­tion before record­ing first macro | Intro­duc­tion to macros and VBA 01

First of all, in the Devel­op­er tab, we have the Record Mar­co com­mand. We can also find this com­mand in the sta­tus bar on the left side. It make it eas­i­er to start our record­ing (Fig. 1)

 Record Macro command in the status bar
Fig. 1 Record Macro com­mand in the sta­tus bar

Then, we should con­sid­er rel­a­tive and absolute ref­er­ence. We have the Use Rel­a­tive Ref­er­ences com­mand. If it isn’t high­light­ed, it means that we work with absolute ref­er­ences. For exam­ple, if I select cell B9, then select cell B12, Excel will record this as ‘select cell B12′. When I turn on the Use Rel­a­tive Ref­er­ence com­mand, then select cell B9, and then cell B12, Excel will record this step as ‘go three rows down’. This issue is very impor­tant when record­ing our macro. 

Let’s click on the com­mand one more time to turn it off and work with absolute reference. 

There is one more com­mand to look into. It’s called Macro Secu­ri­ty. After click­ing it, we have a Trust Cen­ter win­dow. In the Mar­co Set­tings area, we can see an option of Dis­able VBA macros with noti­fi­ca­tions (Fig. 2)

Disable VBA macros with notifications option
Fig. 2 Dis­able VBA macros with noti­fi­ca­tions option

This option should be select­ed by default. It means that when we open a file with macros (with an xmls exten­sion), we will have infor­ma­tion that this file con­tains macro, and some code can be run with­out our knowl­edge. How­ev­er, in most cas­es, we should­n’t be afraid of this code. It’s a code which we copy from secure sources, like Google. Google is quite a secure source, as it shows you good web­sites, not dan­ger­ous ones. 

When you start work­ing with VBA, you should know some­thing about the VBA code. If you see some­thing unfa­mil­iar, you should­n’t run this code. 

I per­son­al­ly don’t like noti­fi­ca­tions, espe­cial­ly the ones that show up every time I open a file con­tain­ing macros. That’s why, in most cas­es I’m work­ing with the Enable VBA macros option. It’s not rec­om­mend­ed, how­ev­er, from my point of view it’s prac­ti­cal. I’ve been work­ing with macros for many years, and I haven’t come across any dan­ger­ous code. The most dan­ger­ous thing a VBA code has done to me is rewrit­ing my cur­rent data. 

Hav­ing the above in mind, when you run macros and you don’t ful­ly know what they are doing, you should always cre­ate a back­up copy before.

The above are the most sig­nif­i­cant pieces of infor­ma­tion you should con­sid­er before record­ing your first macro. In the next post, we will be record­ing our first macro. 

https://www.youtube.com/watch?v=L9TDg-vRC48

Excel VBA — What is a macro and enabling the Developer tab | Introduction to macros and VBA

This post starts a series about an intro­duc­tion to macros and VBA code. 

Excel VBA — What is a macro and enabling the Devel­op­er tab 

What is a macro? In most cas­es it’s a syn­onym for VBA code that we run. It’s almost the same thing. Some­times we dif­fer­en­ti­ate it and we call macros the VBA code that was record­ed by Excel — Record Mar­co com­mand. If we want to use this com­mand, we should turn on the Devel­op­er tab. In order to do that, we have to right-click on our rib­bon and go the Cus­tomize the Rib­bon com­mand (Fig. 1)

Customize the Ribbon command
Fig. 1 Cus­tomize the Rib­bon command

In the Excel Options win­dow that has appeared, we have to find the Devel­op­er tab on the right side, press it and press OK (Fig. 2)

 Developer tab
Fig. 2 Devel­op­er tab

Now, we can see that in the Devel­op­er tab we have the Record Mar­co com­mand (Fig. 3)

 Record Macro command
Fig. 3 Record Macro command

This com­mand allows us to record actions we are doing on a work­sheet, such as cell for­mat­ting, putting val­ues in a cell, size mod­i­fi­ca­tion, etc. It’s a good place to start learn­ing about the VBA code. 

Macros are good for two main reasons. 

First, they are good for repeat­ing tasks. If we want to make reports on a dai­ly basis, we can just record all actions we are doing in a work­sheet and then we are able to use it again, prob­a­bly with tiny mod­i­fi­ca­tions. We will have a report for­mat by run­ning macro. 

Sec­ond, the VBA code allows us to do the things that are not pos­si­ble or very hard when using Excel func­tions. Those things will be much sim­pler using the VBA code. It may even hap­pen that some­body has already writ­ten a prop­er code to this task. 

If we want to save macro, we have to use the ‘xlsm’ exten­sion, where ‘m’ stands for macros. We can run macros on ‘xlsx’, which is a stan­dard exten­sion for Excel, how­ev­er Excel won’t save macros in those files. Remem­ber, only ‘xlsm’ works for macros. 

https://www.youtube.com/watch?v=bUR7BQ4bglA

Extracting only letters from text

If I need to extract only let­ters from my text, what should I do? Lets find out.

Extract only let­ters from text 

When I need to extract only let­ters from my text, it means that I also need to extract spaces and enters (new lines). 

With Pow­er Query this task is quite easy. 

I need to select one cell in my table, then go to the Data tab, and click on the From Table/Range com­mand in order to take our data to Pow­er Query (Fig. 1)

From Table/Range command
Fig. 1 From Table/Range command

Here, I have four sim­ple pieces of text. One in Eng­lish and three in Pol­ish. We need to go to the Add col­umn tab, then to the Cus­tom col­umn com­mand (Fig. 2)

Custom column command
Fig. 2 Cus­tom col­umn command

In the Cus­tom Col­umn win­dow, we have the space to name our col­umn. Let’s call it Clean, and below we have the Cus­tom col­umn for­mu­la box, where we write the Text.Select func­tion. In the func­tion we need to work with text, so let’s select the right col­umn (1). Then, I need let­ters. Since there will be string of char­ac­ters, I have to use curly brack­ets, where we write upper case let­ter in the first sequence, which is “A”..“Z”. Then let’s write low­er case let­ters, which is “a”..“z”. Writ­ing two dots between let­ters means that there is a sequence. Let’s close the curly brack­et and the paren­the­sis. Now, it should be fine (Fig 3)

Whole function
Fig. 3 Whole function

After press­ing OK, the sys­tem extract­ed for us only let­ters. How­ev­er, the text pre­sent­ed like this is just gib­ber­ish. That’s why we have to add spaces. To do that, we need to go to the View bar, then select the For­mu­la bar check­box and start mod­i­fy­ing our for­mu­la. Let’s write a com­ma and a space in dou­ble quotes (1). If we need oth­er signs, we should also write them, e.g. a dot (2). As we can see, we have spaces and dots in our new text (Fig. 4)

Modifying the formula
Fig. 4 Mod­i­fy­ing the formula

How­ev­er, I don’t need dots in my text. What I real­ly need are enters. We can remove the dot from the for­mu­la and write #(lf) in its place. After enter­ing the for­mu­la, we can see that we have extract­ed enters, low­er case let­ters, spaces. But, I did­n’t extract Pol­ish let­ters. In row 4 there are strict­ly Pol­ish let­ters. In rows 2 and 3 there are some of them. The ques­tion is: how can I extract unusu­al signs from text using the Text.Select func­tion. First of all, we have to check the Uni­code. We can see that let­ters from A to Z is a sequence of num­bers from 65 to 90. It’s sim­i­lar in low­er case let­ters. The most impor­tant les­son from it is that we have a sequence (Fig. 5) 

Unicode
Fig. 5 Unicode

Now, let’s go back to our for­mu­la. If I want to have Pol­ish let­ters, I can write a sequence of “Ó”..“ż”. After enter­ing the for­mu­la, we have extract­ed also Pol­ish signs (Fig. 6)

 Extracting Polish signs
Fig. 6 Extract­ing Pol­ish signs

Now, that we have extract­ed all signs we need, we can go to the Home tab and click on the Close&Load to com­mand to trans­fer our results to Excel (Fig. 7)

Loading results to Excel
Fig. 7 Load­ing results to Excel

In the Import Data win­dow, we have to select a cell (Fig. 7)

Choosing the target cell
Fig. 7 Choos­ing the tar­get cell

As we can see, the text is writ­ten in one line, so we need to wrap it. Let’s go to the Home tab and click on the Wrap Text com­mand (Fig. 8)

Wrap Text command
Fig. 8 Wrap Text command

Now, we clear­ly see upper case let­ter, low­er case let­ter, spaces, enters. I removed signs that I did­n’t need (Fig. 9)

Final table
Fig. 9 Final table

https://www.youtube.com/watch?v=K1KgVudEtnk

Hiding scroll bars, formula bars and column headers

When we need more space while work­ing in Dash­board in Excel, we can just hide unnec­es­sary options. How? Let’s find out.

Hide scroll bars, for­mu­la bars, col­umn headers

The options we can hide are e.g. scroll bars, sheet tabs, for­mu­la bars as well as column/row head­ers. If we want to hide then we just go to the File menu and choose the Options com­mand (Fig. 1)

Options
Fig. 1 Options 

In the Excel Options win­dow, we choose the Advanced tag. If you pre­fer using short­cuts, you can press Alt + T + O, which also opens the win­dow. Lets go to the Dis­play area and uncheck the Show for­mu­la bar check­box. Remem­ber that it works for all Excel files (Fig.2 )

Show formula bar checkbox
Fig. 2 Show for­mu­la bar checkbox

Then, we can go to the Dis­play options for this work­book area. We have to uncheck the Show hor­i­zon­tal scroll bar, Show ver­ti­cal scroll bar, Show sheet tabs check­box­es (Fig. 3)

Three checkboxes
Fig. 3 Three checkboxes

We can even go to the Dis­play options for this work­sheet area where we can uncheck the Show row and col­umn head­ers check­box and press OK (Fig. 4)

Show row and column headers checkbox
Fig. 4 Show row and col­umn head­ers checkbox

We can also dou­ble-click on the Home tab to hide the rib­bon, which gives us even more space than before. 

Now, I can press Ctrl + Page Down to go to anoth­er work­book. Here, we can see that col­umn and row head­ers are vis­i­ble. How­ev­er, when we go back to our work­book by press­ing Ctrl + Page Up, we can see that there are no row or col­umn head­ers. That’s how you make more space in your Dashboard. 

https://www.youtube.com/watch?v=2TNzX5RSG7A&t

Drop-down lists

How to add a drop-down list in Excel? It’s a real­ly sim­ple task. 

First of all, we have to have our list (1). Then, we have to select a cell or cells to which we want to add the drop-down list (2). Then, we’re going to the Data tab (3) and click on the Data Val­i­da­tion com­mand (4) (Fig. 1)

Preparing data
Fig. 1 Prepar­ing data

In the Data Val­i­da­tion win­dow, we have to select the List option in the Allow bar, then we’re select­ing the range and write it into the Source bar. Then OK (Fig. 3)

Data Validation window
Fig. 2 Data Val­i­da­tion window

Now, we have a drop-down list (Fig. 3)

A drop-down list
Fig. 3 A drop-down list

Since our cells with data are in a table, when we add a new line to the table, it also involves the drop-down list. 

Even, if we cut and paste our new data, the cells in the select­ed col­umn will still have the drop-down list. What’s more, in the newest Excel ver­sion, we have even Lookup options. We just have to start writ­ing the first let­ter of a word we’re look­ing for, then press the Tab key.

Lookup option
Fig. 4 Lookup option

This func­tion, how­ev­er, does­n’t work when we start from the mid­dle of our text. It works only with the first letter. 

https://www.youtube.com/watch?v=NMhYSgf0O3s