Excel macro for loop? - vba

I am writing a macro in excel and I have a 10 by 10 grid that I need to input a vlookup equation into. So for the first column of data I wrote a for loop
For i = 2 to 11
Cells (i, 30) = "=vlookup (E2" & i & ", B:C, 2, False)"
Next i
And it works the way I want it. Now I would like to write an outer for loop that will allow me to go across the columns.
I started with
For j = 30 to 39
For i = 2 to 11
Cells (i, 30) = "=vlookup (E" & i & ", B:C, 2, False)"
Next i
Next j
But I need E to change to F and I can't figure out how to do that.
I also tried using the Range (Cells(__,__),Cells(__,__)).FormulaR1C1 =...
But I couldn't get that to work either, since it is moving the table as well as fixing E2 across the columns.
If anyone can help I would greatly appreciate it. If not I will just write 10 for loops one for each column.

I think all you need to do is create the first formula and add "$" to appropriately anchor the reference rows/columns at which point you can simply copy the formula across the desired rows/columns.
here is a quick synopsis of anchoring: http://excelonthegrid.wordpress.com/2012/09/26/anchor-with-dollars/
i can give you the final answers if you post the desired formula for
a) column 1 row 1
b) column 1 row 2
c) column 2 row 1
d) column 2 row 2

Related

how to insert array list into table?

I was wondering if there is a faster way to insert a list of values into a table object. The method i'm using goes like this:
1) Clear the old data from table
2) find add in new date values to the table and letting the table formulas auto-populate.
The problem right now is that this process is incredibly slow as i have about 7 tables and 4k+ dates to populate.
Ordinarily if it's just pasting the values, excel works incredibly fast. But when it comes to table objects, it gets a lot slower for some reason.
I think there's a way to do this a lot faster by manipulating the table object but i haven't had any luck so far.
*table1
1 2 3 4 5 6
A Date 1D 2D 3D 4D 5D
B 1/1/2016 Formula Formula Formula Formula Formula
C 2/1/2016 Formula Formula Formula Formula Formula
D 3/1/2016 Formula Formula Formula Formula Formula
E 4/1/2016 Formula Formula Formula Formula Formula
F 5/1/2016 Formula Formula Formula Formula Formula
G 6/1/2016 Formula Formula Formula Formula Formula
H 7/1/2016 Formula Formula Formula Formula Formula
Dim ws(), datelist() As Variant
For i = 1 To UBound(ws)
For j = 1 To UBound(datelist)
Sheets(CStr(ws(i))).Cells(j + 1, 1) = datelist(j)
Next j
Next i
*ws() refers to array where i store worksheet names
*datelist() refers to the dates i want to paste into the cells.
not sure if my description of the problem is clear enough.
Doing a quick search brought up this answer.
To apply it to your case, instead of: -
For i = 1 To UBound(ws)
For j = 1 To UBound(datelist)
Sheets(CStr(ws(i))).Cells(j + 1, 1) = datelist(j)
Next j
Next i
Use: -
For i = 1 To UBound(ws)
Sheets(CStr(ws(i))).Range("A" & j + 1 & ":A" & j + (1 + UBound(datelist))) = WorksheetFunction.Transpose(datelist)
Next i

how do I sum values from a row and the next row, and place them in a column

So in Column C I have values called inflow, starting in Row 3
In Column D, I want to sum the values from the current row and previous row of Column C, (for example. row 4, column D = row 3, column C + row 4, column C) until the end of the sheet
screenshot
Do you want to do this in VBA?
It is easily obtainable through a dragable formula.
In D3 you would obviously have C3.
In D4 you would have =SUM($C$3:C4)
Hope it helps. :)
Edit: I might have misinterpreted your question. If it turns out you just want to sum last two values and not all values from the row and up to row three you should just leave out the $ so it would be: D4 =SUM(C3:C4)
Dragging it down would give you D5 =SUM(C4:C5) and so on.
Edit2:
Okay, I'm not exactly sure what you want, but if you want the formula to be expanded using VBA this should do the trick. Please try it out and let me know if it works for you.
Sub autosum()
Dim r As Integer, rmax As Integer
rmax = Range("C3").End(xlDown).Row
For r = 4 To rmax
Range("D" & r).FormulaR1C1 = "=SUM(R" & r - 1 & "C3:R" & r & "C3)"
Next r
End Sub

VBA(EXCEL) extract information from different rows with various criteria

