Replacing values in a column according to a map - excel

I have two columns A and B in an Excel sheet, similar to the following:-
A B
1 1
2 2
3 4
4 5
5 6
6 7
7 8
8 10
9 11
10 12
11 13
12 15
13 16
14 17
15 18
Now, in a different sheet, I have a column of B values, and I want to 'map' them to their corresponding A values. By 'map' them, I mean replace a B value with the A value that is adjacent to it in the first sheet. How do I do this?

Option 1)
In sheet2 column C you want your results and lets say and your B data is in column D just to mix things up.
=INDEX(SHEET1!$A$1:$A$15,MATCH(D2,SHEET1!$B$1:$B$15,0))
Option 2)
Same setup but lets use the LOOKUP function
=LOOKUP(D2,SHEET1!$B$1:$B$15,SHEET1!$A$1:$A$15)

With Sheet1 like:
and Sheet2 like:
Running this short macro:
Sub Translate()
Dim B As Range, RangeToFix As Range, r As Range
Dim fnd As Range
Set B = Sheets("Sheet1").Range("B1:B15")
Set RangeToFix = Sheets("Sheet2").Range("B1:B11")
For Each r In RangeToFix
Set fnd = B.Find(What:=r.Value, After:=B(1))
If fnd Is Nothing Then
r.Offset(0, 1).Value = "not found"
Else
r.Value = fnd.Offset(0, -1).Value
End If
Next r
End Sub
will Produce this in Sheet2:
This does the "translation" in-place.

Related

How to replace a value only when a given value is present in another column in Excel

This question is a different version of another question I deleted because not correct. Sorry for those who answered the preceding question.
I have an Excel spreadsheet which contains more than 6000 rows filled with values. As an example, here are the first 4 rows of the spreadsheet:
A B C D
1 1,1 1,11 7
2 1,2 1,22 6
3 1,3 1,33 8
4 1,4 1,44 2
What I need to do is to look for each of the value in column D (7, 6, 8, 2, etc.) in column A, and when one of this value is found I want Excel to replace the corresponding value in column B, with the value in column C that is in the same row of the value in column D that has just been found. So, the output I want to obtain looks like the following:
A B C D
1 1,1 1,11 7
2 1,44 1,22 6
3 1,3 1,33 8
4 1,4 1,44 2
As you can see, in this case only the value present in B2 has been replaced with the one present in C2, and this because the only value that is present both in column A and in column D is "2".
Formula in E1
=IF(COUNTIF($D$1:D4,A1)>0,VLOOKUP(A1,$A$1:C4,3,FALSE),B1)
and copy down
New formula for E2 as per your question edit.
=IFERROR(INDEX($C$2:$C$5,MATCH(A2,$D$2:$D$5,0)),B2)
You could try the below macro :
Sub Macro1()
Dim i As Integer
Dim currentValue As Integer
i = 1
Range("D1").Select
LR = Selection.End(xlDown).Row
For i = 1 To LR
currentValue = Cells(i, 4).Value
Range("A1:A" & LR).Select
Set c = Selection.Find(currentValue, LookIn:=xlValues)
If Not c Is Nothing Then
Cells(c.Row, 2).Value = Cells(c.Row, 3).Value
End If
Next i
End Sub

Looping a column and inserting a formula that has relative reference, but made absolute/fixed each time the formula is inserted

This is a follow up from a previous question which has been kindly solved by Jonathan Willcock.
He has helped me with a solution to loop through 2 columns and insert a formula in another column where applicable.
However, my problem now is that the formula I want to insert (in column D below), should be the first corresponding row in column C each time the start is 'triggered'. ie, I'd like column D to show the trigger date each time the loop is triggered.
What this looks like is:
A B C D
1 31/01/2018
2 1 01/02/2018 01/02/2018
3 02/02/2018 01/02/2018
4 03/02/2018 01/02/2018
5 04/02/2018 01/02/2018
6 05/02/2018 01/02/2018
7 1 06/02/2018 01/02/2018
8 07/02/2018
9 08/02/2018
10 09/02/2018
11 1 10/02/2018 10/02/2018
12 11/02/2018 10/02/2018
13 12/02/2018 10/02/2018
14 13/02/2018 10/02/2018
15 1 14/02/2018 10/02/2018
16 15/02/2018
17 16/02/2018
18 17/02/2018
19 1 18/02/2018 18/02/2018
20 19/02/2018 18/02/2018
21 20/02/2018 18/02/2018
Column A is the start trigger, column B is the end trigger, and column D is where I want the formula inserted, which is the date of the trigger.
This is a modification of the code he wrote, which finds the start and stop point of inserting a formula in anothr column:
Sub Button1_Click()
Dim endRow As Integer
Dim doFormula As Boolean
Dim i As Integer
doFormula = False
endRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To endRow
If Cells(i, 1) = 1 Or doFormula Then
Cells(i, 4) = "[formula that returns trigger date of that incident]"
doFormula = True
End If
If Cells(i, 1) <> 1 And Cells(i, 2) = 1 Then
doFormula = False
End If
Next
End Sub
I was wondering if anyone might have some insight on solving this issue?
I understand how to use relative vs absolute RC reference, but not sure how to 'fix' a relative value each time the loop is triggered.
Thank you!

Excel VBA code for categories the data

