Select excel data by condition? - excel-formula

I have a excel table, that I want to select column A data with a condition.But my formula not working.
#pseudo code
column_A_idx
if column_A_idx % 5 == 1:
get_B_column_data, put data At column G
So I write a excel formula =IF(MOD(A1:A36, 5) = 1,B1:B36,0), That I want get column A data that meet colun_A%5 == 1 condtion, and then get column "B" data put it some-where.
The final result like this

If you have Excel 365 you can use this function:
=FILTER(A1:B36,MOD(A1:A36,5)=1,0)
It filters the range based on the mod of column 'A's value.
The result is the filtered range, i.e. your expected result.

Related

How do I write VBA script so that it will compare data?

We need a script to compare the dates in column D to the dates in column E.
If the date in column D is two days before the date in column E then we need column F to state that in the corresponding row to D. We have a range of 2 days before and 2 days after (shoulder days). We need to be able to easily reproduce this and have it be able to run when we import it into access. we have roughly 3300 unique days to check.
I have tried using datevalue or a formula but we need to be able to reproduce this in the future and the formula doesn't allow us to easily do that.
I will not provide a complete implementation, but I can show you the way how you can do it (either the Excel formula way or with VBA code). Both ways will automatically calculate the values you need so you can easily import it into Microsoft Access - the import will take the calculated values from the cells you've selected.
The first way, which is also the simplest way, is to do that with simple Excel formula, consider this picture showing how the formula will calculate the values in columns F and G:
(Note: It is showing German date format, but you can easily change that in Excel).
In column G it calculates the difference D2 - D1 and displays it in days.
Then, in column F there is a simple if condition to determine if the date D1 is less, greater or equal than D2. The Result is calculated automatically.
The formula in row 2 are:
Hint: You can drag and drop the formula to the rows 3 - n below, so you can create a lot of rows prefilled with that formula.
A second way is that you can write a VBA function for the (simple) calculation formula in column F:
Function CalcResult(D1 As Variant, D2 As Variant) As Variant
CalcResult = ""
Dim result As String
Dim diff As Single
diff = D2 - D1
If (diff > 0) Then
result = "D1 < D2"
ElseIf (diff = 0) Then
result = "D1 = D2"
ElseIf (diff < 0) Then
result = "D1 > D2"
End If
CalcResult = result
End Function
This needs to be entered in the VBA editor, which you can display if you press Alt+F11. After you have done that, press Alt+F11 again to close VBA and return to your Excel sheet.
Then, place the formula =CalcResult(E2; D2) in cell F2 as shown below:
Like in the previous example, you can drag & drop the formula to the rows 3 - n below, so you can create a lot of rows prefilled with that formula.
The advantage of the 2nd approach is that you can refine the function CalcResult later without having to change the cells again (as it is the case in the first example).
Note: The function above needs to be in a separate module and you need to save the workbook as "Macro enabled workbook" later - otherwise you'll lose the VBA code.
A third way is to use the Excel macro recorder and record whatever you intend to do. It will create a public module with VBA code. If the recorder asks you, choose to store the code in the workbook.
Later you re-visit the generated VBA code and refine it - for example, put a for loop around it to automate things you've recorded once.
This approach is good for creating a "Calculate" button and put some logic behind it.

Increment column reference in formula

I have data which I have to enter into a table that has the following layout:
I need to store it in another worksheet that has this layout:
I am trying to create a macro that will accomplish 3 things:
Copy the contents in the first table to a column in another worksheet
clear the original table data (but not the headers q, w, e, etc)
Increment the column reference so that the next time the macro is ran, it will copy the data into column C in the second table, and the third time into column D, and so on.
This is what I have tried:
Line 1: Sheets("sheet2").Range("B1").Value = Sheets("sheet1").Range("A5").Value
Line 2: Sheets("sheet1").Range("A5:D6").ClearContents
In order to accomplish 3), I have to manually change "A5" to "B5" in the code, and to do so for each cell (of which there are about 60). Is there a way to automate this?
You can use excel's offset() function.
To change A5 to B5 you can do
For x = 1 to N
Sheets("sheet2").Range("B1").Value = Sheets("sheet1").Range("A5").offset(0,x).Value
If you want to do it only once then you will not need the for loop.

Excel - Pivot Table - Formula Sum a cell only if other cell fulfill an specific value

I trying to create a Pivot Table on Excel with certain formulas. One of them should sum(montant emission) only if off/online = 'OFFLINE'
For example if I group by Code agent, I only want to sum(Montant emission) if 'Off/Online' = 'OFFLINE'
In this case the sum for Code agent = CHO should be 0 and for Code agent = JGR should be 137.5
I need to create a formula that do this calculation. Which formula should I use on "Calculated Field"
Use
=SUMIF(I:I,"OFFLINE",E:E)
assuming that I is your Off/Online column, E is the column with the value you want to sum up.
You can also make "OFFLINE" dynamic and refer to another cell.

Copy Data To Different Column if Row is Filtered: Spotfire, R, Data Function

I'm looking to transfer a data column from one column to another after I click a button. I do not want to transfer the whole column, just rows where the name column is selected in a filter.
My train of thought was:
1) get a list of strings that are filtered
2) then loop through the rows of the table
3) check on that row if the name column is in the list
4) If so, then copy the data, if not leave the value in the copy
I've accomplished the 1) by creating a document property of the filtered values separated by commas. So "A, C" if A and C are filtered.
Table before data function call:
Table after data function call when A & C are filtered.
Have you tried a calculated column?
If ([Name] in ("A","C"),[Value],[Copy])
Add a multi line ctrl doc prop to replace "A","C" in the expression with ${aDocProp} that holds your Name filtered values on a text area.

Excel VBA changing my formulas in a table?

Has anyone come across a situation where Excel seems to manipulate your formulas.
I have a sheet where I have an Index value in Column A. The First row starts with any non zero Value. Subsequent rows in the column increment the value. Eg
A1 = 1000
A2= A1+ 1
A3= A2 + 1
and so on/
I have another column B whose values will either be blank or a formula pointing to column A(usually the subsequent rows)
Eg:
B1.Formula = "=A2"
B2.Formula = "=A3"
B3.Value = ""
B4.value = "=A6"
Now I have a backup-restore functionality that lets me write out the data/formulas to a text file and then read it back in another workbook.
In the case of columns A and B, I am checking if the text value starts with "=" and then set either the value or formula of that cell depending on whether there is a formula or not.
So far the functionality has worked fine. It lets me restore accurately.
Now, if I convert this data range to a table and modify the code accordingly the behaviour is strange. I am using the ListObject structure to refer to the table. So for Column B my restore code is:
If Left(soureString) = "=" Then
'This is a formula
Sheets("MySheet").ListObjects(1).ListColumns("Next").DataBodyRange(row).Formula = sourcestring
Else
'This is a value
Sheets("MySheet").ListObjects(1).ListColumns("Next").DataBodyRange(row).Value = soureString
End If
once I am done writing a row, I loop to the start and
Dim newRow AS listrow
Set newRow = Sheets("MySheet").Listrows.Add(AlwaysInsert:=False)
row = newRow.Index
But this time when I run the process. this is what I get:
B1.Formula = "=A5"
B2.Formula = "=A5"
B3.Value = ""
B4.value = "=A5"
Why are my formula values all changing to the same value when I use a table instead of a range?
I had the same issue when populating a ListObject (Table) from an Excel Add-in, setting AutoFillFormulasInLists was the solution.
My workaround is to save the current setting, set AutoFillFormulasInLists to false, populate the table with data, formulas etc, then set AutoFillFormulasInLists back to the original setting.
bool OriginalAutoFillFormulaInListsFlag = app.AutoCorrect.AutoFillFormulasInLists;
app.AutoCorrect.AutoFillFormulasInLists = false;
//[ListObject population code....]
if (OriginalAutoFillFormulaInListsFlag == true)
{
app.AutoCorrect.AutoFillFormulasInLists = true;
}
Hope this helps someone.
I faced a similar issue. Ideally you could tell excel to stop doing this but I haven't been able to figure out how. Supposedly doing the following is supposed to keep excel from copying the formulas:
xlApp.AutoCorrect.AutoFillFormulasInLists = false
but it didn't work for me.
Using the answer from this question How to create running total using Excel table structured references? helped me. It doesn't feel like the ideal solution but it does do the job.
I used this formula where Weight is a column name from my table. #This Row is a "Special item specifier" and has a special meaning. The syntax looks a little funky because it's what's called a Structured Reference:
=AVERAGE(INDEX([Weight],1):[[#This Row],[Weight]])
The INDEX([Weight],1) part gives the reference for the 1st row in the Weight column
While the [[#This Row],[Weight]] part gives the reference for the current row in the Weight column.
So for example, if Weight is column J, and the current row is, say, 7 then this is equivalent to
=AVERAGE(J1:J7)
and on the 8th row it will be equivalent to
=AVERAGE(J1:J8) and so on
I have found that the only way to solve the problem of formulas changing in Excel Tables when you insert in VBA is to insert at the first row of the table, NOT the bottom or the middle. You can sort after.
And I always select or reference the EntireRow to do my insert in the Worksheet object not in the table itself. I always put a table in its own Worksheet anyway using xx.Entirerow.Insert.

Resources