shrivallabha
Excel Ninja
Are you reading any book related to VBA? If not then please start reading one as it will give you a lot more structured approach and understanding of VBA.No doubt there is probably a better code structure that could be made than what I cobbled together, but at this point in time, I'm going for "does it work correctly?" I have not found any errors, yet. It does take about 70 seconds to run, but that's ok. It saves me incalculable time of manual labor. Without going into detail, do you see anything that I could change in the code to make it run faster? Yes or no, would be fine at this point in time. I can ponder over making it faster later.
I performed a "Record Macro" to figure out what lines I needed to know what to put in the code to make the data do what I needed and then put those recorded segments in the existing VBA code and crossed my fingers.
My question, hopefully more to the point, is "why" did it work without putting something like ".font" in front of each segment line? For example, something like .Section? As you can tell, I know just enough about VBA (and Excel) (painfully self-taught with a lot of help from Chandoo folks like yourself) to be dangerous.
Coming back to your question, Excel is an application (akin to whole body).
Application comprises of several components called objects (body parts) which either perform an action(method/procedure/function) or store information(property). Understanding and manipulating an application through programming needs knowledge of this model, Component Object Model (COM). An object may be parent of another object or vice versa.
In your case you want to control Cell behavior. Cell or Range is an object. Its place in COM hierarchy would be as below:
Application >> Workbook >> Worksheet >> Range
i.e. you can completely qualify the object like below
- Create one workbook with three sheets
- Select third sheet and then step through below code using F8
Code:
Public Sub ExampleRangeFont()
Dim xlApp As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rngOne As Excel.Range, rngTwo As Excel.Range, rngThree As Excel.Range
'\\ Bind Application to running instance of application
Set xlApp = GetObject(, "Excel.Application")
'\\ Set reference to workbook
Set wbk = xlApp.ActiveWorkbook
'\\ Set reference to worksheet
Set wks = wbk.Sheets(1)
'\\ Set reference to range
Set rngOne = wks.Range("A1")
'\\ Equivalent code of above will be as below
Set rngTwo = xlApp.ActiveWorkbook.Sheets(2).Range("A1")
'\\ If we do not define preceding objects then Code assumes action relates to current application, activeworkbook and activesheet
Set rngThree = Range("A1")
'\\ Now if we want to manipulate Range object's font behavior then
'Style one
With rngOne.Font
'here you can manipulate font behavior only inside With
.Bold = True
End With
'Style two
With rngTwo
'Here you can use all properties belonging to Range object
.Font.Bold = True
'Interior is another property
.Interior.Color = vbYellow
End With
'It is an independent statement as there's no loop
rngThree.Font.Bold = True
End Sub
So at any given point of time you need to use "valid" preceding object or property or else VBA will raise an error i.e. font expects Range to be its preceding object. If we write like below where wks is sheet object then code will go in error.
Code:
Dim wks as Worksheet
With wks
.Font.Bold = True
End With
I'd just hope that it makes things clear to you. Let me know if it confuses you more