Wednesday, November 12, 2014

How to print a blank line in python?

If you want the blank line to be printed...

....after your string just add "\n"at the end of it, like in this example:

print  "hello\n"

and  blank line will be printed after "hello".

.... before your string just add "\n" in the beggining of  it, like in this example:

print "\nhello"

.... in other cases, just write the word print, like in this example:

print 


Sunday, November 9, 2014

How to unlock the keyboard on windows 8

If, as I actually did, you got by accident, your keyboard locked on windows 8, just watch the video below to find out how to solve your problem:


Wednesday, October 29, 2014

How to create a string array on VBA?

Dim myarray As Variant
myarray = Array("this", "is", "an", "array") 

Saturday, September 6, 2014

How to print a variable value in TCL

Just be sure about the existence of the variable simply "a" and write:

puts $a


For beginners: If you are learning and wanting to run a Tcl program for the very first time, try to write the world's most famous program like this:

set a "Hello World!"
puts $a

Now, guess want you are expected to get. ;)

New language on this blog TCL-TK

Lately, I have been using TCL-TK a lot on some small projects that I am doing, that is mainly why I haven't been writing a lot during the last weeks. So, I thought it would be a good idea to talk a bit about it.

First of all, do you know what is TCL-TK about? If not, just check it here and here.

On the next posts I will teach you some basic TCL commands.

Wednesday, March 26, 2014

How to adjust automatically the width of the columns?(VBA-Excel)

This might be the easiest command you have ever seen in VBA. ;)

Sub columns_adjust()

Columns.Autofit

End Sub


Monday, March 24, 2014

How to find a maximum of a range (VBA-Excel)

Sub maximum()

Range("t11").Value = Application.Max(Range("e1:e20"))

End Sub

Tuesday, March 18, 2014

How to iterate over the files in a folder (VBA-Excel)

On our example below, we iterate over all text files (*.txt) inside the folder called "myfolder", please change the path according your folder and if you want to see all the files instead of just the files with a specific extension write just: "*" instead of "*.txt"

Sub over_files()

Dim file As String
Dim folderpath As String
Dim filename As String
Dim Count_files As Integer

''''''Data'''''''''''''''''''''
folderpath = "C:\Users\Guest\Desktop\myfolder\*.txt"
'''''''''''''''''''''''''''''''''''''

   file = Dir(folderpath)
  
Count_files = 0

While (file <> "")

    If Len(file) > 0 Then
    filename = Left(file, Len(file) - 4)
    Count_files = Count_files + 1
    End If
  
   ' your code here

     file = Dir

Wend

End Sub



Monday, March 17, 2014

How to know the running time of a macro (VBA-Excel)?

Just use the following code and the running time of your macro will be printed, in seconds, on the immediate window:

Sub macro1()

Dim starttime, endtime As Double

starttime = Now

' your code here

endtime = Now
Debug.Print "Macro ran successfully in " & DateDiff("s", starttime, endtime) & " seconds"

End Sub

Tuesday, March 11, 2014

How to import or remove a .frm file into your VBA project?

Sub to_import()
Application.VBE.ActiveVBProject.VBComponents.Import "c:\temp\myform.frm"
End Sub

Sub to_remove()
Application.VBE.ActiveVBProject.VBComponents.Remove Application.VBE.ActiveVBProject.VBComponents("Userform1")
End sub

P.S: Before running please allow the macro to change its own script, on "Macro Security".
Check the following option: "Trust access to the VBA Project object model".


Friday, March 7, 2014

Macro is executed just during seven days (VBA-Excel)

Sub Your_macro()

If DateSerial(Year(Date), Month(Date), Day(Date)) - DateSerial(2014, 3, 7) < 7 Then

'your code here

End If

End sub

Wednesday, March 5, 2014

How to delete a file with VBA?

Sub delete_specific_file()

Kill "C:\folder\filename.xls"

End Sub

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

Monday, January 27, 2014

Annual Equivalent Rate

To evaluate deeply the revenue of a specific asset, we should take into account the number of times a year that the interest is paid.

So, if you want to know which is the "real" interest rate of a specific asset you can use the following VBA script on Excel.

Sub equivalent_rate()

Dim rate As Double
Dim eq_rate As Double
Dim n As Integer

rate = InputBox("Insert the annual rate in percentage") / 100

n = InputBox("Insert the number of times you receive the interest")

eq_rate = (((1 + rate / n) ^ n) - 1) * 100

MsgBox ("Your equivalent interest rate is " & eq_rate)

End Sub

How to import data from internet into Excel?

Do you know how to Import data from a website into Excel?

Using VBA it is pretty simple, take a look on the script below that downloads the performance of the General Motors Shares during the last month.

If you need to download information from another website just change the URL!

Thursday, January 23, 2014

How to stop an endless loop (Office 2010, 2013...)


Stopping endless loops in VBA


One of the most disappointing things for a VBA beginner is the handling of the endless loops.

When using loops ("for loop", "while loop", "do loop", etc) sometimes we use conditions that are always true and the macro runs forever,  in programming we call it an endless loop.

To give you an example, think about, but please do not run, the following macro:
Sub endless()
While Worksheets(1).Range("A1").value=""
Worksheets(1).Range("A1").value=Worksheets(1).Range("A1").value
Wend
End Sub
If on the first worksheet of the excel file, the cell(A1) is blank this macro will run forever, because the condition to be true is having the cell(A1) blank and this condition will always be true. The macro will run forever, we created an endless loop.

To stop the macro we would have to close the excel program (clicking on task manager-> choose the task -> end task) and to lose all the unsaved data in our file.

However, fortunately there is an easier way to stop an endless loop:

  1. Before running the macro write the following script on the immediate window: Application.EnableCancelKey=xlInterrupt
  2. Run the macro.
  3. If your macro has an endless loop, or is taking much longer than you thought. Please press ESC and it will stop immediately. J
Please check the following video, if you did not get it: