How to increment different numbers in a text in Excel? - excel

I have to fill about 2k rows in excel which would hurt my brain to do it manually... But after 4hours of searching and trying to solve this problem I'm really close to give it up.
Here is a simple example: A101.0 which is used as a code for a position. The .0 is going to .0 to .6, then the 01 should increase by 1 to look like A102.0 and so on. The 01 which is before the . is going to 57 so the last would look like A157.6. And after this the number near the A should go up to 2, like this. A201.0 and starting over the cycle again.
The very last should looks like this: A657.6
So A is fix, first number is going 1 to 6, the 2 next to it is going 01 to 57 for every "1 to 6" and the last number after the dot is going 0 to 6 for every "01 to 57" number.
I wrote a little macro but not working so well...
Sub Makro1()
Dim i As Integer
Dim j As Integer
Dim k As Integer
For k = 1 To 6
For j = 1 To 57
For i = 1 To 7
Munka1.Cells(i * j * k, 10).Value = "A" & k & j & i - 1
Next i
Next j
Next k
End Sub
If there is any solution whithout vba it would be good also.

I think your macro is fine, it's just this: i * j * k that is bad. Multiplying those numbers together is not going to give you an integer that corresponds to an excel row. Instead you'll need one more variable to track the row to which you write the incremented value:
Sub Makro1()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim writeRow as Integer
writeRow = 1
For k = 1 To 6
For j = 1 To 57
For i = 1 To 7
Munka1.Cells(writeRow, 10).Value = "A" & k & j & "." & i - 1
writeRow = writeRow + 1
Next i
Next j
Next k
End Sub
Updated to add: I also stuck a & "." & in there to include the decimal in your outputted string location value.

If you have Excel O365 with the SEQUENCE function, you can enter, in a single cell, the formula:
=TEXT((INT(SEQUENCE(6*57*7,,0)/(57*7))+1)*100+(MOD(INT(SEQUENCE(6*57*7,,1,1/7))-1,57)+1)+MOD( SEQUENCE(6*57*7,,0),7)/10,"A000.0")
and it will spill down to create the series you describe.

I think this will give you what you want. Stringing numbers together the way you are is going to cause problems when k jumps from 9 to 10 as the k string is now 2 digits:
You can build the number numerically instead and then force it into a format of your choosing:
Sub Makro1()
Dim i As Long, j As Long, k As Long, y As Long
y = 1 ' y is used as the writing pointer
For k = 1 To 6
For j = 1 To 57
For i = 0 To 6
Munka1.Cells(y, 10).Value = "A" & Format((k * 100) + j + (i / 10), "000.0")
y = y + 1
Next i
Next j
Next k
End Sub
Finally, if you really wanted to concatenate your numbers instead of using math(s), you could use:
Munka1.Cells(y, 10).Value = "A" & k & Right("0" & j, 2) & "." & i

"If there is any solution whithout vba it would be good also."
Instead of VBA, you could use the following formula in A1 and drag down:
=IF(ROUNDUP(ROW()/399,0)>6,"","A"&ROUNDUP(ROW()/399,0)&TEXT(MOD(ROUNDUP(ROW()/7,0)-1,57)+1,"00")&"."&MOD(ROW(A1)-1,7))
I stuck the IF in there to start showing blanks after A657.6 or rather, row 2395.

Related

Find 2 elements in an array that give a target result in a simple equation

In electronics I need a certain resistance, however there are limited standard values on the market. If I make a combination of two resistors in parallel, I could get a very close result to the target. The question is which 2 resistors could give the best result.
So far i have these elements:
Excel column of 86 standard values from 0.25 to 1000000 in A2:A87 cells
Target resistance that I enter in cell C3
R1 and R2 - variables that can get 86 values from A2 to A87 cells
equation giving the parallel resistance: Rtarget=R1*R2/(R1+R2)
I am a VBA ignorant and find this task as a good opportunity to learn more about VBA operations. Here I started with the variables, but I do not know how to proceed. Your help would be very much appreciated.
Sub ResCom()
Dim Rstd As Double
Dim Rt As Double, R1 As Double, R2 As Double
Rstd = Range("a2:a87").Value
.
.
.
End Sub
Thanks in advance!
We can create a tool that can easily be reused.
First enter the resistance values in A2 through A87. Then run:
Sub tablemaker()
Dim i As Long, j As Long, K As Long
K = 2
For i = 2 To 87
For j = i To 87
Cells(K, 2) = Cells(i, 1)
Cells(K, 3) = Cells(j, 1)
K = K + 1
Next j
Next i
Range("D2:D" & K - 1).Formula = "=B2*C2/(C2+B2)"
End Sub
This will create a resistance table in B2 through D3742. The table lists combination pairs and the associated resistance of each pair.
(permutations are not need since the pair 4,2 is really the same as 2,4)
Then enter the target value in cell E2. In E3 enter the array formula:
=MATCH(MIN(ABS(D2:D3742-E2)),ABS(D2:D3742-E2),0)
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.
This gives the row in the table with the closest match.Finally in E4 enter:
=INDEX(B2:B3741,$E3)
and copy this to F4
To re-run this, just change the target in E2
Just to get you started, use this to continue
Option Explicit
Sub ResCom()
Dim Rstd As Double
Dim Rt As Double, R1 As Double, R2 As Double
Rstd = Range("a2:a87").Value
' step thru upper triangle of R1/R2 values
' (lower triangle gives same results)
Dim r1Row As Long, r2Row As Long
For r1Row = LBound(Rstd) To UBound(Rstd)
For r2Row = r1Row To UBound(Rstd)
' here is where you solve for Rtarget=R1*R2/(R1+R2)
' R1 = Rstd(r1Row)
Next r2Row
Next r1Row
End Sub
Try this
Sub ResCom()
Dim Rt as Double, R1 as Double, R2 as Double
Rt = Range("C3").Value
For i = 2 to 87
R1 = Range("A" & i).Value
if R1 < Rt Then
For j = 2 to 87
R2 = Range("A" & j).Value
if Round(Rt,2) = Round((R1*R2/(R1+R2)),2) then
' Success you have found a Resistance Combination that matches
End If
Next j
End If
Next i
End Sub
...

