After using =Char(Mid(A1, 15, 1)), =Char(Mid(A1, 16, 1)), =Char(Mid(A1, 17, 1)), ... a few hundred times in years past, I made a form and wrote some code to list the contents of a cell in binary. It's been handy.
I recently added support for Unicode, so it may not be fully cooked (or maybe reheated), and double-width Unicode characters are problematic.
In addition to adding the attached form to the workbook, this needs to go in a code module.
Code:Sub CSB() ' Shortcut: Ctrl+Shift+B, as in ' "conveniently show binary" frmShowBinary.ShowBinary End Sub
Last edited by shg; 06-16-2009 at 12:16 AM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I use these functions in lieu of the volatile constructs like this:
ROW(INDIRECT("1:" & A1))
COLUMN(INDIRECT(A1 & ":" & A2))
... like this:
RowArr(1, A1)
ColArr(A1, A2)
... with the added benefit of having a step variable
RowArr(1, 10, 2)
... and you can have negative numbers
RowArr(10, -5, -3)
Here 'tis.
Last edited by shg; 06-16-2009 at 12:26 PM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Just thought I'd share ...
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Ever wonder about how binary (IEEE-754) floating point numbers (the source of all those mysterious precision problems) are actually represented? The attachment has a family of functions for converting between floating point numbers and hex strings. I used a couple of these in responding to http://www.excelforum.com/excel-prog...h-queries.html.
Most work in either VBA or as worksheet functions. Here's the list:
Code:' Routine Input Output WF/VBA ' -------- -------------------- -------------------------- ------ ' Byte2Sng ab(0 To 3) Single VBA ' Sng2Byte Single 4-byte array Variant Both ' Sng2Hex Single Hex String Both ' Hex2Sng Hex string Single Both ' Var2Sng 4-byte array Variant Single Both ' Routine Input Output WF/VBA ' -------- -------------------- -------------------------- ------ ' Byte2Dbl ab(0 To 7) Double VBA ' Dbl2Byte Double 8-byte array Variant Both ' Dbl2Hex Double Hex string Both ' Hex2Dbl Hex string Double Both ' Var2Dbl 8-byte array Variant Double Both ' Routine Input Output WF/VBA ' -------- -------------------- -------------------------- ------ ' Flt2Byte Single or Double 4- or 8-byte array Variant VBA ' Byte2Hex Byte array Hex string VBA
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I’ll play even though by comparison, I fear I’m a bit lowbrow.
I have a number of macros which are specific to my job – organising, running checks on and analysing data.
I have a macro listing names of files in a folder, and one which extracts data from files in a folder. I have a couple of template-style macros which contain little bits of code which I tend to use more often than most (e.g. Find).
I also have a number of very short macros which I’ve assigned to buttons on a toolbar. They’re so simple but they save so much time. For example, vertically centring text, removing colours, turning off AutoFilter, centring across selection, filling selected cells with random numbers.
My thought was that we could use this thread as a repository of sorts, and vector people here when one of the routines might solve a problem.
Those sound great! How making a separate post for each?I have a macro listing names of files in a folder, and one which extracts data from files in a folder. I have a couple of template-style macros which contain little bits of code which I tend to use more often than most (e.g. Find).
I also have a number of very short macros which I’ve assigned to buttons on a toolbar. They’re so simple but they save so much time. For example, vertically centring text, removing colours, turning off AutoFilter, centring across selection, filling selected cells with random numbers.
So -- post 'em up, Stephen!
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Mostly have a lot of little utility macros with shortcut keys if I use them often.
Here's one where I select all the cells in my workbook that I want to have a specific # of digitsCode:Sub AddDecimals() Dim x As Integer x = Application.InputBox("No of Decimals !!", "Add Decimals", Type:=2) If x = 0 Then Selection.NumberFormat = "0" & WorksheetFunction.Rept(0, x) Else Selection.NumberFormat = "0." & WorksheetFunction.Rept(0, x) End If End Sub
ChemistB
My 2¢
Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)
And here's one that Inserts a worksheet and lists all the formulas off the active sheet and what cells they are inCode:Sub ListFormulas() Dim FormulaCells As Range, Cell As Range Dim FormulaSheet As Worksheet Dim Row As Integer ' Create a Range object for all formula cells On Error Resume Next Set FormulaCells = Range("A1").SpecialCells(xlFormulas, 23) ' Exit if no formulas are found If FormulaCells Is Nothing Then MsgBox "No Formulas." Exit Sub End If ' Add a new worksheet Application.ScreenUpdating = False Set FormulaSheet = ActiveWorkbook.Worksheets.Add FormulaSheet.Name = "Formulas in " & FormulaCells.Parent.Name ' Set up the column headings With FormulaSheet Range("A1") = "Address" Range("B1") = "Formula" Range("A1:B1").Font.Bold = True End With ' Process each formula Row = 2 For Each Cell In FormulaCells Application.StatusBar = Format((Row - 1) / FormulaCells.Count, "0%") With FormulaSheet Cells(Row, 1) = Cell.Address _ (RowAbsolute:=False, ColumnAbsolute:=False) Cells(Row, 2) = " " & Cell.Formula Row = Row + 1 End With Next Cell ' Adjust column widths FormulaSheet.Columns("A:B").AutoFit Application.StatusBar = False End Sub
ChemistB
My 2¢
Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)
I use this one a lot before protecting my workbook. It selects all unlocked cells.Code:Sub SelectUnlockedCells() Dim WorkRange As Range Dim FoundCells As Range Dim Cell As Range Set WorkRange = ActiveSheet.UsedRange For Each Cell In WorkRange If Cell.Locked = False Then If FoundCells Is Nothing Then Set FoundCells = Cell Else Set FoundCells = Union(FoundCells, Cell) End If End If Next Cell If FoundCells Is Nothing Then MsgBox "All cells are locked." Else FoundCells.Select End If End Sub
ChemistB
My 2¢
Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)
Mine are mostly specific to my workplace, like changing Csv files to a nicely Formatted BOM form.. or actually consolidating quantities in CSV files to BOM's, etc..
But here is a simple one that I am sure people have already access to, I use to delete blank rows...
Code:Sub DeleteBlankRows() ' Deletes the entire row within the selection if the ENTIRE row contains no data. ' Revised March 14, 2007 - added For/Next loop to ensure "empty appearing" cells are cleared. On Error Resume Next Dim i As Long Dim c As Range, OrigRange As Range With Application .Calculation = xlCalculationManual .ScreenUpdating = False ' Set original selection for later use Set OrigRange = Selection ' clear contents of seemingly empty cells that are really not empty ' This version formats text as general With ActiveSheet .UsedRange.Value = Evaluate("IF(ROW(" & .UsedRange.Address & "),CLEAN(" & .UsedRange.Address & "))") End With ' This version keeps cell formats as text ' Selection.SpecialCells(xlCellTypeConstants, 23).Select ' ' For Each c In Selection ' If Len(c) = 0 Then c.ClearContents ' Next c 'Delete blank rows starting at bottom of selection For i = OrigRange.Rows.Count To 1 Step -1 If WorksheetFunction.CountA(OrigRange.Rows(i)) = 0 Then OrigRange.Rows(i).EntireRow.Delete End If Next i Range("A1").Select .Calculation = xlCalculationAutomatic .ScreenUpdating = True End With End Sub
Microsoft MVP - Excel
Where there is a will there are many ways. Pick One!
Please read the Forum Rules
If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below
Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.
Preferred Charities: Lupus Canada and Sick Kids Foundation.
Feel Free to Donate if you want to, for the assistance you received today.
I just wanted to say I don't use a Personal.xls... adding no value to the thread per se just thought I should point out how odd I am... and wondered if anyone else out there like me... I do have a libary of exported modules (.bas) which I import to files as and when but that's about it (along with some old .xla's)...
Interesting thread... cheers shg.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
You're in good company -- Andy Pope doesn't use one.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Maybe it's just because we're both "Essex Boys" ...
(I might live in Suffolk in the UK but I'm a born & bred Essex Boy... note to non-Brits - that's not a good thing in terms of sterotypes...)
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
I had a friend in Essex who was continually at pains to point out that he lived in the posh bit (which was virtually Suffolk).
I don't claim authorship of any of these except the really short ones, some of which could probably be done with keyboard shortcuts but originate from when I first discovered VBA and wanted to do everything with macros.
Code:Sub Filenames() Dim sName As String, vNames(), sType As String, sPath As String, i As Long ' Change folder to suit sPath = "C:\Documents and Settings\StephenR\" ChDrive sPath ChDir sPath sType = "*.xls" sName = Dir(sType) Do Until sName = "" i = i + 1 ReDim Preserve vNames(1 To i) vNames(i) = sName sName = Dir Loop If i = 0 Then MsgBox "No files found" Else For i = 1 To UBound(vNames) Cells(i, 1) = vNames(i) ' To remove .xls suffix ' Cells(i, 1) = Left(vNames(i), Len(vNames(i)) - 4) Next i End If End SubCode:Sub LoopThroughFolder() Dim nCount As Long, wbResults As Workbook, wbCodeBook As Workbook With Application .ScreenUpdating = False: .DisplayAlerts = False: .EnableEvents = False End With On Error Resume Next Set wbCodeBook = ThisWorkbook With Application.FileSearch .NewSearch ' Change path to suit .LookIn = "C:\MyDocuments\TestResults" .FileType = msoFileTypeExcelWorkbooks ' .Filename = "Book*.xls" ' Workbooks in folder If .Execute > 0 Then ' Loop through all For nCount = 1 To .FoundFiles.Count Set wbResults = Workbooks.Open(Filename:=.FoundFiles(nCount), UpdateLinks:=0) ' do whatever wbResults.Close SaveChanges:=True Next nCount End If End With On Error GoTo 0 With Application .ScreenUpdating = True: .DisplayAlerts = True: .EnableEvents = True End With End SubCode:Sub FindTemplate1() Dim rFind As Range, sFind As String, sAddress As String sFind = "Word" With Sheet1.Columns(1) Set rFind = .Find(What:=sFind, After:=rFind, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If Not rFind Is Nothing Then sAddress = rFind.Address Do ' do whatever Set rFind = .FindNext(rFind) Loop While rFind.Address <> sAddress ' Loop Until rFind Is Nothing - use this if found values are being deleted or moved outside rFind End If End With End SubCode:Sub VerticalCentre() Selection.VerticalAlignment = xlCenter End Sub Sub Wrap() Selection.WrapText = Not Selection.WrapText End Sub Sub RemoveFilter() ActiveSheet.AutoFilterMode = False End Sub Sub NumbComma() Selection.NumberFormat = "#,##0" End Sub Sub NoColour() Selection.Interior.ColorIndex = xlNone End Sub Sub CentreSel() Selection.HorizontalAlignment = xlCenterAcrossSelection End Sub Sub TxtBox() ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 575.25, 42.75, 250#, 200).Select End Sub Sub ClearHyperlinks() Dim rng As Range For Each rng In Selection rng.Hyperlinks.Delete Next rng End Sub Sub UnhideHiddenSheets() Dim ws As Worksheet For Each ws In Worksheets If ws.Visible = False Then ws.Visible = True End If Next ws End Sub Sub CorrectBlank() ' Corrects Value error caused by empty cell regarded as text Dim rng As Range For Each rng In Selection If Len(rng) = 0 Then rng.Value = rng.Value End If Next rng End Sub Sub BevelButton() ActiveSheet.Shapes.AddShape(msoShapeBevel, 500#, 100, 93#, 34.5).Select With Selection .Characters.Text = "PRESS ME" .Font.Bold = True .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .ShapeRange.Fill.ForeColor.SchemeColor = 46 End With End Sub Sub DelimitSpace() On Error GoTo errline Range(Selection, Selection.End(xlDown)).TextToColumns Destination:=Selection(1, 1), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False, _ Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True Exit Sub errline: MsgBox "No data selected" End Sub Sub RandomNumbers() Dim rng As Range, c As Long Application.ScreenUpdating = False On Error GoTo errline For Each rng In Selection rng.Value = Int(Rnd() * 100) Next rng With Selection For c = 1 To .Columns.Count .Cells(1, c) = "Heading " & c Next c .Cells.HorizontalAlignment = xlRight End With Application.ScreenUpdating = True Exit Sub errline: MsgBox "Invalid selection" End Sub
Are we friends but don't know we're friends ??? Me too...I had a friend in Essex who was continually at pains to point out that he lived in the posh bit (which was virtually Suffolk).
(If he was indeed a he, and he continuously referred to his hometown as the oldest recorded town & former Roman Capital of Britain (Camulodunum) and former home to Damon Albarn of Blur then there's a good chance we know each other...)
Promise I won't darken the thread further unless I have something worthy to add. Sorry.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks