Interspersing Cells From Different Columns in Excel - excel

I have a rather simple Excel problem that I cannot seem to solve using the fill handle. I have spent two days developing complex formulas to extract and format hexadecimal test data but I'm stuck on what seems a trivial problem. It is best described with an image(I need 10 rep to post directly):
I have a pair of columns of 128 rows, each row across the columns contains consecutive data entries. I want to arrange the data in a single in-order column, as show above. I tried manually entering a series of functions in column D (=A1,=B1,=A2,=B2 etc.) for the first several rows and using the fill handle, but it ends up skipping large chunks of the data.
There must be a simple solution for this sort of rearrangement but I cannot work it out!

In D1 enter
=INDIRECT("a"&QUOTIENT(ROW()+1,2))
then in D2 enter
=INDIRECT("b"&QUOTIENT(ROW()+1,2))
then select D1:D2 and drag the fill handle in D2 down to twice as many rows you have in columns A:B.

Try =INDEX($A$1:$B$128,INT((ROW()+1)/2),ABS(MOD(ROW(),2)-2)). Put in C1 and fill down. If you are putting it somewhere else that doesn't begin at row 1, subtract (start row - 1) from each call to ROW().

in vba, just increment your variables before ending the for, next loop like so:
Sub crundle()
i = 1
j = 1
For i = 1 To 128
ActiveSheet.Range("d" & i) = "=a" & j
ActiveSheet.Range("d" & i + 1) = "=b" & j
i = i + 1
j = j + 1
Next i
End Sub

Related

Find max for values of the same group and indicate the value in a seperate column

I'm trying to find the max in the second column when they belong to the same group defined in the first column and write the maximum value on a third column on the same line as it appears in column 2.
I wrote a piece of code but can't get further.
Sub MarkMax()
Dim i As Double
Dim x As Double
For i = 1 To 1000
For x = 1 To 4
If Cells(i, 1) = x Then
Cells(i, 3) = Application.WorksheetFunction.Max(Cells(i, 2))
End If
Next x
Next
End Sub
Thank you!
No reason for using VBA for that. As explained here there is a basic feature, subtotals, which covers this.
Obviously, as a function, don't use SUM or AVERAGE, but MAX:
You don' need VBA for this. If I were you I would create a table to make the formulas dynamic when you enter new rows.
Supposing that your column headers are in A1:C1 and that you have only 20 rows paste this in first cell of MAX column and copy down.
=IF(MAXIFS($B$2:$B$20;$A$2:$A$20;A2)=B2;MAXIFS($B$2:$B$20;$A$2:$A$20;A2);"")

How to automatically fill down by multiple?

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.

How do I input values in the Excel model without doing it manually?

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).

Concatenate permutations between two columns

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 to the fix the decimals based on another cell decimlas

I have an excel file, i need to do is, format the cell depending on another cell.
If cell "S1" is having 3 decimals cell "T1" should be have the same number of decimals and it is calculated field.
ex:
S1 = 11.123 ---- T1 = 1.099
I need to do for the entire column.
I am able count the number of decimals on the cell by placing the code on worksheet_change. But i have no idea how to format it.
Please help in this regards.
Thanks in advance
Since you wanted an example, here it is :) But please note that this is not an answer. It's just that the comments will not be able to hold all this data plus it will ruin the formatting as well. Having said that, it is almost as good as an answer as it covers almost every aspect of what you want...
My Assumptions:
Data is in Sheet1
Col S and Col T are identical
Logic to achieve what you want
Get the last row of Col S. For example see this
Loop though the cells in Col S and check the number for decimals
Use .Numberformat for format the cells in Col T based on the number of decimals in respective cell in Col S
Few code snippets
Looping Through cells in Col S
For i = 1 To LastRow
If Sheets("Sheet1").Range("S" & i) .... Then
End If
Next i
Setting the number format of cell T in the above loop
'~~> n below is the number of decimal places that you want
Sheets("Sheet1").Range("T" & i).NumberFormat = "0." & String(n, "0")
Incorporate all these and then try to come up with a code. Let use know where you are stuck and we will take it from there.

Resources