Tuesday, September 22, 2015

Write codes

If you want to write codes for Excel with VBA, enable the developer tool bar, then see the developer tool bar on the Ribbon.
the image: cited from HomeLearn. Though "stop recording" is being selected on the photo, click the Visual Basic to open the Visual Basic Editor. 

By clicking the Visual Basic on left of the bar, you can open the Visual Basic Editor on which you can write codes for Excel. 
the image: cited from HomeLearn.

If you want to save the macro for only Sheet1, open the folders of VBAProject (Book1) and click sheet1 as being shown on the photo. In case you want to use this macro every time you open the Excel, you click VBA project (Personal.exlb) and save there, but we will save the macro only for sheet1 this time, so we will save it here.

Now we will writes codes here. On the coding window on right, write "Sub Range_A1_D6()". This is the name of the Sub. Then we press Enter on keyboard. Then "End Sub" will automatically appears at the last line.
the image: cited from HomeLearn.
Now we will write how the program actually works in between Sub and End Sub. Write there as follows:
the image: cited from HomeLearn.

Range is the object with properties A1 and D6. Select is a method. This codes means "select the cells of a range A1 to D6." From the menu bar at the top, click File then Save Book 1.

When we do this, we might get a message saying this file can not be saved. Excel can't save Macros in a file that end in xlxs. In that case, click the "Save As Type" dropdown list and, from the list, select Excel Macro-Enabled Workbook(*.xlsm). After changing the file to xlsm, we can save the file without error message now. 





References

Home Learn "Excel VBA programming" accessed on 22nd September 2015.