I have an excel file with 15000 rows of data. I have numerical values in Columns D & E . I have to calculate the difference between D & E and store it in F using VBA. I am new to VBA Help is appreciated.
Is it absolutely necessary that you use VBA for this ? If I understand the question correctly this would be much easier to do with excel formulas rather than using an array and having to loop through.
In cell F1 you need to type the following formula:
= D1-E1
Be sure to include the equals sign. Then press enter. Double click the bottom right corner of cell in F1 and it should auto fill all the way down.
Hope this helps. This is my first post so hopefully I haven't done anything wrong !
For data between rows 2 and 15000, run:
Sub SplitTheDifference()
Range("F2:F15000").Formula = "=D2-E2"
End Sub
The formulas adjust as we move down the columns
If we need values rather than formulas in column F, use:
Sub SplitTheDifference()
With Range("F2:F15000")
.Formula = "=D2-E2"
.Value = .Value
End With
End Sub
Related
https://i.stack.imgur.com/EXqon.png
I have tried to get results like the image, but I get these results using VBA, is there any way to use functions to get these results?
VBA code
Function SumNumbers(rng As Range) As Double
Dim a, e, m As Object
a = rng.Value
With CreateObject(“VBScript.RegExp”)
.Pattern = “\d+(\.\d+)?”
.Global = True
For Each e In a
If .test(e) Then
For Each m In .Execute(e)
SumNumbers = SumNumbers + Val(m.Value)
Next
End If
Next
End With
End Function
Please help me, so that I can get 61 results without using VBA ,thanks
If the numbers are always at the front of a string:
=SUMPRODUCT(--LEFT(B1:B4,SEARCH(" ",B1:B4)))
Here's how I solved your problem in my example.
I created a helper column. In fact, I used column C but it could be any column, preferably (but not necessarily) on the same sheet as your data.
I entered this formula in it's row 2 and copied down to as far as column B has data. Note that I trimmed the original text to ensure that there are no leading blanks that shouldn't be there.
=VALUE(LEFT(TRIM(B2),FIND(" ",TRIM(B2))))
I hid the helper column. That wouldn't be necessary if you have it on another sheet.
I entered =SUM(C2:C5) below the last row of the original column B.
Done.
Please see below
I want to concatenate 'comments' in table 2 into table 1 as shown in the series of images without using TEXTJOIN() or macros. Only using regular excel functions
There is no simple solution without using UDF or helper columns. I would suggest using UDF formula which is simple to implement and use in worksheets. To use this approach, please enter this code in your regular module(module1).
Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)
Dim i As Long
Dim result As String
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & " " & Return_val_col.Cells(i, 1).Value
End If
Next
Lookup_concat = Trim(result)
End Function
now you can use this UDF just like regular worksheet formula. Enter this formula =Lookup_concat(G3,$D$3:$D$12,$E$3:$E$12) in cell I3 and drag it to the bottom.
in case you want to use only regular formulas, you will need to enter this formula =IFERROR(INDEX($D$3:$E$12, SMALL(IF(($G3=$D$3:$D$12), ROW($D$3:$D$12)-MIN(ROW($D$3:$D$12))+1, ""),COLUMNS($A$1:A1)), 2),"") in cell K3 using CTRL+SHIFT+ENTER combination since it is an array formula. Now drag formmula to the right and down(Estimate how far to the righ your formula needs to go in order to catch all unique values).
Then enter this formula =CONCATENATE(K3," ",L3," ",M3," ",N3," ",O3," ") in cell J3 and drag it to the bottom (adjust formula to estimated number of unique values).
There's a simple way to do this. :) Please see this Google sheet for a working example.
You can use the FILTER and JOIN functions to achieve this:
=iferror(join(", ", filter(E$3:E$12, D$3:D$12 = G3)))
In the above example the FILTER function will look at cells D3:D12 and try to find rows matching the value in G3. For the matching rows, the FILTER function returns the values from cells E3:E12 as an array.
JOIN is used to join the array items together with a comma in between.
Finally, IFERROR gets rid of N/A errors resulting from FILTER not matching anything.
(Kudos to original answer here https://stackoverflow.com/a/23367059/36817)
You will need to add a helper column to achieve your goal.
Assuming you have the helper column C and this is the array formula (means you have to click Ctrl + Shift + Enter altogether) you should try:
{=IF(OR(ROW(C1)=1,MAX(--($A$1:A1=A2)*ROW($A$1:A1))=0),B2,INDEX($C$1:C1,MAX(--($A$1:A1=A2)*ROW($A$1:A1)))&", "&B2)}
Now at column G assuming this is the place you want to get your outcome, you can enter this array formula (means you have to click Ctrl + Shift + Enter altogether):
{=IFERROR(INDEX($A$2:$C$11,MAX(--($A$2:$A$11=E2)*ROW($A$2:$A$11))-1,3),"")}
This way you should get the results you are expecting.
I am trying to create a large spreadsheet(10,000 rows of formulas) that takes information in from two other sheets. The basic layout that I want is:
Row1
=Sheet1!A7
=Sheet2!M7
=Sheet1!A8
=Sheet2!M8
=Sheet1!A9
=Sheet2!M9
...etc.
When I try to use to formula auto fill, excel picks up on the wrong pattern and I end up with something like this:
=Sheet1!A7
=Sheet2!M7
...
=Sheet1!A11
=Sheet2!M11
=Sheet1!A17
=Sheet2!M17
I gave excel 10 cells to base the pattern off of, and have not been able to get it to work. Can anyone help me figure out how to do this (hopefully without VBA)?
Try to avoid the volatile¹ OFFSET function or INDIRECT / ADDRESS function pairs in favor of the INDEX function and a little maths.
In the first two cells use these two formulas.
=INDEX(Sheet1!A:A, 7+ROW(1:1)/2)
=INDEX(Sheet2!M:M, 7+ROW(1:1)/2)
Select the two cells and drag the Fill Handle down.
¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. Examples of volatile functions are INDIRECT, OFFSET, TODAY, NOW, RAND and RANDBETWEEN. Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well.
Here is a simple VBA macro that sets up your links. Use it until a non-VBA solution is presented:
Sub propagator()
Dim i As Long, k As Long
k = 1
For i = 7 To 99
Cells(k, 1).Formula = "=Sheet1!A" & i
k = k + 1
Cells(k, 1).Formula = "=Sheet2!M" & i
k = k + 1
Next i
End Sub
Just select the destination worksheet and run the macro.
Adjust the 99 to meet your needs.
You can solve this without VBA with some =INDIRECT trickery -- the following is located in "Sheet3":
You can type out Sheet1!A and Sheet2!M in cells A1 and A2 respectively and fill down. Then, type a 7 in B1 and the formula in B2 -- again fill down. This first formula is effectively incrementing the count by two. Finally, you can type the formula in C1 and fill down.
Use the INDIRECT function to build this, and you can make the pattern work based on your current row number. Assuming your first cell is in Row 2:
=INDIRECT("Sheet1!A" & 7+(ROUNDDOWN(ROW()/2,0)-1))
=INDIRECT("Sheet2!M" & 7+(ROUNDDOWN(ROW()/2,0)-1))
ROW() returns the current row, which is then divided by 2 (since you only increase one row reference for every two cells), and then subtracted 1 (since we want the first adjustment to be 0).
Try in row 2:
=INDIRECT("Sheet1!A" & 7 +QUOTIENT(ROW()-2,2))
And in row 3:
=INDIRECT("Sheet2!M" & 7 +QUOTIENT(ROW()-2,2))
Highlight both and copy down.
I am trying to concatenate content of two different columns row wise but I am not getting a way by which I can do this with a single formula/condition.
name Surname
A B
C D
E F
.............
And I want to display:
A B
C D
E F
.....
I know the formula to concatenate like =CONCATENATE(A1," ",B1).
But I know that I can do this for individual rows. Is there any way by which I can do this for all rows simultaneously with single formula dependoing on number of rows?
I use a macro to do that, the macro works with selected text, you can easily change it to suit your solution:
sub Append_Text()
'Add right text to the left text
for each c in Selection
if c.Value <> "" then c.Offset(0,2).Value = c.Value & c.Offset(0,1).Value
next
end sub
You select the left side values and run the macro, it would be a good idea to assign a shortcut for the macro to speed up the process.
Let me know if this will work for you.
If you want to achieve this using a single formula such as =A1&B1,
then first convert your data as table (select a cell in your data, use the Ribbon menu Insert -> Table ) and then use the formula which will fill down automatically.
I have a column A with dates which has 1000 entries. I want Bi = Weekday(Ai) for all i <= 1000. How can I apply the method to only 1000 cells in column B and not more(without dragging)?
With some dates in column A; this places the formulas in column B:
Sub CreateFormula()
Range("B1:B1000").Formula = "=WEEKDAY(A1)"
End Sub
and the formula automatically adjust for each cell,(just like drag-down)
There is absolutely no need for VBA (though you may want it).
Enter your formula in B1.
Copy the formula.
Goto cell B1000 (shortcut Ctrl+G may work, depending on your keyboard setting).
Select B1:B1000 with Shift+Ctrl+Up.
Paste.
Copy formula. Bi = Weekday(Ai) the count i will be updated automatic.