I am trying to do the following with knowing that column A and B are data and C is the result:
A B C
1 5 (B1-A1)=4
2 3 (B2-A1)=2
3 5 (B3-A1)=4
4 7 (B4-A2)=5
5 4 (B5-A2)=3
6 9 (B6-A2)=7
.
.
.
.
How do I do this automatically in Excel or in Excel Visual Basic?
Sub sequence()
Dim i As Integer
Dim j As Integer
i = 2
j = 2
For i = 2 To 25 Step 3
Cells(i, 3) = Cells(i, 2) - Cells(j, 1)
Cells(i + 1, 3) = Cells(i + 1, 2) - Cells(j, 1)
Cells(i + 2, 3) = Cells(i + 2, 2) - Cells(j, 1)
j = j + 1
Next i
End Sub
Here is the VBA code that solves.
You must define the range in for loop, currently it is set from 2nd Row to 25th Row.
A B C
1 4 =B2-A2
1 2 =B3-A3
1 3 =B4-A4
=A2+1 5 =B5-A5
=A3+1 6 =B6-A6
=A4+1 7 =B7-A7
=A5+1 6 =B8-A8
=A6+1 7 =B9-A9
=A7+1 9 =B10-A10
You can initiate your first 3 rows with 1 and then just add 1 in the 4th row column A; drag the formula down. Subsequently, you may then subtract Column B from Column A.
The only drawback is that your column A will not be a sequence incrementing by 1 every step instead a sequence stepping by 1 on every fourth occasion.
OFFSET with ROW() is your friend for any repeating n-th row/column problem.
=B1-OFFSET(A$1,ROUNDUP(ROW()/3,0)-1,0), copied down column C.
1 5 4
2 3 2
3 5 4
4 7 5
5 4 2
6 9 7
You can use the $ in the function ($B5-$A1) and drag the cursor with the cell over the C column to the last element written.
Related
I have 5 items that can be placed in any unique order, I want to store the values (numbers) of a single unique order to a variable, one by one. For example:
User input: 7
Then i_Int = 7
should give me
v_Var = 1
wait 1 sec
v_Var = 3
wait 1 sec
v_Var = 2
wait 1 sec
v_Var = 4
wait 1 sec
v_Var = 5
The data below list all possible permutations of 5 items, where the first row lists the permutation #, I will not have this data to make things easy.
1 1 2 3 4 5
2 1 2 3 5 4
3 1 2 4 3 5
4 1 2 4 5 3
5 1 2 5 3 4
6 1 2 5 4 3
7 1 3 2 4 5
8 1 3 2 5 4
9 1 3 4 2 5
10 1 3 4 5 2
...
111 5 3 2 1 4
112 5 3 2 4 1
113 5 3 4 1 2
114 5 3 4 2 1
115 5 4 1 2 3
116 5 4 1 3 2
117 5 4 2 1 3
118 5 4 2 3 1
119 5 4 3 1 2
120 5 4 3 2 1
Here is a function that returns the permutation of 1,...,n of rank i:
Function Unrank(ByVal n As Long, ByVal rank As Long, Optional lb As Long = 1) As Variant
Dim Permutation As Variant
Dim Items As Variant
ReDim Permutation(lb To lb + n - 1)
ReDim Items(0 To n - 1)
Dim i As Long, j As Long, k As Long, q As Long
Dim fact As Long
For i = 0 To n - 1
Items(i) = i + 1
Next i
rank = rank - 1
j = lb
For i = n - 1 To 1 Step -1
fact = Application.WorksheetFunction.fact(i)
q = Int(rank / fact)
Permutation(j) = Items(q)
'slide items above q 1 unit to left
For k = q + 1 To i
Items(k - 1) = Items(k)
Next k
j = j + 1
rank = rank Mod fact
Next i
'place last item:
Permutation(lb + n - 1) = Items(0)
Unrank = Permutation
End Function
As a default, it returns the result as a 1-based array. To make it 0-based, use a call like Unrank(5,7,0). As a test:
Sub test()
'fills A1:A120 with the permutations of 1,2,3,4,5
Dim i As Long
For i = 1 To 120
Cells(i, 1).Value = Join(Unrank(5, i), " ")
Next i
End Sub
13! is too large to hold in a Long variable, so the code throws an untrapped error when n=14. The algorithm that I use depends on the ability to do modular arithmetic with the relevant factorials, so there is no easy fix in VBA. Note that you could easily tweak the code so that you pass it an array of items to permute rather than always permuting 1-n. The algorithm destroys the array Items, so such a tweak would involve creating a 0-based (so that the modular arithmetic works out) copy of the passed array.
I haven't seen any similar question to this one. Thank you in advance for your help!
I have these two columns:
Final Product - Subcomponent
A - 1
B - 1
C - 1
D - 1
A - 2
C - 2
B - 3
C - 3
A - 4
C - 4
D - 4
A - 5
B - 5
Final product A is made with the subcomponents 1, 2,4 and 5.
B is made with the subcomponents 1,3 and 5.
C is made with the subcomponents 1,2 and 4.
D is made with the subcomponents 1 and 4.
What I am looking for is an algorithm in vba or pivot tables that optimizes the final production in this way:
1 repeats 4 times.
2 repeats 2 times.
3 repeats 2 times.
4 repeats 3 times.
5 repeats 2 times.
First A should be made because it has more common components. Then B should be made because there is just 1 component missing compared with A. Then C because there is just one component to be replaced and last D because there is has the same two components as C.
I know this is not easy at all... Thank you!
Try this code
Sub Test()
Dim d As Object
Dim v As Variant
Dim m As Long
Dim r As Long
Dim i As Long
m = Range("A" & Rows.Count).End(xlUp).Row
v = Range("A1:B" & m).Value
Set d = CreateObject("Scripting.Dictionary")
For r = 1 To m
If d.Exists(v(r, 1)) Then
d(v(r, 1)) = d(v(r, 1)) & ", " & v(r, 2)
Else
d(v(r, 1)) = v(r, 2)
End If
Next r
Range("E1").Resize(d.Count).Value = Application.Transpose(d.Keys)
Range("F1").Resize(d.Count).Value = Application.Transpose(d.Items)
End Sub
I have to manipulate range of cells in excel using VB. Can I do it in the following manner ?
Range("a1:b5")=[1 2 3 4 5 6 7 8 9 0]
I don't think so, I've never seen that syntax. One thing you can do is something like:
For Row = 1 To 5
Range("a" + CStr(Row)).Value = Row
Range("b" + CStr(Row)).Value = (Row + 5) Mod 10
Next Row
assuming that you want it set up thus:
A B
+------
1 | 1 6
2 | 2 7
3 | 3 8
4 | 4 9
5 | 5 0
You may need to use Mid(CStr(Row),2) - I can't remember off the top of my head if Cstr gives you a leading space for non-negative numbers.
I apologise if this question already exists, I searched for a while but couldn't find anything.
I have 2 columns in Excel and I need to concatenate the values of 1 column where the values of another are the same. As an example I have this:
A | B
12 | Value 1
10 | Value 2
13 | Value 3
12 | Value 4
10 | Value 5
And I would like to get out:
A | B
12 | Value 1, Value 4
10 | Value 2, Value 5
13 | Value 3
I have thousands of rows and ideally I would like it to create a new worksheet with the results and not destroy the existing sheet. There are also some blank values in column B which I would like it to ignore and not concatenate.
Thanks in advance.
Try this:
Sub combineValues()
Dim dic As Dictionary
Dim key, val, i, p, k
Set dic = New Dictionary
For i = 1 To Worksheets(1).Range("A65536").End(xlUp).Row
key = Worksheets(1).Cells(i, 1).Value
val = Worksheets(1).Cells(i, 2).Value
If dic.Exists(key) Then
dic.Item(key) = dic.Item(key) & ", " & val
Else
dic.Add key, val
End If
Next
p = 1
For Each k In dic.Keys
Worksheets(2).Cells(p, 1) = k
Worksheets(2).Cells(p, 2) = dic.Item(k)
p = p + 1
Next
End Sub
Note that you'll have to include "Microsoft Scripting Runtime" in "Reference" in order to use Dictionary.
Tested with following [Sheet 1]:
1 value 1
2 value 2
3 value 3
1 value 4
1 value 5
3 value 6
3 value 7
2 value 8
1 value 9
2 value 10
2 value 11
2 value 12
Results in following [Sheet 2]:
1 value 1, value 4, value 5, value 9
2 value 2, value 8, value 10, value 11, value 12
3 value 3, value 6, value 7
I have found that Passerby's answer works beautifully, but in my case, I have several columns that I run this on and in some cases there are empty cells. To avoid getting , , , entries, I added this:
If val <> "" Then
before
dic.Item(key) = dic.Item(key) & ", " & val
I have been trying to write a macro to re-arrange the Cells in the rows and columns of Stock tables for the output I desire. Luckily the Stock Tables are generally the same each and every time (Different names and values), and the desired outcome is the same format..
Here is some example Data.
A
1 Name
2 description
3 description
4 description
5 description
6 ID#: 56284
7 Quantity in stock: 34
8 Zoom In and Configure
B
1 Name
2 description
3 description
4 description
5 description
6 ID#: 56284
7 Quantity in stock: 50
8 Zoom In and Configure
And I would like the Output to go into something like this(If possible to sheet2 starting on Cell B2):
B C E
B Being Row 1
C being Row 2 3 4 and 5 Combined
E being JUST Row 7 Stock Value I.E 50
On a single spreadsheet there would be 4 columns, and 8 rows I would have to re-arrange.. Making 32 total.
It would be great to automated this, so any help would be greatly appreciated.
Let me clarify my understanding. For each column you want the following data format:
A A
1 Name 1 Name
2 Desc1 2 Desc1; Desc2; Desc3; Desc4
3 Desc2 On sheet 2 3 50
4 Desc3 --------------->
5 Desc4
6 Id#: 56284
7 Quantity in Stock: 50
8 Zoom in and configure
If this is the case you can use the following code. It assumes your data is in A1 to D8 in Sheet 1.
Sub FormatData()
Dim col As Integer
For col = 1 To 4
With Worksheets(2)
.Cells(1, col) = Cells(1, col) //Get name
.Cells(2, col) = Cells(2, col) & "; " & Cells(3, col) & "; " & Cells(4, col) & "; " & Cells(5, col) //Concatenate descriptions into single string
.Cells(3, col) = Cells(7, col) //Get quantity in stock
End With
Next col
End Sub