Input user defined values inside multiple cells using vba - vba

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.

Related

Calculate new column using relative row references

I would like to turn a data frame like this:
DF
Nrow
a
1
5
2
6
3
7
4
11
5
16
Into this:
DF
Nrow
a
b
1
5
NA
2
6
NA
3
7
2
4
11
5
5
16
9
Column 'b' is calculated as the value from column 'a' minus another value from column 'a', in [row-2]. For example b4 = a4-a2.
I have had no success so far with indexing or loops. Is there a tool or command for this or some obvious notation that I am missing? I need to do this continuously without splitting into groups.

Create new ID based on cumulative sum in excel vba

I need to create a new transport ID based on the cumulative sum of the volume being transported. Let´s say that originally everything was transported in truck A with a capacity of 25. Now I want to assign these items to shipments with truck B (Capacity 15).
The only real constraint is amt shipped cannot exceed capacity.
I can´t post a picture because of the restrictions...but the overall set up would be like this:
Old Trans # Volume New Trans # Cumulative Volume for Trans
1 1
1 9
1 3
1 7
1 4
2 9
2 10
3 8
3 5
3 9
4 4
4 6
4 8
5 9
5 1
5 5
5 8
6 3
6 4
6 3
6 4
6 4
6 7
7 7
7 10
7 4
8 10
8 6
8 7
9 4
9 9
9 6
10 7
10 4
10 1
10 1
10 5
10 2
11 9
11 3
11 9
12 8
12 5
12 9
13 9
Expected output would be that the first three entries would result in a new shipment ID of 1;the next two entries would result in a new shipment ID of 2;and so on... I´ve tried everthing that I know(excluding VBA): Index/lookup/if functions. My VBA skills are very limited though.Any tips?? thanks!
I think I see what you're trying to do here, and just using an IF formula (and inserting a new column to keep track):
In the Columns C and D, insert these formulas in row 3 and copy down (changing 15 for whatever you want your new volume capacity to be):
Column C: =IF(B3+C2<15,B3+C2,B3)
Column D: =IF(B3+C2<15,D2,D2+1)
And for the cells C2 and D2:
C2: = B2
D2: = A2
Is this what you're looking to do?
A simple formula could be written that 'floats' the range totals for each successive load ID.
In the following, I've typed 25 and 15 in D1:E1 and used a custom number format of I\D 0. In this way, the column is identified and the cell can be referenced as a true number load limit. You can hard-code the limits into the formula if you prefer by overwriting D$1 but you will not have a one-size-fits-all formula that can be copied right for alternate load limits as I have in my example..
      
The formula in D2 is,
=IF(ROW()=2, 1, (SUM(INDEX($B:$B, MATCH(D1, D1:D$1, 0)):$B2)>D$1)+ D1)
Fill right to E2 then down as necessary.

how do I conditionally subtract in Excel?

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.

Need to delete partial rows in excel using vba

I have an excel sheet, in which i need to delete partial data from rows for some columns for further processing using vba. Any suggestions would be great.
Thanks,
Raw data:
**A** **B** **C** **D**
1 2 3 4
1 2 5 6
1 2 7 8
2 3 8 9
2 3 5 7
Expected data
**A** **B** **C** **D**
1 2 3 4
- - 5 6
- - 7 8
2 3 8 9
- - 5 7
If the idea is purely to delete repeating data you could evaluate each cell, look at what row your in, look at the range below that cell, find first and clear it, repeat until your find first returns nothing.

Extract a grid from a one-dimentional array

I have an array with 12 values and I need to display these in a grid like this (always 4 columns, 3 rows):
1 | 2 | 3 | 4
----------------------
5 | 6 | 7 | 8
----------------------
9 | 10 | 11 | 12
I am looping through the grid and I have two coordinates: column and row.
How do I know which index belongs to which row and column? I tried several things, but they are not working:
objectAtIndex: (row + 1) * (column + 1) - 1
objectAtIndex: row + column
etc...
Row and column indexes start with 0.
forward conversion: objectAtPosition(x,y) = array[columns*y + x]
provided x<columns && y<rows
backward conversion: positionAtIndex(i) = (row=(i div columns), col=(i mod columns))
note that div and mod correspond to integer operators / and % in C languages.