Excel formula to find the X nearest combination of values to a certain value combination

In Excel, we have 3 columns with values in them Length, Width, and Height of objects. Each row stands for 1 object, now we need to find the X number of objects that fit the most together.
For Example:
Now let's say we need the 2 closest sets of values, the output should give Nbr. 1 and 2 because [abs(11-10) + abs(9-8) + abs(4-5)] is the smallest value you'll get. Here we needed to find the 2 closest but sometimes we need to find the 25 closest sets of values.
Moreover, some might find Height not important or really important so you may want to lower or heighten its importance by adding in factors F(1,2,3) with which you multiply a part of the formula:
[F1 * abs(11-10) + F2 * abs(9-8) + F3 * abs(4-5)]
I tried to find the first (second, third and so on) smallest difference in value which works for just one variable but not multiple ones at the same time with:
=INDEX(A$2:A$5002,MATCH(SMALL(ABS(B$2:B$5002-B2),2),ABS(B$2:B$5002-B2),0))
I donĀ“t know an Excel formula to solve this problem as I want to find the X nearest combination of values.
I expect a result showing the row numbers of X objects that fit the best together.
I Created a VBA Excel code that seems to work pretty nice in finding out which value combinations fit best together. It shows the closest values and the sum of all differences to determine the combination that works best. I did remove the first row of the picture in the description though. If anyone can improve, please do.
Sub test()
Dim last_row As Long
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim L As Integer
Dim W As Integer
Dim H As Integer
Dim AantalGelijken As Integer
Dim FL As Integer
Dim FW As Integer
Dim FH As Integer
FL = 1
FW = 1
FH = 0.8
AantalGelijken = InputBox("How many comparable sets do you need", "Number")
L = 2
W = 3
H = 4
For i = 1 To last_row
For j = 1 To last_row
If i <> j Then
Cells(j, 5).Value = FL * Abs(Cells(i, L) - Cells(j, L)) + FW * Abs(Cells(i, W) - Cells(j, W)) + FH * Abs(Cells(i, H) - Cells(j, H))
End If
Next j
For k = 1 To AantalGelijken
Cells(i, 5 + k) = WorksheetFunction.Index(Range("A1:A9999"), WorksheetFunction.Match(WorksheetFunction.Small(Range("E1:E9999"), k), Range("E1:E9999"), 0))
Cells(i, AantalGelijken + 6) = Cells(i, AantalGelijken + 6) + WorksheetFunction.Small(Range("E1:E9999"), k)
Next k
Range("E1:E9999").Clear
Next i
End Sub
`

Excel macro for data reformatting

I need to reformat a 33500 row of data in excel. I am trying to write a macro that would do this for me.
I have put some nested loop to solve the issue
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim K As Integer
Dim p As Integer
Dim c As Integer
For c = 0 To 10
For n = 5 To 10
K = 14 + 7 * (n - 5)
For i = 0 To 7
m = 14 + 8 * c
ActiveSheet.Cells(m + i, n).Select
Selection.Copy
ActiveSheet.Cells(K + i, 37).Select
ActiveSheet.Paste
Next i
Next n
Next c
I am stuck at how to get this operation done for 32500 rows
Excel's Integer has a range of values from -32,768 to 32,767 so formatting 33,500 rows might be a problem. Assuming that you are happy with how your code works, changing your variable types to Long might be a good start.
BTW, you should avoid SELECTing cells as it slows down the code and can lead to errors. You can easily copy and paste between cells using soemthing like
Cells(m + i, n).Copy Destination:=Cells(K + i, 37)
Revised the code according to the comment and now working like a charm. Thank you so much
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim K As Integer
Dim p As Integer
For i = 0 To 121
m = 14
For n = 5 To 35
ActiveSheet.Range(Cells(m + i * 8, n), Cells(m + i * 8 + 7, n)).Copy
Range("AK" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues
Next n
Next i
End Sub

Extract any first two letters and 6 digit number from an excel and copy it to another cell

How do I extract only the first two letters and the 6 digit number from one cell to another? ie. Column 1 will have aa111111, bb222222, ccccc, dd12, eeee1
I only want to copy aa111111 and bb222222 in this case.
Thanks,
Alex
Try this short macro:
Sub KopyKat()
Dim N As Long, i As Long, K As Long
Dim s As String
N = Cells(Rows.Count, 1).End(xlUp).Row
K = 1
For i = 1 To N
s = Cells(i, 1).Value
If Len(s) = 8 _
And Mid(s, 1, 1) Like "[a-zA-Z]" _
And Mid(s, 2, 1) Like "[a-zA-Z]" _
And IsNumeric(Mid(s, 3)) Then
Cells(K, 2).Value = s
K = K + 1
End If
Next i
End Sub
If your strings are in A:A then in B1 (and copy down):
=IF(LEN(A1)>7,IF(AND(CODE(LOWER(MID(A1,{1,2},1)))<>CODE(UPPER(MID(A1,{1,2},1))),ISNUMBER(MID(A1,{3;4;5;6;7;8},1)*1)),LEFT(A1,8),""),"")
This is an array formula and must be confirmed with ctrl + shift + enter.
Running evaluate formula shows what happens and how it works :)
Using VBA then this will do:
Sub CopyMe()
Dim x As Variant, i As Long
For Each x In Range([A1], Cells(Rows.Count, 1).End(xlUp)).Value2
If x Like "[a-zA-Z][a-zA-Z]######" Then
i = i + 1
Cells(i, 2) = x
End If
Next
End Sub
Here is another way to do it. The formula loops through your string looking for a 6 digit number, at which point it takes the previous two characters (as long as they exist) and returns the 8 character string. Otherwise it returns an empty string.
=IFERROR(MID(A1,SUMPRODUCT(ROW($A$1:$A$100),--ISNUMBER(VALUE(MID(A1,ROW($A$1:$A$100),6))))-2,8),"")
Loops 100 times, so will work with strings up to a maximum length of 106 characters. To use the formula place your string in column A, and place this formula in cell B1 (for example) and drag down

Extract number from random text string without fixed format

I want to extract 10 digit mobile form random string in cell A1 which has text as well as mobile numbers some has one mob ile other has two or maybe three mobile numbers .All mobile number to be saved in different coloumns.Only excel sheet formula is required
First of all, you will need to create a named range. The purpose of the named range is to normalize and split the data by space so that it can be read by other formulas without having to type that out each and every time.
First, put your data in column A starting in row 1 (as shown in your sample data image). Then create a named range with a name of SplitString and define it with this formula:
=INDEX(TRIM(MID(SUBSTITUTE(TRIM(SUBSTITUTE($A1,"."," "))," ",REPT(" ",999)),999*(ROW($1:$10)-1)+1,999)),)
Note the ROW($1:$10). The 10 in that is a guess that the strings will never have more than 10 entries to evaluate in a single cell. This is consistent with your sample data where the cell with the most entries is ROHTAK (BUILDER) 7777777777 PAL 6666666666 which has 5 entries to be evaluated. If you need to increase the number, just increase the 10 to be a higher number.
Then in cell B1 and copied over and down, use this formula which utilizes the SplitString named range that has been defined:
=IFERROR(IF(AND(ISNUMBER(--$A1),LEN($A1)=10,COLUMN(A1)=1),--$A1,--INDEX(SplitString,MATCH(1,INDEX((COUNTIF($A1:A1,SplitString)=0)*(LEN(SplitString)=10)*(ISNUMBER(--SplitString)),),0))),"")
I would try using regular expressions as mentioned in this answer : https://stackoverflow.com/a/22542835/2068595
On the face of it, I would look for this regex [0-9]{10} (meaning 10 consecutives characters from 0 to 9) in your column.
With data in column A run this short macro:
Sub numbersss()
Dim N As Long, L As Long, K As Long
Dim i As Long, j As Long, t As String
N = Cells(Rows.Count, "A").End(xlUp).Row
For j = 1 To N
t = Cells(j, 1).Text
L = Len(t)
For i = 1 To L
If Mid(t, i, 1) Like "[0-9]" Then
Else
Mid(t, i, 1) = " "
End If
Next i
ary = Split(Application.WorksheetFunction.Trim(t), " ")
K = 2
For Each a In ary
If Len(a) = 10 Then
Cells(j, K) = "'" & a
K = K + 1
End If
Next a
Next j
End Sub
For example:
If someone posts a pure formula solution, please ignore this post.

Resources