I have an excel file which contain information of the composite in 1 row and in below rows information of the components of this composite. Number of component rows below a composite are varied between 2 - 20 and there can be many composites in a file.
My question is: is it possible somehow to define how many rows are in the components and to extract information from each component in to one cell(concatenate). Problem I face is that number of rows are different each time and there can be multiple composites in the file containing components. So i do not know how to stop my loop and start a new composite aggregation.
Maybe there are ways to loop from Request1(ColumnA) and assign "Request1" as a text to every empty column below until it reaches Request2, after that is finished to concatenate based on Request"n"
Example what i want the data to look like
~~~~~~~~~~~~EDIT~~~~~~~~~~~~~~~~~~~~
I might have over complicated my question
I was just looking to concatenate information from different set of rows(for simplicity just 1 consistent cell from every row) in to 1 cell(for example last cell in the first column) for each specific composite(which contains components) My problem is I do not know how to stop the concatenation and start a new one when i am working with a new composite(new set of rows).
So as an example from the first picture, I would like to have "Request 1 Green Yellow White" (cells: A1, F1, F2,F3) populated in cell J1, and "Request 2 Amber Red White Blue" (cells: A4,F4,F5,F6,F7)populated i cell J4
#######EDIT
I have established another way of doing but still struggle with concatenation formula.
In this picture example
https://i.stack.imgur.com/iQdNu.jpg
If my table stars from row 2
=IF(A2="",J1,A2) - by putting this in column J and dragging down i will get his Request 1
Request 1
Request 1
Request 2
Request 2
Request 2
Request 2
Then deleting duplicates i will be left only with
Request 1
Request 2
Then I can concatenate columns i want going by Request 1 or request 2 criteria(index match), but I cant figure out how to do it...
You can use array formula to work out the start and end rows, like =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) to find the next populated cell in A1:A20, where this would be in the cell G1. So in G1, I have a fixed 1, then in G2 down, I have =H1+1, then in each H filled down I have =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) this gives the following
Unfortunately we cant do the concat using what we have in Excel, so this will help with your loop start and ends. Number of products, is the difference in the 2
If I am reading your question correctly, then the following code may help. You want to be able to add element rows beneath categories rows, and when you do that it changes the row number for every row beneath the new row. This code will show you that it doesn't matter which row the category is on because you can find it's row number any time, and also the number of elements beneath it.
The trick is to add a word in col A of each category that will not be found in any element A value. For example, A1 might read "Category: Apples"," and there may be ten element rows under "Category: Apples" And then under those rows another category in col A will be "Category: Bananas." The code below looks for the value "Category:" in col A and gets the row number of each category line and how many elements are under it. With a little math you can figure out where to insert a new line for a new element row or what row to concatenate. And you won't need to hard code the rows numbers of the category. Just run this simple code and it will give you those row numbers to get and concatenate all rows beneath any category.
Sub findCategoryRows()
Dim lastRowColA As Long, myArray() As Variant
Dim rowOfCategoryNameArray, nameOfCategoryNameArray, categoryCounter As Long
lastRowColA = ActiveSheet.Range("A65536").End(xlUp).Row
myArray = Range("A1:A" & lastRowColA)
categoryCounter = 1
ReDim rowOfCategoryNameArray(1 To 1)
ReDim nameOfCategoryNameArray(1 To 1)
For i = 1 To UBound(myArray)
If InStr(1, Range("A" & i).Value, "Category: ") Then
rowOfCategoryNameArray(categoryCounter) = i
nameOfCategoryNameArray(categoryCounter) = Range("A" & i).Value
categoryCounter = categoryCounter + 1
ReDim Preserve rowOfCategoryNameArray(1 To categoryCounter)
ReDim Preserve nameOfCategoryNameArray(1 To categoryCounter)
End If
Next i
For i = 1 To UBound(rowOfCategoryNameArray) - 1
If i <> UBound(rowOfCategoryNameArray) - 1 Then
Debug.Print nameOfCategoryNameArray(i) & " has " & (rowOfCategoryNameArray(i + 1) - rowOfCategoryNameArray(i)) - 1 & " element rows under it."
Else
Debug.Print nameOfCategoryNameArray(i) & " has " & (lastRowColA - rowOfCategoryNameArray(i)) & " element rows under it."
End If
Next i
End Sub

Insert new rows and populate

I need help creating a excel macro to split orders based on qty column.
In the before sheet we have 4 orders with two of them having a qty greater the 1. see before macro image
What I need is an excel macro that can check the qty field and insert new rows below based on qty value if greater then 1, then populate these rows with the data from the original row.
Then the last step would be to delete the C column. See After Macro image
I have never used macros before, so i'm not even sure if this can be done.
It's only 10 lines of code. SO is not a code sweatshop where we do the work for you, it is one where we help you work it out for yourself.
This will poll the rows for you going backwards:
For X = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
This is key because you will be inserting rows, going forwards creates all sort of nastiness.
You will then need a loop inside there using your qty as a reference. Here is one I built but you will need to make some changes for it to work for your columns:
For Y = 1 To Range("B" & X).Value - 1
Then you need to copy row X inside this loop
Next line will be an insert with a shift down (like this Insert Shift:=xlDown) on row X + 1 or on an offset of row X by 1 row, your choice how you do this.
Last thing to do is delete column C, This can be done like this Range("C1").EntireColumn.Delete
That is it, the whole macro, just drop a couple of nexts and an end sub and run it.
You will need to Dim X and Y, dim them as longs.
Have a go, if you get stuck post back with the code you have put together and we can help you fix it.

VBA macro to select cell based on row and column criteria

I'm trying to create a macro that runs with a custom user form shown below. My ultimate goal is to have the Expenditure Category radial select the sheet in the file, then have the PO Number field search column A of the selected sheet to find a match, then subsequently have the Week Ending Date field search row 7 to find a match for the date entered. Where these two intersect (i.e. CN72) is where I want to input what is put in Accrual Amount. I have no idea about how to go about doing this, so any help would be great!
It is a loop inside another loop...I might use something like this..
x = 1
z = 1
datexx = [input]
POnumber = [input]
do
do
if Sheets("[name]").Range("[column]" & z) = Sheets("[name]").Range("[column]" & x) then
'do your stuff here
end if
z = z +1
loop until isempty(Sheets("[name]").Range("[A column that is never empty for all data]" & z)
x = x+ 1
loop until isempty(Sheets("[name]").Range("[A column that is never empty for all data]" & x)