Add text for under character limit - excel

Just wondering if this is possible, and how I would go about doing it... I'm not sure if I can use conditional formatting, or if it requires VBA, or what.
I have a vlookup that I split coding for, but it only works when they include ".000000.00000.0000.0000" at the end. How would I go about adding ".000000.00000.0000.0000" when the character limit is under 46 for that cell? The coding will always be 46 characters when the zeroes are included, so anything under is invalid.

I was able to use =IF(LEN(A2) < 23, "0" & LEFT(A2,15),LEFT(A2,16)) to truncate what I needed and add leading zeroes if not included to have the end result do what I needed without all of the zeroes.

try this
Sub test()
Dim i As Long
Dim text As String
For i = 1 To 32767
text = text & "a"
Next
text = text & "end"
Range("A1").Value = text
'"end" will be lost
End Sub

Related

How do you get the different ThemeFonts used in a text in excel?

In Excel, I have a text in a cell that has two different fonts. One of them is variable, be it arial or calibri, and the other is Symbol. I need to be able to detect the symbol font and what is within it.
I've tried to check for characters with values above 122, but these are just regular text values with a different font.
I tried using ThemeFont, such as
Dim WhatIsFont As ThemeFont
WhatIsFont = Worksheets("MySheet").Range("A1").Font.ThemeFont
But it returns Nothing when I this variable.
And if I use WhatIsFont as a String, it instead gives me a numeric value between 0 and 2 based on what kind of fonts and how many are used.
I cannot, for the life of me, find any other function that deals with ThemeFonts and know which ones exactly are being used.
Anyone knows how to do this?
Thank you.
After a while of playing around, I found an answer to my own question through the following code:
Sub TestFont()
'
' TestFont Macro
'
'
Dim i As Long
Dim r As Range
ThisWorkbook.Worksheets("Instructions").Range("C11").Select
For Each r In Selection
For i = 1 To Len(r.Value)
With r.Characters(i, 1).Font
MsgBox .Name
End With
Next i
Next r
End Sub
To answer my question then. ThemeFont won't cut it. Its .Font.Name that gives you the actual font being used here. By cycling through the several characters in a string - rather than investigate the string as a whole - you can pinpoint the Font name of each character, and thus from there single out the ones written in Symbol!

Remove leading zeros from textbox VBA

User inputs only numerical values in textbox name tbID, in my file, that column value is saved as numerical. However, if user input 00120 instead of 120. After attempting to remove the leading zeros, there seems to be a space before 120.
When I input Criteria1:="120" it works
tbIDf = Cint(tbID) 'Debug.Print tbIDf gives " 120" and renders my filter criteria to a blank
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=IDN, Criteria1:="tbID"
How do I remove both the leading zeros and the " " in front of 120?
Here is a thought, I would make sure data that makes it to your sheet is actually correct usable data. To do so you could use:
Trim(Val(tbID))
The use of Val would return a double value ignoring non-numeric values, 120. Alternatively CLng is also a possibility returning a long value. Just don't use CInt as there is no need to use integers (range only from -32,768 to 32,767)
The TRIM function will remove leading and trailing spaces.
You can/should also implement a keypress event described here to make sure only numerical values are entered.
This should work:
Option Explicit
Sub test()
'just for testing (this comes from your textbox)
Dim tbID As String
tbID = "00120"
If IsNumeric(tbID) Then 'test if numeric to prevent errors
Dim MyCriteria As String
MyCriteria = CStr(CLng(tbID))
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=IDN, Criteria1:=MyCriteria
End If
End Sub
If not use Trim(CStr(CLng(tbID)))

Extract text from a long string of text

