I need generate passwords that do not repeat and do not have the same consecutive number, in addition to having a length of 8 digits, only numbers. All this in Visual Basic for applications Excel.
Well, if you only want numbers, a random number between 0 and 9 can be achieved by Int(Rnd() * 10).
Int() rounds down, Rnd() returns a number between 0 and <1.
If you then loop that 8 times and add the numbers after each other (with & rather than +) with an If checking the last last added value to the one we want to add, then you have an 8 digit number with no repeating numbers.
Then you just have to print or save them somewhere.
Something like:
Sub random()
Dim test As Long, ran As Long, i As Long, j As Long
For j = 1 To 500 '- Amount of numbers to print
ran = 0
For i = 1 To 8 '- Length of random number
test = Int(Rnd() * 10)
If test = Right(ran, 1) Then
i = i - 1 '-Same number, try again
Else
ran = ran & test '-New number, add this
End If
Next i
Range("A" & Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row).Value = ran '- Print on last row of column "A"
Next j
Range("A:A").RemoveDuplicates Columns:=1 '- Remove duplicates from column "A"
End Sub
This would then print 500 random 8 digit numbers starting from A2, or the last empty cell in column A. Then proceed with removing the duplicates.
You could check for duplicates each time, and thus making sure you actually end up with the specified amount of numbers. But it's extremely slow, and the chance to get a duplicate is really small (testrun of 10.000 numbers had a single duplicate for me).
The context here is that I'm trying to identify phone number patterns of a big messy column, and format them as this:
(CC) NNNN-NNNN
CC being two digit area code
There may be two or more numbers in the same cell(They need to remain in the same cell unfortunately), and need to be as follow :
(CC) NNNN-NNNN / (CC) NNNN-NNNN
The numbers are just raw digits, no spaces or characters, but are as TEXT and need to remain so because of the 15 digit limit on Excel
Now, I'm having problem with two cases
Case 1:
Two phones and one area code (18 digits)
Example : CCNNNNNNNNNNNNNNNN
What I need is a function that takes the first two characters from this string and add them at the eleventh spot of said string, resulting in a 20 char string
Case 2:
One phone and two same area code(12 digits)
CCCCNNNNNNNN
This one just need to remove the first two characters
Tried this way for case 1, but ended up with a 22 string char? not sure where I went wrong
s1 = Mid(Cells(j, 3), 1, 2)
s2 = Mid(Cells(j, 3), 3, 10)
s3 = Mid(Cells(j, 3), 11, 18)
s4 = s1 & s2 & s1 & s3
The big caveat here is the formatting of the existing data.... how do you know what digits are "area code" and what digits are phone number? You can do what you are trying to do here with Excel formulas without any VBA... For Case 1 you can use the formula below, replacing "A1" with the appropriate cell reference.
="("&MID(A1, 1, 2)&") "&mid(A1,3,4)&"-"&mid(a1,7,4)&" / ("&MID(A1, 1, 2)&") "&mid(A1,11,4)&"-"&mid(a1,15,4)
For Case 2 you can use the formula below.
="("&MID(A1, 1, 2)&") "&mid(A1,5,4)&"-"&mid(a1,9,4)
Does your solution have to be in vba?
The third parameter in Mid is the length of the extracted substring, not its final index. If it is omitted - the selection goes to the end of the string. The selection also goes to the end of the string if the length parameter exceeds the number of possible characters. s2 = Mid(Cells(j, 3), 3, 10) was thus of length 10 rather than the expected 8. Your case 1 would look something like this:
Sub test()
Dim s As String, t As String
s = "CCNNNNNNNNNNNNNNNN"
t = Mid(s, 1, 10) & Mid(s, 1, 2) & Mid(s, 11)
Debug.Print t
End Sub
Which has output:
CCNNNNNNNNCCNNNNNNNN
For your two cases:
=IF(LEN(A1)=18,REPLACE(A1,11,0,LEFT(A1,2)),IF(LEN(A1)=12,MID(A1,3,10)))
Not knowing the other formats or desired results, it is hard to expand on the formula. The formula will return FALSE if the length is other than 12 or 18.
I have 1 column with about 60 cells with values or different length. Each (or at least most) of the values have a numeric characters in the value. I want to split the columns cells into more columns which I normally would do with the 'tekst to columns' function of excel.
But this function does not have an advanced option of splitting the value at the first numeric character. splitting based on spaces, comma etc. is possible but this does not help me.
Is there any way to divide the cells into 2 columns at the first number in the cell value?
I have looked at numerous other questions but none of them (or other internet fora) have helped me to split the value at the first number of the cell value.
Thanks #quantum285 for the answer. This routine works if the string contains one number. I changed the teststring to firstpart323secondpart.
then part1 returns 'firstpart32' and part2 return secondpart.
I tried to understand what happens in this code, please correct me if I'm wrong:
First, the lenght of the string is determined.
Secondly, for each position in this string is checked if it is numeric or not. But this check is dan from right to left? So in case of firstpart323secondpart: the length is 22.
then isnumeric() checks for every position from 1 to 22 if it is numeric and stops when it finds a number?
If so, part 1 is the the tekst before the value i, where i is the first position from right to left in the string where a number is found.
and part 2 is then the string on the right from this same position.
However, I am looking for a routine which find the first position from left to right (or the last position from right to left) where a number is, ...
So I changed the routine now, simply adjusting the for i = 1 to line:
Sub test()
For j = 4 To Cells(Rows.Count, 4).End(xlUp).Row
For i = Len(Cells(j, 4)) To 1 Step -1
If IsNumeric(Mid(Cells(j, 4), i, 1)) Then
Cells(j, 5) = Left(Cells(j, 4), i - 1)
Cells(j, 6) = (Right(Cells(j, 4), Len(Cells(j, 4)) - i + 1))
End If
Next i
Next j
End Sub
this almost perfectly works (except for a few cells which have multiple number combinations in the value (like: soup 10g 20boxes). But as these are only a few, I can adjust them by hand.
Thanks!
Sub test()
testString = "firstpart3secondpart"
For i = 1 To Len(testString)
If IsNumeric(Mid(testString, i, 1)) Then
part1 = Left(testString, i - 1)
part2 = (Right(testString, Len(testString) - i))
End If
Next i
MsgBox (part1)
MsgBox (part2)
End Sub
Use something like this within your loop.
I have some large numbers in an Excel sheet and I want to convert them to binary.
e.g.
12345678
965321458
-12457896
If we are talking positive number between 0 and 2^32-1 you can use this formula:
=DEC2BIN(MOD(QUOTIENT($A$1,256^3),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^2),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^1),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^0),256),8)
NOTE: =DEC2BIN() function cannot handle numbers larger than 511 so as you see my formula breaks your number into four 8-bit chunks, converts them to binary format and then concatenates the results.
Well, theoretically you can extend this formula up to six 8-bit chunks. Maximum precision you can get in Excel is 15 (fifteen) decimal digits. When exceeded, only the most significant 15 digits remain, the rest is rounded. I.e. if you type 12345678901234567 Excel will store it as 12345678901234500. So since 2^48-1 is 15 decimal digits long the number won't get rounded.
Perhaps a simpler option:
For positive numbers only, just use BASE (as in BASE2) for numbers between 0 to 2^53 in Excel. Here are some examples:
=BASE(3,2) # returns 11
=BASE(11,2) # returns 1011
Credit for answer goes here:
https://ask.libreoffice.org/en/question/69797/why-is-dec2bin-limited-to-82bits-in-an-32-and-64-bits-world/
Negative numbers: Come to think of it, negative numbers could be handled as well by building upon howy61's answer. He shifts everything by a power of two (2^31 in his case) to use the 2's complement:
=BASE(2^31+MyNum, 2)
so (using 2^8 for only 8 bits):
=BASE(2^8+(-1),2) # returns 11111111
=BASE(2^8+(-3),2) # returns 11111101
The numbers given by the OP requires more bits, so I'll use 2^31 (could go up to 2^53):
=BASE(2^31+(-12457896),2) # returns 11111111010000011110100001011000
For either positive or negative, both formulas could be coupled in a single IF formula. Here are two ways you could do it that give the same answer, where MyNum is the decimal number you start with:
=IF(MyNum<0, BASE(2^31+MyNum,2), BASE(MyNum, 2))
or
=BASE(IF(MyNum<0, MyNum+2^32, MyNum), 2)
See VBA posted here
' The DecimalIn argument is limited to 79228162514264337593543950245
' (approximately 96-bits) - large numerical values must be entered
' as a String value to prevent conversion to scientific notation. Then
' optional NumberOfBits allows you to zero-fill the front of smaller
' values in order to return values up to a desired bit level.
Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
DecToBin = ""
DecimalIn = CDec(DecimalIn)
Do While DecimalIn <> 0
DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(DecToBin) > NumberOfBits Then
DecToBin = "Error - Number too large for bit size"
Else
DecToBin = Right$(String$(NumberOfBits, "0") & _
DecToBin, NumberOfBits)
End If
End If
End Function
I just tried the formula above, and found that Microsoft screwed up the DEC2BIN function in another way that keeps the formula from working correctly with negative numbers. Internally, DEC2BIN uses a ten bit result; leading zeroes are dropped from the text result, unless the optional length parameter is used, in which case the required number of leading zeroes are left in the string. But here's the rub: a negative number always starts with a one, so there are no leading zeroes to drop, so DEC2BIN will always show all ten bits! Thus, DEC2BIN(-1,8), which should show 11111111 (eight ones) will instead show 1111111111 (ten ones.)
To fix this, use RIGHT to trim each eight bit chunk to eight bits, dumb as that sounds.
=RIGHT(DEC2BIN(QUOTIENT(A1,256^3),8),8) & RIGHT(...
(I read through the VBA, and it does not have the same problem, but it doesn't look like it will handle negatives at all.)
To add easier to read formatting to Taosique's great answer, you can also break it up into chunks of 4 bits with spaces in between, although the formula grows to be a monster:
=DEC2BIN(MOD(QUOTIENT($A$1,16^7),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^6),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^5),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^4),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^3),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^2),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^1),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^0),16),4)
1101 0100 1111 0110 0011 0001 0000 0001
Of course, you can just use the right half of it, if you're just interested in 16 bit numbers:
=DEC2BIN(MOD(QUOTIENT($A$1,16^3),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^2),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^1),16),4)&" "&DEC2BIN(MOD(QUOTIENT($A$1,16^0),16),4)
0011 0001 0000 0001
While I didn't write this for negatives or decimals, it should be relatively easy to modify. This VBA will convert any super large (or not so large if you want, but that wasn't the point) decimal up to the converted binary result containing up to 32767 digits (maximum string length in VBA).
Enter decimal in cell "A1" as a string, result will be in "B1" as a string.
Dim NBN As String
Dim Bin As String
5 Big = Range("A1")
AA = Len(Big)
For XX = 1 To AA
L1 = Mid(Big, XX, 1) + CRY
CRY = 0
If L1 = 0 Then
FN = "0"
GoTo 10
End If
If Int(L1 / 2) = L1 / 2 Then
FN = L1 / 2
GoTo 10
End If
If Int(L1 / 2) <> L1 / 2 Then
FN = Int(L1 / 2)
CRY = 10
GoTo 10
End If
10 NBN = NBN & FN
Next XX
If Left(NBN, 1) = "0" Then
NBN = Right(NBN, (Len(NBN) - 1))
End If
If CRY = 10 Then Bin = "1" & Bin Else Bin = "0" & Bin
Range("A1") = NBN
Range("A2") = Bin
If Len(NBN) > 0 Then
NBN = ""
CRY = 0
GoTo 5
End If
Someone can find binary shift operations more clear and relevant here
=DEC2BIN(BITRSHIFT($A$1,24),8) & DEC2BIN(MOD(BITRSHIFT($A$1,16),256),8) & DEC2BIN(MOD(BITRSHIFT($A$1,8),256),8) & DEC2BIN(MOD($A$1,256),8)
This formula is for 32-bit values
This vba function solves the problem of binary conversion of numbers greater than 511 that can not be done with WorksheetFunction.dec2bin.
The code takes advantage of the WorksheetFunction.dec2bin function by applying it in pieces.
Function decimal2binary(ByVal decimal2convert As Long) As String
Dim rest As Long
If decimal2convert = 0 Then
decimal2binary = "0"
Exit Function
End If
Do While decimal2convert > 0
rest = decimal2convert Mod 512
decimal2binary = Right("000000000" + WorksheetFunction.Dec2Bin(rest), 9) + decimal2binary
decimal2convert = (decimal2convert - rest) / 512
Loop
decimal2binary = Abs(decimal2binary)
End Function
=IF(Decimal>-1,BASE(Decimal,2,32),BASE(2^32+(Decimal),2))
Does both positive and negative numbers.
Took a bit LOL. Tech pun.
You're welcome.
Here's another way. It's not with a single formula, but I have tried and converted up to the number 2,099,999,999,999. My first intention was to build a 51 bit counter, but somehow it does not work with numbers beyond the one I mentioned. Download from
http://www.excelexperto.com/content/macros-production/contador-binario-de-51-bits/
I hope it's useful. Regards.
Without VBA and working with negative numbers as well (here: sint16), however, taking much more space:
You can download the excel file here: (sorry, didn't know where to put the file)
int16 bits to decimal.xlsx
or alternatively follow these steps (if your Excel is not in English, use Excel Translator to "translate" the formula into your MS Office language):
Enter the binary number in 4-bit nibbles (A4 = most significant to D4 = least significant) like shown in the screenshot. Enter all 4 digits (even if starting with 0) and format them as "text"
Enter formula in F4:
=IF(NUMBERVALUE(A4)>=1000,TRUE,FALSE)
Enter the letter "A" in G2-J2, "B" in K2-N2, "C" in O2-R2, "D" in S2-V2
Enter "1" in G3, K3, O3 and S3; "2" in H3, L3, P3 and T3; "3" in I3, M3, Q3 and U3; "4" in J3, N3, R3 and V3
In G4, enter:
=MID(INDIRECT(G$2&ROW()),G$3,1)
Copy the formula to H4-V4
In X4, enter:
=IF(G4="1",0,1)
Copy X4 to Y4-AM4
In BD3 enter "1"
In BC4, enter:
=IF((AM$4+BD3)=2,1,0)
IN BD4, enter:
=IF((AM$4+BD3)=2,0,IF((AM$4+BD3)=1,1,0))
Copy BD4 and BD4 and insert it 15 times diagonally one row further down and one column further left (like in the screenshot), i.e. insert it to BB5 and BC5, then BA6 and BB6, ..., AN19 and AO19.
In AO20, enter "=AO19"; in AP20, enter "=AP18" and so on until BD20 ("=BD4") - i.e. bring down the numbers into one line as seen in the screenshot
In BE20, enter (this is your result):
=IF(F4=FALSE,BIN2DEC(A4&B4)*2^8+BIN2DEC(C4&D4),-1*(BIN2DEC(AO20&AP20&AQ20&AR20&AS20&AT20&AU20&AV20)*2^8+BIN2DEC(AW20&AX20&AY20&AZ20&BA20&BB20&BC20&BD20)))
There maybe a simple solution. I have several 4.2 billion cells that are actually a negative Two's Complement and this works to get the correct value:
=SUM(2^31-(A1-2^31))
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