general purpose VBA routines that you find useful ?
general purpose VBA routines that you find useful ?
(OP)
I'd be interested to hear if you have any general purpose excel vba routines that you find especially helpful
The one I'll present is one that was discussed in another thread thread770-485250: Excel formula help please
In there I had presented a cell highlighter routine so you can keep track of the current cell if you have multiple windows open (excel's built in current-cell highlighter disappears if you shift focus to another window).
That previous version did have the undesirable characteristic of wiping out some existing formatting of the sheet.
3DDave made a comment which suggested that conditional formatting could overcome that limitation.
I have redone that code to use conditional formatting for highlighting, and that fixed the problem, it doesn't interfere with formatting (minor exception at the end):
Below is the new code, which should be pasted into the code area for a particular sheet, NOT into a module area.
Let me know if you have any problem with it. And let us know your own favorite general purpose vba routines
=====================================
(2B)+(2B)' ?
The one I'll present is one that was discussed in another thread thread770-485250: Excel formula help please
In there I had presented a cell highlighter routine so you can keep track of the current cell if you have multiple windows open (excel's built in current-cell highlighter disappears if you shift focus to another window).
That previous version did have the undesirable characteristic of wiping out some existing formatting of the sheet.
3DDave made a comment which suggested that conditional formatting could overcome that limitation.
I have redone that code to use conditional formatting for highlighting, and that fixed the problem, it doesn't interfere with formatting (minor exception at the end):
- It doesn't wipe out any existing permanent formatting.
- It doesn't wipe out any existing conditional formatting (I wasn't as sure of this, but my experimentation supports the conclusion.
- It doesn't interfere with using the paintbrush formatter tool to paste format from current selection to another location in the same sheet.
- ...(even though you can't see the format while your cursor is in that cell due to the highlighting).
- It DOES interfere with using the paintbrush formatter tool to paste format from current selection to another location in a DIFFERENT sheet
- ... (because the target sheet doesn't have the same macro to help clear out that cell highlighting format)
- ... I don't think it's a big problem, just don't paste formats from the highlighter sheet into a different sheet
- ... ... the fact that you have a big yellow highlight in the cell is an obvious clue that naturally reminds you to think about effects of copying format from that cell
Below is the new code, which should be pasted into the code area for a particular sheet, NOT into a module area.
Let me know if you have any problem with it. And let us know your own favorite general purpose vba routines
CODE
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) ' Current Cell row/column highlighter ' update 072121 - uses conditional formating, and "union" ' Usage note - if for some reason a cell remains highlighted ' ... which is not the current cell, then fix it by ' ... clicking on that cell and then clicking on any other cell Static lastRow, lastCol ' Holds the cell coordinates from last call to sub Dim mySheet As Sheet1 Dim thisRange, lastRange As Range Dim thisFC, lastFC As FormatCondition Set mySheet = Target.Parent ' Clear highlighting from last (previous) cell from last call to this function: If lastRow <> "" Then ' don't proceed if empty values (when initially open workbook) Set lastRange = Union(mySheet.Rows(lastRow), mySheet.Columns(lastCol)) ' builds a range that highlights both row and column of last cell With lastRange For Each lastFC In .FormatConditions If lastFC.Type = xlExpression And (lastFC.Formula1 = "=ROW()>0") Then lastFC.Delete End If Next lastFC End With End If ' Highlight current cell: Set thisRange = Union(mySheet.Rows(Target.Row), mySheet.Columns(Target.Column)) ' builds a range that highlights both row and column of current cell Set thisFC = thisRange.FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()>0") With thisFC .SetFirstPriority With .Interior .Color = vbYellow .Pattern = xlSolid End With End With ' Save lastRow and lastCol for the next call... lastRow = Target.Row lastCol = Target.Column End Sub
=====================================
(2B)+(2B)' ?
RE: general purpose VBA routines that you find useful ?
It supplements Excel's built-in unit conversion function. The built-in CONVERT function is quite nice with lots of units, although some options are bit weird. Not often I want to convert km to angstroms or light years. It has many basic conversions, but lacks flow rates, velocities and a lot of units particular to engineering work. It also isn't very good at giving you hints of what built-in conversion units are available and the unit abbreviations are hard to remember.
So... I wrote a module that first tries to convert my number using Excel's built-in units, but when it does not find MMCFD to m3/h, it checks my custom unit conversions and gives me the answer I want in my desired units. I also included a form with a button that lists all XL built-in units and their abbreviations for easy reference. With one leg in the US and the other in Europe, I find it is pretty helpful. Lbm/hr to kg/s, or gal/m to m3/h, W/m2 to BTU/ft2-s ... directly. No muss, no fuss.
RE: general purpose VBA routines that you find useful ?
TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg
FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm
RE: general purpose VBA routines that you find useful ?
=====================================
(2B)+(2B)' ?
RE: general purpose VBA routines that you find useful ?
1. A "Paste as Text" macro. For a spectra analysis calc, I pull the data from a standard website and the bring it to the line and paste as text. It saves me about 4-5 keystrokes and clicking through the menu.
2. A "Clear Inputs" macro. Basically helps me start a sheet fresh.
Both were created with recording, so I actually didn't have to code anything. Once I get some time I would like to start getting more coding going on to limit some of my calcs in cell. It's a slow process.
RE: general purpose VBA routines that you find useful ?
TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg
FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm
RE: general purpose VBA routines that you find useful ?
1,000,000,000 m3/yr = = 1E-39 m2LY
Mass rates get a little tricky.
RE: general purpose VBA routines that you find useful ?
TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! https://www.youtube.com/watch?v=BKorP55Aqvg
FAQ731-376: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers Entire Forum list http://www.eng-tips.com/forumlist.cfm
RE: general purpose VBA routines that you find useful ?
ElectricPete will recognise it, because he contributed to its evolution.
I thought I had put into my FAQ Archive, but if I did it has since disappeared.
CODE
RE: general purpose VBA routines that you find useful ?
Cubic, quartic and higher order polynomial equation solvers.
Brent's method solver (variation of Newton's method).
Section properties for defined shapes and from coordinates.
Intersection of lines defined by a series of points.
Evaluate formulas entered as text on the spreadsheet.
Unit conversion and formula evaluation with units.
Linear algebra functions, including solving large matrix equations.
Draw images to scale from coordinates.
Code for all the above can be found on the blog, but let me know if there is something specific you would like a link to.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: general purpose VBA routines that you find useful ?
https://www.eng-tips.com/faqs.cfm?fid=1901
It's under the FAQ column in the list of all posts, rather than the FAQ archive.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: general purpose VBA routines that you find useful ?
It was a bit like the house keys, or my reading glasses: I knew I'd put it somewhere but couldn't remember where.
RE: general purpose VBA routines that you find useful ?
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: general purpose VBA routines that you find useful ?
Spoiler:
Option Explicit
Option Base 1
Public Function Disfor(x, Optional precision = 10)
Dim CallerSheet As String, CallerBook As String
Dim OriFor As String, FormularLen As Integer
Dim MathOp As Variant, i As Integer
Dim SearchStart As Integer, k As Integer
Dim SwapOp As Boolean, LastK As Integer
Dim CellAdd As String, Genfor As String, GenOpt As String
Dim Variable As Variant
Dim Bf As String, Md As String, Af As String
Static AlreadyOpen As Boolean
Application.Volatile
CallerSheet = Application.Caller.Parent.Name
CallerBook = Application.Caller.Parent.Parent.Name
MathOp = Array("=", "+", "-", "*", "/", "^", ",", ":", "<", ">")
OriFor = x.Formula
FormularLen = Len(OriFor)
ReDim Operator(FormularLen) As Integer
If Left$(OriFor, 1) <> "=" Then
OriFor = "=" + OriFor
FormularLen = FormularLen + 1
End If
'Replace ":\" with "|\"
SearchStart = 1
Do While InStr(SearchStart, OriFor, ":\") > 0
k = InStr(SearchStart, OriFor, ":\")
Mid$(OriFor, k, 2) = "|\"
SearchStart = SearchStart + 1
Loop
k = 1
For i = 1 To UBound(MathOp) '10 math operators (include ',' and ':')
SearchStart = 1
Do While InStr(SearchStart, OriFor, MathOp(i)) > 0
Operator(k) = InStr(SearchStart, OriFor, MathOp(i))
SearchStart = Operator(k) + 1
k = k + 1
Loop
Next i
Operator(k) = FormularLen + 1
LastK = k
'Sort Operator()
Do
SwapOp = False
For i = 1 To LastK - 1
If Operator(i) > Operator(i + 1) Then
'swap Operator(I), Operator(I + 1)
k = Operator(i)
Operator(i) = Operator(i + 1)
Operator(i + 1) = k
SwapOp = True
End If
Next i
Loop Until SwapOp = False
Genfor = ""
For i = 1 To LastK - 1
CellAdd = Mid$(OriFor, Operator(i) + 1, Operator(i + 1) - Operator(i) - 1)
GenOpt = Mid$(OriFor, Operator(i), 1)
Call CheckBK(CellAdd, Bf, Md, Af)
GenOpt = GenOpt + Bf
CellAdd = Md
If CellAdd <> "" Then
FormularLen = Len(CellAdd)
' For K = 1 To FormularLen
' Next K
Call ObtainValue(CellAdd, CallerSheet, CallerBook, Variable, precision)
Else
Variable = ""
End If
Select Case GenOpt
Case Is = ":"
GenOpt = " to "
'GenOpt = "->"
'Case Is = "*"
' GenOpt = "x"
End Select
Genfor = Genfor + GenOpt + Variable + Af
Next i
'Set supercript
Genfor = Right$(Genfor, Len(Genfor) - 1)
Call ReplaceConstant(Genfor)
If Left$(Genfor, 1) = "+" Then
Disfor = Right$(Genfor, Len(Genfor) - 1)
Else
Disfor = Genfor
End If
'Replace "|\" with ":\"
SearchStart = 1
Do While InStr(SearchStart, Disfor, "|\") > 0
k = InStr(SearchStart, Disfor, "|\")
Mid$(Disfor, k, 2) = ":\"
SearchStart = SearchStart + 1
Loop
End Function
Sub CheckBK(ForStr As String, Bf As String, Md As String, Af As String)
'Check for Brackets ie. '()'
'Bf = before '('
'Md - between '('&')'
'Af -after ')'
Dim i As Integer, k As Integer, L As Integer
Dim Opb As Integer, Clb As Integer
L = Len(ForStr)
k = 0
Do While InStr(k + 1, ForStr, "(") > 0
k = k + 1
Loop
Opb = k
Clb = InStr(1, ForStr, ")")
If Clb = 0 Then Clb = L + 1
Bf = Left$(ForStr, Opb) ': Print "bf="; "@"; Bf$; "@"
Md = Mid$(ForStr, Opb + 1, Clb - Opb - 1) ': Print "md="; "@"; Md$; "@"
Af = Right$(ForStr, L - Clb + 1) ': Print "Af="; "@"; Af$; "@"
End Sub
Sub RmvU(Md)
'Remove unwanted formating in number format string
Dim L As Integer, NewMd As String, i As Integer
Dim TempMd As String
NewMd = ""
L = Len(Md)
For i = 1 To L
TempMd = Mid(Md, i, 1)
Select Case TempMd
Case Is = "?"
'do nothing
Case Is = "_"
i = i + 1
Case Else
NewMd = NewMd + TempMd
End Select
Next i
Md = NewMd
End Sub
Sub ObtainValue(CellAdd, CallerSheet, CallerBook, Variable, precision)
On Error GoTo NonAdd
Dim Md As String
Dim VarAdd As Object
'Print #1, CellAdd, TypeName(Range(CellAdd).Value)
If Asc(Left$(CellAdd, 1)) < 58 And Left$(CellAdd, 1) <> "$" Then
Variable = CellAdd
'Print #1, "no address ", CellAdd
Else
If InStr(CellAdd, "!") = 0 Then
'Set VarAdd = Workbooks(CallerBook).Worksheets(CallerSheet).Range(CellAdd)
Set VarAdd = Workbooks(CallerBook).Worksheets(Range(CellAdd).Parent.Name).Range(CellAdd)
Else
Set VarAdd = Range(CellAdd)
End If
'With VarAdd
'Md = TypeName(Worksheets(CallerSheet).Range(CellAdd).Value)
Md = TypeName(VarAdd.Value)
If Md = "Empty" Or Md = "Null" Or Md = "Error" Or Md = "String" Then
Variable = Md
Exit Sub
End If
'Print #1, CellAdd, Range(CellAdd).Value
'Variable = Str$(Range(CellAdd).Value)
'Variable = Str$(Range(CellAdd).Value)
'Md = Worksheets(CallerSheet).Range(CellAdd).NumberFormat
Md = VarAdd.NumberFormat
Call RmvU(Md)
'Print #1, CellAdd
If Md = "General" Then
'Variable = Str$(Worksheets(CallerSheet).Range(CellAdd).Value)
Variable = Str$(Round(VarAdd.Value, precision))
Else
'Variable = Format$(Worksheets(CallerSheet).Range(CellAdd).Value, Md)
Variable = Format$(Round(VarAdd.Value, precision), Md)
End If
'End With
End If
Exit Sub
NonAdd:
Variable = "Address Error"
On Error GoTo 0
End Sub
Sub ReplaceConstant(Genfor)
'Replace PI() with 3.142
Dim i As Integer, L As Integer, k As Integer
L = Len(Genfor)
For i = 1 To L
If InStr(i, Genfor, "PI()") > 0 Then
k = InStr(i, Genfor, "PI()")
Mid$(Genfor, k, 4) = "3.142"
End If
Next i
End Sub
RE: general purpose VBA routines that you find useful ?
RE: general purpose VBA routines that you find useful ?
https://interactiveds.com.au/software/Eval2.ZIP
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: general purpose VBA routines that you find useful ?
I wonder how hard it would be to put all numbers into a common format, like scientific with 2 decimal places.
=====================================
(2B)+(2B)' ?
RE: general purpose VBA routines that you find useful ?
RE: general purpose VBA routines that you find useful ?
=====================================
(2B)+(2B)' ?
RE: general purpose VBA routines that you find useful ?
Private Sub Workbook_Activate()
Application.CalculateFull
End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.CalculateFull
End Sub
Also, extensive use of this function slows down the worksheet, especially when running VBA routines.