I need to extract the following portion CDA-CUP-PF from the following string of
MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)
text
AddFormula TopLeft.Offset(1, 3).Resize(RowCount, 1), "=IFERROR(RIGHT(AA" & Row & ",FIND(""~"",AA" & Row & ")-1,FIND(""^"",AA" & Row & ")+1-FIND(""-"",AA" & Row & ")),"""")"
This is what I see right now: MECH^CHU
I need to see this: CDA-CUP-PF
I need to use something like the VBA code above.
Assuming your pattern is isolating the text in between ~ a formula solution is:
=MID(A1,FIND("~",A1)+2,FIND("~",A1,FIND("~",A1)+1)-FIND("~",A1)-3)
A VBA - UDF solution would look something like this
Public Function Isolate(x As Range)
Dim xString: xString = Split(x, "~")
Isolate = xString(1)
End Function
This formula will do what you want: =TRIM(MID(SUBSTITUTE(A1,"~",REPT(" ",255)),255,255))
It works by replacing ~ with 255 spaces, then it carves out 255 characters from 255 characters in (Which guarantees we get what you want) then it trims off the spare spaces.
If you want the other parts, use left or right instead of mid.
The UDF is a much better option though especially as you are doing this from code already.

How would I remove all leading alphabetical characters?

I am interested in removing leading alphabetical (alpha) characters from cells which appear in a column. I only wish to remove the leading alpha characters (including UPPER and LOWER case): if alpha characters appear after a number they should be kept. Some cells in the column might not have leading alpha characters.
Here is an example of what I have:
36173
PIL51014
4UNV22001
ZEB54010
BICMPAFG11BK
BICMPF11
Notice how there are not always the same number of leading alpha characters. I cannot simply use a Left or Right function in Excel, because the number of characters I wish to keep and remove varies.
A correct output for what I am looking for would look like:
36173
51014
4UNV22001
54010
11BK
11
Notice how the second to last row preserved the characters "BK", and the 3rd row preserved "UNV". I cannot simply remove all alpha characters.
I am a beginner with visual basic and was not able to figure out how to use excel functions to address my issue. How would I do this?
Here is an Excel formula that will "strip off the leading alpha characters" Actually, it looks for the first numeric character, and returns everything after that:
=MID(A1,MIN(FIND({0;1;2;3;4;5;6;7;8;9},A1&"0123456789")),99)
The 99 at the end needs to be some value longer than the longest string you might be processing. 99 usually works.
Here's a formula based solution complete with test results:
=MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"),255),100)
Change the 100 at the end if any string may be longer than 100 characters. Also the 255 is not needed, but it won't hurt.
This short UDF should strip off leading alphabetic characters.
Function noLeadAlpha(str As String)
If Not IsNumeric(str) Then
Do While Asc(str) < 48 Or Asc(str) > 57
str = Mid(str, 2)
If Not CBool(Len(str)) Then Exit Do
Loop
End If
noLeadAlpha = str
End Function
        
Koodos Jeeped, you beat me to it.
But here is an alternative anyway:
Function RemoveAlpha(aString As String) As String
For i = 1 To Len(aString)
Select Case Mid(aString, i, 1)
Case "0" To "9"
RemoveAlpha = Right(aString, Len(aString) - i + 1): Exit For
End Select
Next i
End Function

How to add leading zeros in Excel (timestamp with ms has no leading zeroes)

I’m still having a grave problem with some files. It’s a rather stupid problem, but I’ve been working at it for quite some time and can’t find a solution.
I need leading zeroes in the time stamps, at least on the ms level.
The timestamps that my software makes always look like this: (example)
9:55:1:19 (that is 9h, 55min, 1sec, 19 ms)
while what I need would look like
09:55:01:019
It’s no problem to make a conversion in Excel. I use
=VALUE(SUBSTITUTE(G2;":";",";3))
but I always get
09:55:01:190 (190ms!!)
Thus the milliseconds are always read like comma values, which is understandable from the software’s point of view.
I'd like a solution that either appends the correct values to the end of each row in a new column or directly changes the values in the original column (D) to the correct values. (Appending is OK because my other scripts work that way already!)
Can you help out really quickly?
https://www.dropbox.com/sh/3ch6ikddplnyjgg/vUfnVgbbzH here's an example file
With a value in A1 like:
0.413206238
the formula:
=SUBSTITUTE(TEXT(A1,"hh:mm:ss.000"),".",":")
will display:
09:55:01:019
EDIT#1:
Or if you want to convert the values in column D, in place. Select the cells and run this small macro:
Sub FFormat()
Dim r As Range, L As Long, U As Long
For Each r In Selection
ary = Split(r.Text, ":")
U = UBound(ary)
L = LBound(ary)
For i = L To U
If Len(ary(i)) = 1 Then
ary(i) = "0" & ary(i)
End If
Next i
If Len(ary(U)) = 2 Then
ary(U) = "0" & ary(U)
End If
r.Value = Join(ary, ":")
Next r
End Sub
If the original in R12 (my example) is text, you can enter this big formula: :)
=TEXT(LEFT(SUBSTITUTE(R12;":";REPT(" ";20));4);"00") & ":"&TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));14;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));34;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));54;20);"000")
Depending on your regional settings you may need to replace field separator "; " by ","
Your data in column G has a leading space - this formula in a new column should convert to a valid time value whether you have leading spaces or not
=LEFT(TRIM(G2);FIND("^";SUBSTITUTE(TRIM(G2);":";"^";3))-1)+LOOKUP(1000;RIGHT(G2;{1;2;3})+0)/86400000
format as [h]:mm:ss.000
This will cope with single or double digit milliseconds, assumes that if there are no milliseconds you will still have third : followed by a zero. Also copes with single digit hours, minutes or seconds

Resources