Monday, February 24, 2014

How to decrease the running time of a macro? (VBA)

Turning off the screen updating is one of the best ways to decrease the running time of a macro and it is also very easy to use, just assign it to False at the beginning of your macro and to True at the end, as shown on the example:

Sub my_macro()

Application.ScreenUpdating = False

 'write here the code

Application.ScreenUpdating = True

End Sub

Wednesday, February 19, 2014

How to refer to a non-contiguous range in VBA? (VBA-Excel)

You just have to separate the continuous ranges by commas inside the range specifications, follow the example:

Range("A1:A10,C1:C10,E1:E10")

Tuesday, February 18, 2014

Creating a Table with two contrasting colours (VBA-Word)

Name

Examination

Mary

Pass

Peter

Fail

Kate

Fail

John

Pass

Victoria

Pass


Do you remember how were the tables with the final classification of our exams at school? Normally they were not a simple white table with the names and results, but a table with two contrasting colours  to make it easier to be read by us.

It is very easy to paint a table with two different colours with VBA:
1. Paint manually the second row of the table with the colour you want.
2. Select the whole table.
3. Run the following macro.

Sub number_of_rows()

For a = 3 To Selection.Tables(1).Rows.count

  If a Mod 2 = 0 Then
  Selection.Tables(1).Rows(a).Shading.BackgroundPatternColor = Selection.Tables (1).Rows(2).Shading.BackgroundPatternColor
  End If

Next a

End Sub

Monday, February 17, 2014

How to copy just the value of a cell (VBA-Excel)

The following macro copy just the content of a cell (A13) to another cell (B16).

Sub Copy_just_value()

Worksheets(1).Range("A13").Copy
Worksheets(2).Range("B16").PasteSpecial (xlValues)

End Sub

How to know the code of the font color (VBA Word)

If you know the color you want, but you do not know which is its code, simply :

1.Paint text with the color you want,
2.Select the painted text'
3.And, run the following macro.

Sub color_code()

Msgbox Selection.Font.color

End Sub

A Message Box will appear on your screen with the code of the required color.

How to change the font color (VBA-Word)

To change the font color of a selected text just write the following macro:

Sub green()

Selection.Font.Color = 5296274

End Sub



Wednesday, February 12, 2014

Opening a different Workbook (VBA-Excel)

If you want to open an excel file, different from the one you are working on, just run the following macro and when the Inputbox shows up just insert the directory of your file.

Sub Open_a_different_workbook()

Dim Wbk As workbook
Dim directory As String
'''''''''''''''
directory = InputBox("Insert the directory of the Wbk File")
Set Wbk = Application.Workbooks.Open(directory)
''''''''''''
End sub

To give you an example my directory was C:\Users\Me\Desktop\example.xlsm, so the directory is, actually, the full path to the file.

How to count the number of rows of a big table in word? (VBA-word)

Just select the table and run the macro attached.

Sunday, February 9, 2014

How to insert comments in python?

To insert comments in python simply add # (mesh character) before your comments.

As example:

#this is a comment in python, the program will just ignore this line of code

You may also insert comments on the same line of your code, just write # and whatever you write after it will be ignored by python:

print 'Hello world!'  #but this comment will not be displayed

Wednesday, February 5, 2014

Sunday, February 2, 2014

How to merge cells in VBA (Excel)

Merging cells with VBA is one of the most easy commands in VBA.

You just need to specify the cells you want to merge, and give the instruction:

Sub Merge()

Worksheets(1).Range("A1:D1").Merge

End Sub