Hi there.
I've been dabbling with VBA for years now, though my code is little more than cobbled-together mishmash. As my scenarios get more complex, I'm increasingly aware of my need to step things up.
My code is inefficient and, all too frequently, relies upon my front-end knowledge of complex formulas. Which is to say, I frequently use VBA to poulate cells with built-in formulas and copy them down along a range. I know this is terribly inefficient.
Here's some sample code from a recent project, in which I populate cells with three different formulas and then fill them down:
The first and third formula are rather simple, the second one a little more complex.
I need to abandon this approach and learn how to convert front-end formulas into VBA functions. Since most of what I've learned is by studying the code of others, I figured I'd post this and see if anyone can demonstrate how to accomplish any of these three samples with a purely-VBA approach.
Thanks as always!
I've been dabbling with VBA for years now, though my code is little more than cobbled-together mishmash. As my scenarios get more complex, I'm increasingly aware of my need to step things up.
My code is inefficient and, all too frequently, relies upon my front-end knowledge of complex formulas. Which is to say, I frequently use VBA to poulate cells with built-in formulas and copy them down along a range. I know this is terribly inefficient.
Here's some sample code from a recent project, in which I populate cells with three different formulas and then fill them down:
Code:
Range("L2").Select
Range("L2").Value = "=COUNTIF(H:H,H2)"
'Dim lastRow As Long
lastRow = Columns(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
ActiveCell.AutoFill Destination:=Range(ActiveCell, Cells(lastRow, ActiveCell.Column))
Range("L2", Selection.End(xlDown)).Value = Range("L2", Selection.End(xlDown)).Value
Range("M2").Select
Range("M2").Value = "=IF(ISNUMBER(SEARCH(""\\ln"",G2)),(MID((G2&""\""),FIND(CHAR(135),SUBSTITUTE((G2&""\""),""\"",CHAR(135),5))+1,10000)),(SUBSTITUTE(G2,""\"",,1)))"
lastRow = Columns(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
ActiveCell.AutoFill Destination:=Range(ActiveCell, Cells(lastRow, ActiveCell.Column))
Range("M2", Selection.End(xlDown)).Value = Range("M2", Selection.End(xlDown)).Value
Range("N2").Select
Range("N2").Value = "=M2&I2&""\""&F2&""\""&B2"
lastRow = Columns(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
ActiveCell.AutoFill Destination:=Range(ActiveCell, Cells(lastRow, ActiveCell.Column))
Range("N2", Selection.End(xlDown)).Value = Range("N2", Selection.End(xlDown)).Value
The first and third formula are rather simple, the second one a little more complex.
I need to abandon this approach and learn how to convert front-end formulas into VBA functions. Since most of what I've learned is by studying the code of others, I figured I'd post this and see if anyone can demonstrate how to accomplish any of these three samples with a purely-VBA approach.
Thanks as always!