I have an Excel file that contains some data in column B, now i wish to categories the data in A column like serial number first 1 to 5 again starts from 1 to 5 until the data ends,
for example in below format
1 A
2 B
3 C
4 D
5 E
1 F
2 G
3 H
4 I
5 J
1 K
2 L
3 M
4 N
5 O
I do not have existing code for above task please help me.
you can use the following
put 1 in the Cell A1
put =IF(OFFSET(A2,-1,0)=5,0,OFFSET(A2,-1,0))+1 in cell A2
double click in the bottom corner of cell A2, this will repeat the function for all cells in column A
hope that it will help you
Use some code
Sub DoItGood()
Dim rws As Long, rng As Range, t As Range
Columns(1).ClearContents
rws = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("A1:A" & rws)
x = 1
For Each t In Range("A1:A5")
t = t + x
x = x + 1
Next t
Range("A1:A5").AutoFill Destination:=rng, Type:=xlFillCopy
End Sub
You can get a repeated list of numbers from 1 to n downwards in rows with the following approach:
=MOD((ROW(A1)-1),n)+1
Take the integer remainder of the division row number (starting with 0) and n. You will get 0,1,2,...,n-1,0,1,2,...,n-1,0,1... To this add 1.
In your case n is 5:
=MOD((ROW(A1)-1),5)+1
filled downwards.

Adding up values in columns with conditions

Being beginner and first time on this site, I truly appreciate your help.
WK 1 WK 2 WK 3 WK 4 WK 5 TOTAL HOURS TOTAL OF FIRST 3 WEEKS <> 0
John 10 0 5 6 5 26 21
Smith 4 1 10 3 4 22 15
Peter 0 4 4 4 2 14 12
Susan 5 5 0 5 8 23 15
From my table I want to add only the first three columns that contain no zero. If there's zero on first three, check on next column and add it up to complete three columns again with no zero value. Some function like in Col H TOTAL OF FIRST 3 WEEKS <>0 (where I had to do it manually).
If I can learn set of VB code or any example with formula or macros, thank you so so much. I'm using Excel 2007.
This is the complicated formula Ali M refers to. It's an array formula entered with ctrl-shift-enter:
=IF(COUNTIF(A2:F2,"<>0")=0,0,SUM(A2:INDEX(A2:F2,SMALL(IF(A2:F2<>0,COLUMN(A2:F2),""),MIN(3,COUNTIF(A2:F2,"<>0"))))))
Note that it works if there are less than three non-zero values.
you can use formula but it would be complicated. instead you can use this subroutine that act exactly as you want!
Public Sub y()
Dim i, sum, c As Integer
Dim Rng, Row, cell As Range
Set Rng = Range("B2:F5")
i = 0
For Each Row In Rng.Rows
For Each cell In Row.Cells
If (cell.Value <> 0 And i < 3) Then
sum = sum + cell.Value
i = i + 1
End If
Next cell
Cells(Row.Row, 7).Value = sum
sum = 0
i = 0
Next Row
End Sub
It always put the sum in column H. you can change it by changing this line:
Cells(Row.Row, 7).Value = sum

how to transform three columns to a matrix using macro

I need some help converting three colums into a matrix using excel macro.
Here is an example:
From this:
A A 0
A B 23
A C 3
B A 7
B B 56
B C 33
C A 31
C B 6
C C 5
to this:
A B C
A 0 23 3
B 7 56 33
C 31 6 5
Hope you can help me.
Thanks
Not quite sure what exactly you are meaning by matrix. For the code below I assumed you were looking for a way to read the data in the first two columns as Row and Column data of the output table. Assume the input data is in the Columns 1 - 3 of "Sheet1"
Sub ConvertTableOfData()
Dim testArray(1 to 3)
Dim chkROW as Integer
Dim chkCOL as Integer
Dim chkVAL as Integer
'// index the Row and Column headers
testArray(1) = "A"
testArray(2) = "B"
testArray(3) = "C"
'// Iterate through every row in the initial dataset
For i = 1 to Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
With Worksheets("Sheet1")
'// Assign the Output Row and Column values
'// based on the array indices
For j = 1 to UBound(testArray, 1)
If .Cells(i, 1) = testArray(j) Then
chkROW = j
End If
If .Cells(i, 2) = testArray(j) Then
chkCOL = j
End If
Next j
'// store the actual value
chkVAL = .Cells(i, 3)
End With
'// output table (in Sheet2)
With Worksheets("Sheet2")
.Cells(chkROW, chkCOL) = chkVAL
End With
Next i
'// Add headers to Output table
For i = 1 to 3
With Worksheets("Sheet2")
.Cells(i + 1, 1) = testArray(i)
.Cells(i, i + 1) = testArray(i)
End With
Next i
End Sub
You can also perform this without VBA.
Assume your table of data is in the range A1:C9.
Assume the first number (0) in the 3 by 3 grid of data is cell F3, with A, B, C in the row above, and A, B, C in the column to the left.
Enter the formula in cell F3 as
=INDEX($C$1:$C$9,SUMPRODUCT(--($A$1:$A$9=$E3),--($B$1:$B$9=F$2),ROW($A$1:$A$9)))
Copy this formula to all 9 cells in the 3 by 3 grid.
This generalized to any size of data.

Resources