Find number combination in every row in excel - vba

I have ten column and multiple rows in one Excel sheet with numbers in it. How could I let VBA find all the combination in every row and add the number for all rows.
For example, there is A2:100 and D2:7; C9:7 and F9:100 then it will output a format like 100+7:2

These steps should help get started. Unless you are talking about every possible combination of the number, which is a far more complex topic. You should try to write something and then edit your question to include your code.
Loop the columns For i = 1 to 10
Range(Cells(1, i),Cells(10,i)).Address(False,False) : e.g. A1:A10
WorkSheetFunction.Sum(Range(Cells(1, i),Cells(10,i)).Address(False,False)): 100

Related

How to merge or add rows based on IF condition while maintaining column row order

I need to merge several sheets and maintain data integrity. I have managed to reduce the problem to the following example and would really appreciate some help. The two columns have to be merged into one. The data in a column has to stay in the order it is originally in as each cell is a code that refers to time data. The duplicates need to be reduced to one entry as the whole data set will later be geo coded and duplicates are not tolerated.
The data is complicated and messy but essentially the problem i cannot solve can be artificially reduced to the following for simplicity:
[edit: Yellow bus stops at the following stop codes in the given order.
Blue bus stops at the following stop codes in the given order.
The order in which the buses stop cannot be changed.
The output is one list including all of the stop codes only once, retaining the order present in both lists.]
I want to change it to this:
The colour coding is just to make it clear to readers here.
The green represents where duplicates have been reduced to one (this will allow me to vlookup the data against the code and enter the data from both sheets in the same row.
The order that the codes are in must be maintained so i think i just have to move all the data down.
So, this is, (conceptually), what i was trying to do as a first stage.
Each row retains the order of data but is spread across enough rows to have enough space for both columns in one. The duplicates take the position of the one which has greater row number.
The rest of the solution follows procedurally. I can then delete duplicate codes, and create a new list with all of the values, then remove the spaces.
So if i can get to there i should be ok.
Therefore, as far as i can tell, i need to match with two formulas:
=MATCH(A1,$B$1:$B$11,0)
=MATCH(B1,$A$1:$A$11,0)
but i am not sure if this is actually the direction to take.
It gives me the row numbers on which the common entries occur, but I am not sure how recreate the necessary positions.
Perhaps i need to create these dummy columns and then count the necessary total rows, but how to do that between errors?
Or, have a dynamic running offset. But not sure how to do that either. I am finding it very difficult to wrap my head around it.
It has occurred to me that each value will be on the row number equal to the number of unique values in the two columns above it.
Many thanks in advance for any pointers / solution.
I have tried to reduce the problem down and make it as clear as possible. If anyone can see the solution and can also see where i need to focus my learning, advice on specific area of training would also be welcome.
Cheers
D
I think your problem is more easily addressed with VBA. Let's assume you are working in columns A and B (meaning, 1st and 2nd columns). In your example you have two columns and 11 rows, and since you want to handle your data row-by-row, row is the outer loop and column is the inner loop:
Sub Indexing()
Dim irow, icol, count As Integer
Dim lookIn, FoundRange As Range
count = 1
For irow = 1 To 11:
For icol = 1 To 2:
Cells(irow, icol).Select
Selection.Copy
Set lookIn = Range("F1:F" & count)
Set FoundRange = lookIn.Find(what:=Cells(irow, icol).Value, lookIn:=xlFormulas, lookat:=xlWhole)
If FoundRange Is Nothing Then
Range("F" & count).Select
ActiveSheet.Paste
count = count + 1
End If
Next icol
Next irow
End Sub
This yields the following result, output in column F:
Note that the order is slightly different from yours, but it sort of makes more sense. In your result example, you have 113 before 68, although the first number to appear in row #4 is 68. There are other differences so maybe I misunderstood the order you wanted.
You can adapt this code as follows:
If you have more columns, just replace the 2 in the icol loop by whatever number of columns you want.
If you have more rows, just replace the 11 in the irow loop by whatever number of rows you want.
If you want this to be written in a different column (not column F, as it is now), just replace F with the column letter that you want.
If you are not familiar with Macros and need help setting this up, let me know.

VBA : re-sort/re-order rows based on column values through specific range

I'm fairly new to VBA, and have found this to be a great resource. I understand enough to generally find someone else's code and modify it to what I need it to do, but I've been stumped here. I've tried coding this several times in different ways, but can't seem to get it to work, and am not overly convinced I'm approaching it the right way to start with. I'm hoping someone is willing to help me.
Here's a theoretical example of what I'm trying to do.
I have a sheet that contains groups of orders of a certain product type with their quantities listed by the step of the process they're currently at:
Current orders in process
I need to "sort" them or re-order them so that:
1. The orders are copied into a tab based on process step, with the furthest process step being listed first working back to the closest, and...
2. Orders listed in the "complete" step need to be excluded completely, and...
3. I need to do this for multiple product types, which have different numbers of process steps, so the end column needs to be identified somehow (as a dynamic variable, or some trigger)
For the example, it would look like this:
Sorted data
I've thought through a half-dozen ways to logically go about this and tried to figure out how to then do that with code, but I'm stuck ... and I have the feeling that there is probably 1 or 2 "best" ways to go about this that someone in this community probably knows off the top of their head. Thank you in advance.
Edit: Here's my first attempt at coding the outline. I realize there are some components missing, but would love feedback on whether this seems to be a usable, or efficient approach.
Sub test1()
Dim skuStartRow As Long, skuEndRow As Long
Dim qty As Integer
Dim colCheck as long, lastCol as Long
skuStartRow = .Range("A:A").Find(what:=sku, after:=.Range("A1")).Row
skuEndRow = .Range("A:A").Find(what:=sku, after:=.Range("A1"), searchdirection:=xlPrevious).Row
' Code here to identify last column (lastCol) either by .UsedRange or by header?
For colCheck = 3 To lastCol
For rowCheck = skuStartRow To skuEndRow
qty = Cells(rowCheck, colCheck).Value
If qty <> "" Then
'copy current row and paste into destination worksheet
MsgBox qty 'test to see if correct qty is displayed
End If
Next rowCheck
Next colCheck
End Sub
Update:
Still having problems.
I've copied/pasted the whole range of orders based on product type to a new sheet where it will be sorted.
I've inserted a column and used the match/index formula provided by Variatus (autofill down) to create an index column.
Insert new column and use RANK function to create a rank ordered list, as there are duplicates in the Helper/Index column.
Used INDEX/MATCH functions to return values into rank ordered lists for both work order number and quantity value.
The code I currently have works - except that it's hard-coded using FormulaRICI = ... commands. I'm having major challenges figuring out how to convert the code to allow use of MATCH/INDEX functions with variables. I need to do this b/c depending on product type there are varying amounts of total orders, as well as varying amounts of number of steps; so I need to have a dynamic range for both rows/columns.
I've looked at a bunch of examples but can't seem to find something that works correctly. The most promising I've seen appears to be using the "replace" function in VBA, but my head's about to explode.
Any suggestions are much appreciated.
I wonder if you really need VBA for this. Please consider these steps.
Insert a column in your worksheet with this formula in row 2, copied down.
=IFERROR(MATCH(TRUE,INDEX($D2:$I2<>0,0),0),0). The range D2:I2 is defined by D2 = Step 1 and I2 = "Complete". The formula finds the first non-blank cell in the range. The column could be hidden.
Sort the sheet on the helper column, largest to smallest.
Apply a filter.
Filter on the product.
What you see at this point is the list you wish to create using VBA, however including the 'Complete' column and any other columns to the right of it.
There are a number of ways of automating this process. You might write code to either insert the formula in the helper column automatically or its result. This could be done when needed, without having a permanent extra column. You could write code to do the sorting and filtering automatically, for example, based on a currently selected product. And, finally, you might write code to copy the result to another sheet, remove the unwanted columns there, and restore the original sheet to its original condition (presumably sorted by order number).

Make a difference Amount from 2 sheet using Macro

I have a problem to be solved. I have data from sheet 2016 and 2017. What I want to do is to make another sheet to calculate the difference money within 2016 and 2017. Please see the image link attached:
Case 1:
Case 2:
What I want to do is simply create the new sheet to compare each column except column A (column name).
and another problem is the number of columns is not fixed. some case may have 1 column to be compared, another might have more. So I guess I need the number of columns to become a variable?
How is the idea to make the total (comparison) sheet using macro that can solve case 1 and case 2? please share your thought!
=INDEX ( Column I want a return value from ; MATCH ( My Lookup Value ; Column I want to Lookup against;0))
Use this formula to get the values in each column, then just subtract the results.

Excel formula not working as expected

I have a sheet that shows max values spent anywhere. So I need to find most expensive place and return it's name. Like this:
Whole sheet.
Function.
Function in text:
=IFS((A6=MAX(D2:D31)),(INDEX(C2:C31,MATCH(A6,D2:D31,0))),(A6=MAX(H2:H31)),(INDEX(G2:G31,MATCH(A6,H2:H31,0))),(A6=MAX(K2:K31)),(INDEX(K2:K31,MATCH(A6,L2:L31,0))))
Basically I need to find a word left to value, matching A6 cell.
Thanks in advance.
Ok.. Overcomplicated!
Firstly, why the three rows? it's a lot easier if you just have one long row with all the data (tell me if you actually need 3 I'll change my solution)
=LOOKUP(MAX(D2:D31);D2:D31;C2:C31)
The MAX formula will lookup the biggest value in the list, the Lookup formula will then match it to the name.
Please note: If more than one object has the maximum price, it will only return the first one. The only way I can think of to bypass that would be to build a macro.
EDIT:
Alright.. Multi Column solution is ugly and requires extra columns that you can just hide.
As you can see you'll need 2 new columns that will find the highest for each row, 2 new columns that will find the value for each of these "highest" (in this case tree and blueberries) and then your visible answer will simply be an if statement finding out which one is bigger and giving the final verdict. This can be expanded with an infinite number of columns but increases complexity.
Here are the formulas:
MAX(H2:H31)
LOOKUP(A5;H2:H31;G2:G31)
MAX(L2:L31)
LOOKUP(C5;L2:L6;K2:K6)
IF(A5>C5;B5;D5)

Deleting data in last cell of data in Column A

I am in need of assistance agian. When my macro works, the number 1 is populated in the final cell in column A because the coding is set up to place a number 1 or 2 depending on other data until the last row of data. Since the last row of data has necessary information in other columns, I wanted to delete the 1 in column A because it is not needed. How do I code to take away the value in the final cell of a column? I've tried referencing to it and if statements. I am sure I am missing something simple.
This is my first macro using VBA.
Thanks.
I just started working with VBA in the last day or two, but I believe the thing you are looking for is: Cells(x,y).End(xldown).ClearContents
Alternatively, I think Range("A1").End(xldown).ClearContents would be equally effective.
But I just started doing this myself, so take with spoonful of salt.