I have a column of data that need to be filled. The formula should go like:
SUM(A10 + B10)
SUM A20 + B20
SUM A30 + B30
. .
. .
I have no idea on how to do the setup. Appreciate for any help :)
You're looking for the INDIRECT worksheet function. You need to nest it within the SUM function and you'll get what you're after. E.g. assuming you're in a cell in the very first row on a worksheet, you type:
=SUM(INDIRECT("A" & ROW()*10), INDIRECT("B" & ROW()*10))
One option is to use INDEX function here, it's not volatile like INDIRECT and will still work if you insert rows or columns, e.g. in cell C2 use this formula copied down
=SUM(INDEX(A$1:B$1000,ROWS(C$2:C2)*10,0))
.....or alternatively, this method will actually give you the formula =SUM(A10,B10) in the first cell and =SUM(A20,B20) in the next cell etc.
Put this formula in C2 and copy down as far as required
="=SUM(A"&ROWS(C$2:C2)*10&",B"&ROWS(C$2:C2)*10&")"
Select whole range > Right Click > Copy > Right Click > Paste Special > Values > OK > ENTER
That creates text versions of the required formulas - to convert to actual formulas do an "Edit/Replace" and replace = with =
Use =SUM(A10,B10) in the first cell and the drag the cell content to all the below cells if you want to fix a attribute like column number than put a $ symbol in front of it eg =SUM($A10,$B10). Similarly, for rows use =SUM(A$10,B$10).
I would probably do something like this, only because I prefer VBA.
Sub FillSheet()
Dim j, k
j = 10
k = 1
For j = 10 to 500 Step 10 '<<--Starts at 10, then 20, 30, etc up to 500
Worksheets("YourWorkSheetName").Range("A" & k).Formula = "=SUM(A" & j & ":B" & j & ")"
k = k +1
Next j
End Sub
Modify according to your requirements. Change "A" if want the formula in another column. Change "500" to however many lines you need.
Related
I have the feeling that my previous question is being misunderstood. Therefore, I will do this as follows. What I would like is the following:
In this picture you see that the input is 4 in cell B1. This B1 cell is used as an input for a complicated model in a different sheet. The output of that model is linked to B2. I do not want to modify the complicated model on the other sheet (this part is required).
Now I would like to create a table like this:
So when the input is 4, the output from the model in the other sheet is 1. Now I can do this manually for 1, 2 and 3, by simply replacing the number written at B2 (which is 4) to 1 and check its output. Let’s observe what our complicated model gives us:
Apparently it gives us a 9, so I fill 9 in the F2. Now this is currently not a problem, since my table goes from 1 to 4. But how do I automate this? For example if I go from 1 to 100.
The problem is, you cannot touch or simplify the model on the other sheet.
How would one do this in VBA or do this in Excel itself?
I think you might have an issue in your explanation. Your example is A1 = 1, B1 = A1+5, despite saying it's A1 & A2. You are saying "Columns C & D" when I believe you mean "Rows 3 & 4).
If I understand your plight correctly, you are looking to drag down (auto-fill) the formula to the subsequent rows. You can do this with VBA, but you can do it without.
Provided you have data in the first column (A) as far as you would like the formula to travel, you can double-click from the lower right hand corner of the formula-reference cell and it will fill down. You could also drag the formula down by clicking and holding the lower right hand corner of the formula-reference cell.
You will know if you have the lower right hand corner if your cursor changes from from a "+" that is relatively large (with interior color) to a "+" that is relatively small (with no interior color... all black).
You will need to ensure that you have relative references when doing this, or ensure that your non-relative references are what you want. This is more than your question asked, but is important when doing this type of work.
See this:
Range("B1").Formula = A1 + 5
In this formula, A1 is relative to B1 by an off-set of -1 column. Every cell that the formula is pasted or dragged into will perform the formula with the cell that is -1 column relative.
You can add specifics to columns, rows, or cells, by use of "$". Say you have your example, and want to show the formula in Column C. There're 3 scenarios by using "$" which have different outcomes:
Fully relative, the dragged-formula will automatically designate the adjacent column.
Range("B1").Formula = A1 + 5
Range("C1").Formula = B1 + 5
Range("B2").Formula = A2 + 5
Range("C2").Formula = B2 + 5
If the "$" is in front of the column in the formula, the-dragged will "lock" the column, so when the formula is dragged, the column stays the same, but the row number will change.
Range("B1").Formula = $A1 + 5
Range("C1").Formula = $A1 + 5
Range("B2").Formula = $A2 + 5
Range("C2").Formula = $A2 + 5
If the "$" is in front of the row in the formula, the-dragged will "lock" the row, so when the formula is dragged, the row stays the same, but the column will change.
Range("B1").Formula = A$1 + 5
Range("C1").Formula = B$1 + 5
Range("B2").Formula = A$1 + 5
Range("C2").Formula = B$1 + 5
If the "$" is in front of the each the column and row in the formula, the-dragged will "lock" both. When the formula is dragged, the referenced-cell stays the same.
Range("B1").Formula = $A$1 + 5
Range("C1").Formula = $A$1 + 5
Range("B2").Formula = $A$1 + 5
Range("C2").Formula = $A$1 + 5
Hopefully that helps and is what you're looking for!
So you want to automate putting values through your model and recording the outputs.
A very simple approach begins with putting your list of inputs in column E as in your example picture. Note down the start and end row numbers - 2 and 5 in your example (as your input values are in the range E2:E5).
In the VBA editor created a new sub in a new module to hold your code (you can Google how to do this). The code is fairly simple.
Sub TurnInputsIntoOutputs()
' First we create some useful and important variables
Dim inputStartRow As Long ' The row your input values start on
Dim inputEndRow As Long ' The row your input values end on
Dim currentRow As Long ' A placeholder for when we cycle through the rows
Dim processingWorksheet As Worksheet ' How we're going to reference your worksheet
' Then we put values into those variables
inputStartRow = 2
inputEndRow = 5
Set processingWorksheet = ThisWorkbook.Sheets("*the name of your worksheet here*")
' Now we cycle through the input rows
For currentRow = inputStartRow to inputEndRow
' Put input value from the current row of column E into cell B1
processingWorksheet.Range("B1").Value = processingWorksheet.Range("E" & currentRow).Value
' Put the resulting output value into the current row of column F
processingWorksheet.Range("F" & currentRow).Value = processingWorksheet.Range("B2").Value
Next currentRow
End Sub
And then just run your macro (you can link it to a Form button or just run it from the VBE - easy enough for your to Google how to do).
is possible to get results from columns A-E into column H without use of VBA?
Do you have any suggestions how to do it? Thank you
It needs to look at the row above, so you need to paste it on row 2 and drag from there. On row 1 you will have a ref error but it will still work. For the future you may want to add an extra empty row at the top and have the formula in H2 and down.
H2:
=IF(COUNT(A2:E2)=0,H1,IF(A2<>"",A2&".",LEFT(H1,FIND("|",SUBSTITUTE(H1,".","|",MATCH(TRUE,INDEX(A2:E2<>"",0),0)-1),1))&INDEX(A2:E2,1,MATCH(TRUE,INDEX(A2:E2<>"",0),0))&"."))
I know it looks like a mess, but I'll try to explain it:
IF(COUNT(A2:E2)=0,H1
This is for the empty rows. if empty use the one above
IF(A2<>"",A2&"."
if A2 has a value start with completely new text
INDEX(A2:E2<>"",0)
returns an array of true/false for if the cells are empty
MATCH(TRUE,INDEX(A2:E2<>"",0),0)
This gives you the columnnumber with the new data
SUBSTITUTE(H1,".","|",MATCH...)
Repleces the n-th dot with a | This is so we can use Find to figure out how much of the text above to keep.
INDEX(A2:E2,1,MATCH(TRUE,INDEX(A2:E2<>"",0),0))
is the new data
You can do this by building up the target in a set of columns that can be hidden.
Following from your example above, if you add columns I, J, K and L, use the following formulae:
Cell I1: =A1 & "."
Cell J1: =IF(B1="","",B1 & ".")
Cell K1: =IF(C1="","",C1 & ".")
Cell L1: =IF(D1="","",D1 & ".")
All other rows:
Column I: =IF(A2="",I1,A2 & ".")
Column J: =IF(B2="",IF(I2=I1,J1,""),B2 & ".")
Column K: =IF(C2="",IF(J2=J1,K1,""),C2 & ".")
Column L: =IF(D2="",IF(K2=K1,L1,""),D2 & ".")
Then set column H to be:
=I1 & J1 & K1 & L1 & M1
This roughly translates as "If there isn't a new number, use the number from the row above, unless the parent number has changed, in which case use a blank."
You can hide columns I, J, K and L, or just use alternative columns further to the right if these are not available.
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 need help with an excel assignment.
Name City
---------------
John London
Maxx NY
Ashley DC
Paris
Solution for this must be:
John-london
John-NY
John-DC
John-Paris
Maxx-london
Maxx-NY
.
.
.
.so on.
Simply I have to add text of all elements in one column to text of all elements in other column. I will appreciate if a solution without macros or VB is provided.
You can use this formula (start in Row 1 and fill down until you run out of combinations):
=IFERROR(INDEX(L_1, CEILING(ROW()/COUNTA(L_2),1) ,1) & "-" &
INDEX(L_2, 1+MOD(ROW()-1, COUNTA(L_2)) ,1), "That's it!")
I'm using named ranges "L_1" and "L_2" to refer to the first and second lists respectively
Here's an Array Formula you can use, though you will need to modify the size of the matrix depending on how many entries you have
=CONCATENATE(INDEX(A:A,MMULT(ROW(A1:A3),TRANSPOSE(ROW(B1:B4))/TRANSPOSE(ROW(B1:B4)))),"-",INDEX(B:B,MMULT(ROW(A1:A3)/ROW(A1:A3),TRANSPOSE(ROW(B1:B4)))))
Assuming Column A is Names and Column B is Cities, you would select 12 cells (3 rows high, 4 columns wide), paste the above formula in the first cell and press Ctrl + Shift + Enter to create the array formula.
If you want to see a little simpler version to see what it does before the INDEX references, check with the same area:
=CONCATENATE(MMULT(ROW(A1:A3),TRANSPOSE(ROW(B1:B4))/TRANSPOSE(ROW(B1:B4))),"-",MMULT(ROW(A1:A3)/ROW(A1:A3),TRANSPOSE(ROW(B1:B4))))
Here's a screenshot (with the formula split in 2 lines) of the single formula displaying the output over multiple cells:
This is a simple example in VBA. It is intended to show the concept, not the best practices. Please use it to get you started, and get back here if you need more help, if you want to improve the performances, etc.
The example assumes that the two lists are in A1:An and B1:Bm, and the resulting list goes in column C.
Sub Test()
Dim R1 As Integer, R2 As Integer, R As Integer, NR As Integer
NR = ActiveSheet.UsedRange.Rows.Count
Columns(3).Clear
For R1 = 1 To NR
If Not IsEmpty(Cells(R1, 1)) Then
For R2 = 1 To NR
If Not IsEmpty(Cells(R2, 2)) Then
R = R + 1
Cells(R, 3) = Cells(R1, 1) & " - " & Cells(R2, 2)
End If
Next R2
End If
Next R1
End Sub
resulting column formula should be
=column1&"-"&column2
How can I append text to every cell in a column in Excel? I need to add a comma (",") to the end.
Example:
email#address.com turns into email#address.com,
Data Sample:
m2engineers#yahoo.co.in
satishmm_2sptc#yahoo.co.in
threed_precisions#rediffmail.com
workplace_solution#yahoo.co.in
threebworkplace#dataone.in
dtechbng#yahoo.co.in
innovations#yahoo.co.in
sagar#mmm.com
bpsiva#mmm.com
nsrinivasrao#mmm.com
pdilip#mmm.com
vvijaykrishnan#mmm.com
mrdevaraj#mmm.com
b3minvestorhelpdesk#mmm.com
sbshridhar#mmm.com
balaji#mmm.com
schakravarthi#mmm.com
srahul1#mmm.com
khramesh2#mmm.com
avinayak#mmm.com
rockindia#hotmail.com
See if this works for you.
All your data is in column A (beginning at row 1).
In column B, row 1, enter =A1&","
This will make cell B1 equal A1 with a comma appended.
Now select cell B1 and drag from the bottom right of cell down through all your rows (this copies the formula and uses the corresponding column A value.)
Select the newly appended data, copy it and paste it where you need using Paste -> By Value
That's It!
It's a simple "&" function.
=cell&"yourtexthere"
Example - your cell says Mickey, and you want Mickey Mouse. Mickey is in A2. In B2, type
=A2&" Mouse"
Then, copy and "paste special" for values.
B2 now reads "Mickey Mouse"
It's simple...
=CONCATENATE(A1, ",")
Example: if email#address.com is in the A1 cell then write in another cell: =CONCATENATE(A1, ",")
email#address.com After this formula you will get email#address.com,
For remove formula: copy that cell and use Alt + E + S + V or paste special value.
There is no need to use extra columns or VBA if you only want to add the character for display purposes.
As this post suggests, all you need to do is:
Select the cell(s) you would like to apply the formatting to
Click on the Home tab
Click on Number
Select Custom
In the Type text box, enter your desired formatting by placing the number zero inside whatever characters you want.
Example of such text for formatting:
If you want the cell holding value 120.00 to read $120K, type $0K
Pretty simple...you could put all of them in a cell using the concatenate function:
=CONCATENATE(A1, ", ", A2, ", ", and so on)
Highlight the column and then Ctrl + F.
Find and replace
Find ".com"
Replace ".com, "
And then one for .in
Find and replace
Find ".in"
Replace ".in, "
Simplest of them all is to use the "Flash Fill" option under the "Data" tab.
Keep the original input column on the left (say column A) and just add a blank column on the right of it (say column B, this new column will be treated as output).
Just fill in a couple of cells of Column B with actual expected output. In this case:
m2engineers#yahoo.co.in,
satishmm_2sptc#yahoo.co.in,
Then select the column range where you want the output along with the first couple of cells you filled manually ... then do the magic...click on "Flash Fill".
It basically understands the output pattern corresponding to the input and fills the empty cells.
I just wrote this for another answer:
You would call it using the form using your example: appendTextToRange "[theRange]", ",".
Sub testit()
appendTextToRange "A1:D4000", "hey there"
End Sub
Sub appendTextToRange(rngAddress As String, append As String)
Dim arr() As Variant, c As Variant
arr = Range(rngAddress).Formula
For x = LBound(arr, 1) To UBound(arr, 1)
For y = LBound(arr, 2) To UBound(arr, 2)
Debug.Print arr(x, y)
If arr(x, y) = "" Then
arr(x, y) = append
ElseIf Left(arr(x, y), 1) = "=" Then
arr(x, y) = arr(x, y) & " & "" " & append & """"
Else
arr(x, y) = arr(x, y) & " " & append
End If
Next
Next
Range(rngAddress).Formula = arr
End Sub
Select the range of cells, type in the value and press Ctrl + Enter.
This, of course, is true if you want to do it manually.
Put the text/value in the first cell, then copy the cell, mark the whole colum and 'paste' the copied text/value.
This works in Excel 97 - sorry no other version available on my side...
This is addition to #Edward-Leno 's answer for more detail/explanation and cases where the text cells are formulas instead of values, and you want to retain the original formula.
Suppose your cells look like this (formulas)
="email" & "#" & "address.com"
=A1 & "#" & C1
instead of this (values)
email#address.com
If "email" and "address.com" were some cells like A1 is the email and C1 is the address.com part, then you'd have something like =A1&"#"&C1 which would be important to retain since A1 and C1 might not be constants and can change, so the comma-concatenated values would change, like if C1 is "gmail.com", "yahoo.com", or something else based on its formula.
Values method: The following steps will successfully append text but only keep the value using a scratch column (this works for rows, too, but for simplicity, the directions are for columns)
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=A1&","
Essentially, the "&" is the concatenation operator, combining two strings together (numbers are converted to strings). The "," can be adjusted to ", " if you want a space after the comma.
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Formula retention method: The following steps will successfully retain the original formula. The process is similar to the values method, and only step 2, the formula used to concatenate the comma, changes.
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=FORMULATEXT(A1)&","
FORMULATEXT() grabs the formula of the cell as opposed to the value of it, so a simple example would be that it grabs =2+2 instead of 4, or =A1 & "#" & C1 where A1 is "Bob" and C1 is "gmail.com" instead of Bob#gmail.com.
Note: This formula only works for Excel versions 2013 and greater. For alternative equivalent solutions for Excel 2010 and older, see this superuser answer: https://superuser.com/a/894441/495155
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Type it in one cell, copy that cell, select all the cells you want to fill, and paste.
Alternatively, type it in one cell, select the black square in the bottom-right of that cell, and drag down.