Donde colocar codigo VBA?

Sam05

New Member
Joined
Feb 7, 2009
Messages
23
Hola a todos,

Soy un usuario de Excel y creo que tengo un nivel medio de conocimiento de este y algo de VBA principalmente aprendido de los ejemplos de codigo que veo en este excelente foro, pero tengo algunos vacios. Por ejemplo: podria alguien explicar cual es la diferencia de colocar codigo en un Modulo o en ThisWorkbook o en una Hoja? (mi excel es en ingles). Y tambien, hay diferencias en el tipo de codigo que debo utilizar en cada uno de los anteriores? Por ultimo, si saben de un buen sitio de VBA para principiantes como yo les agradeceria lo publicaran. Gracias de antemano.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello

I hope you can understand English. :)

Sheets:

You can code VBA in the sheets for a few different purposes.
1. You are coding objects that are located in sheets (e.g. command button, combobox etc.).
2. You want to use sheet events. There are many sheet events, but as an example you may wish for a macro to run eveytime someone changes something on a sheet. Example, place this code in one of your sheets. Then go back to your sheet and notice how everytime you select A1 it will trigger the macro:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address(0, 0) = "A1" Then MsgBox "Hola!"
End Sub


ThisWorkbook:
Again this is often used for event driven macro's. Let's say you want a macro to run each time the workbook is opened. Then you would use the Workbook_Open event. Example, place this code in Thisworkbook, save the file, close it and then open the file. It will trigger the macro everytime you open the workbook:
Code:
Private Sub Workbook_Open()
    MsgBox "Hola"
End Sub


Module:

Notice that if you record a macro that the code is stored in a module. This is because you are expected to trigger the macro directly. You can, however, assign a sub routine to a object like a command button without coding the button directly. Modules are also useful for storing functions. The functions can be used like a worksheet function (e.g. SUMIF) but they can also be called in other routines.

I recommend you buy a good book. I can recommend some but I don't know if any are available in Spanish:
Excel 2003 (or 2007) Power Programming in VBA by John Walkenbach
Possibly this from MrExcel: http://www.mrexcel.com/sunshop/index.php?action=item&substart=0&id=50

Hopefully one of our Spanish speaking members will pop in soon and give you more information in Spanish, and perhaps they can recommend some good Spanish web site for VBA and Spanish books.
 
Upvote 0
Hola Sam,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Por lo general un pone código VBA en módulos normales. Uno coloca código en módulos de hojas y cuadernos cuando uno necesita reaccionar a ciertas acciones tomadas por el usuario.
<o:p></o:p>
En Object Oriented Programming, (programación orientado a objetos) cuando el usuario hace algo a un objeto, puede ser que el objeto levanta un evento (raises an event). Lo módulos de hojas y cuadernos que usted ve en al VBE son una especie de Class Modules. Uno usa class modules para programar “event handlers”.
<o:p></o:p>
En el caso de hojas y cuadernos, dado que cuadernos son padres de hojas, hay dos lugares para manejo de eventos de hojas. Uno dentro de la hoja misma que corresponderá únicamente a eventos de tal hoja y también en el cuaderno que tiene un “handler” para tal evento pero para todas las hojas. Además, dentro del class module del cuaderno se puede responder a eventos que el cuaderno levanta pero no las hojas, como OPEN, BEFORECLOSE, BEFORESAVE, etc. <o:p></o:p>
Atte, <o:p></o:p>
 
Upvote 0
Gracias and thanks to Greg and Jon for the great input. I've been reading off the web about the subject but will definately buy a book to learn lots more!
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top