I have a column A, that contains either one of the following values:
DATEnone
nonenone
noneTIME
DATETIME
I want to write a function that basically does this:
if A1 is "DATEnone" or A1 is DATETIME:
A1 = "D"
elif A1 is "noneTIME":
A1 = "T"
else:
A1 = "S"
how can I do that in an excel cell function?
Copy-Paste this code in a new module and hit F5 to run the macro.
Sub Main()
Application.ScreenUpdating = False
Dim c As Range
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
If StrComp("DATEnone", c, 1) = 0 Or StrComp("DATETIME", c, 1) = 0 Then
c = "D"
ElseIf StrComp("noneTime", c, 1) = 0 Then
c = "T"
Else
c = "S"
End If
Next c
Application.ScreenUpdating = True
End Sub
This code iterates over column A and replaces the contents based on the conditions you specified.
And if you wanted just a function then stick this formula
=IF(OR(A1="DATEnone", A1="DATETIME"),"D", IF(A1="noneTime","T","S"))
in cell B1 and drag it down the B column
Related
I want a formula for column E3 depend on column A3,B3,C3 and D3. If multiple columns show yes or single column show yes I want show as below in pink. Need to combine shctin names which show "yes".Formula required for column E .End result shoul show as pink color.
Check it out
Sub Button1_Click()
Dim x, y, z
For x = 3 To 15
For y = 1 To 4
If UCase(Cells(x, y)) = "YES" Then
z = z & "_" & Cells(2, y)
End If
Next y
Cells(x, 5) = Right(z, Len(z) - 1)
z = ""
Next x
End Sub
User Defined Function,the function code belongs in a regular module.
Place this formula in E3 and drag down,
=Get_It(A3:D3,2)
Function Get_It(a As Range, Return_Row As String)
Dim c As Range
Dim s As String
For Each c In a.Cells
If UCase(c) = "YES" Then
s = s & "_" & Cells(Return_Row, c.Column)
End If
Next c
Get_It = Right(s, Len(s) - 1)
End Function
There's another way using formulas. A little ugly but ok as a non-VBA alternative.
I am trying to conditionally color a bunch of cells on Tab1. I am using data from a column on Tab2 and also a column on Tab3, in an attempt to match to that given cell variables.
Basic logic is:
If there is a match to the cell in column B of Tab2, then check the value in column E of that same row on Tab2.
If that value in column E is greater than zero in Tab2, then colorize the initial cell value in the search range on Tab1 on color... but if I also exist on Tab3 too, then color something else.
Copy and paste of portions of the code. This is a blown out ‘non-working’ version of the code. It takes forever to run, if it does run.
For Each cellValue In mainRng2
‘if I do not exist in SerializedInvtLocations, but do exist in NonSerializedInventory then check the value in cell E is greater than zero.
If VBA.IsError(Application.match(cellValue, Sheets("SerializedInvtLocations").Range("A2:A" & lngLastRowSer), 0)) And Not VBA.IsError(Application.match(cellValue, Sheets("NonSerializedInventory").Range("B2:B" & lngLastRowNon), 0)) Then
For Each cell In Sheets("NonSerializedInventory").Range("B2:B" & lngLastRowNon)
x = x + 1
checker = Application.WorksheetFunction.VLookup(cellValue, Range("B" & x), 1, False)
'if the vlookup value in B2
If (checker = cellValue) Then
'i exist in non-serialized list, do I have a quant > 0?
quant = Application.WorksheetFunction.VLookup(cellValue, Range("E" & x), 1, False)
If quant >= 1 Then
cellValue.Interior.ColorIndex = 8 'teal
‘Sheets("Serialized and Non-Serialized").Range(cell.Address).Interior.Color = RGB(0, 255, 0)
‘ Debug.Print "Checker value is: " & checker & " and " & cell.Address & "/" & cell.Value
i3 = i3 + 1 ‘ counter
Else
cellValue.Interior.ColorIndex = 15 'gray
End If
End If
Next cell
End If
Next cellValue
Currently, the file just hangs and does not produce results (or it taking over 40 minutes to run and I just quit out). If I modify the code and change things up - I CAN get results, but they are not accurate.
EDIT:
Another attempt:
If inSer = cellValue.Value And inNon = cellValue.Value Then
If inNonQuan >= 1 Then
cellValue.Interior.ColorIndex = 46
Else
cellValue.Interior.ColorIndex = 4
End If
End If
If inSer <> cellValue.Value And inNon = cellValue.Value Then
If inNonQuan >= 1 Then
cellValue.Interior.ColorIndex = 8
Else
cellValue.Interior.ColorIndex = 15
End If
End If
If inSer = cellValue.Value And inNon <> cellValue.Value Then
cellValue.Interior.ColorIndex = 4
End If
If inSer <> cellValue.Value And inNon <> cellValue.Value Then
cellValue.Interior.ColorIndex = 15
End If
You should be able to do something with this:
Sub Tester()
Dim c As Range, mainRng2 As Range, t2q As Variant, t3m As Boolean, Tab2, Tab3, wb
Set wb = ActiveWorkbook 'or ThisWorkbook ?
Set Tab2 = wb.Worksheets("Tab2")
Set Tab3 = wb.Worksheets("Tab3")
Set mainRng2 = wb.Worksheets("Tab1").Range("A2:A1000") 'for example
For Each c In mainRng2
'quantity on Tab2 from colE, based on ColB match
' will be an error value if no match found
t2q = Application.VLookup(c.Value, Tab2.Range("B:E"), 4, False)
'any match on Tab3 ColA ?
t3m = Not IsError(Application.Match(c.Value, Tab3.Range("A:A"), 0))
'did we get a quantity from Tab2 (was there any match)?
If Not IsError(t2q) Then
If t2q >= 1 Then
'15 if also a match on tab3, else 8
c.Interior.ColorIndex = IIf(t3m, 15, 8)
End If
End If
Next c
End Sub
My solution was disappointingly simple - VLookup only returns the first instance of a matching value, and not all subsequent values. Instead of vlookup, I should have basically 'summed' the values of a column to get anything greater than zero.
I need to retrieve the last but one row value in column A/B(Value in Date)
Ex1: Row 3 : last column is AB. I need Row 3 Column AA value (Date) in Row 3 Column C.
Ex 2: Row 4:last column in AS: I need Column AR value (date) in ROw 4 column C.
Ex 3:Row 5:last column in BC: I need Column BB value (date) in ROw 5 column C.
there can b one or 2 blank values.
Please let me know if there are any excel formula to tackle this scenario.
with iteration you could use an array-formula like: (for row 3)
=INDIRECT("R3C"&MAX(COLUMN(3:3)*(LEN(3:3)>0))-1, 0)
without you need to exclude the cell itself... having it at column C it would be something like: (for row 3)
=INDIRECT("R3C"&MAX(COLUMN($D3:$ZZ3)*(LEN($D3:$ZZ3)>0))-1, 0)
if you want to simply auto fill the formula down then replace the "R3C" with "R"&ROW()&"C"
The formulas shown here are array formulas and must be confirmed with Ctrl+Shift+Enter.
If you get inclined to VBA, you could try something like this:
' In C3 type =GetSecondLastColumnValue()
' This function will keep on checking values on column at a time to the right
' If 3 continuous empty cells are found, it will assume that there are no more
' columns with values after that. The last known cell with value will be identified
' and value of column just to its left will be plugged in to column C
Public Function GetSecondLastColumnValue()
Dim Looper As Integer
Looper = 1
Const NoOfContinuousEmptyColumns As Integer = 3
Dim m_Address As String, m_LastKnownColumnWithValue
m_Address = Selection.Address
m_LastKnownColumnWithValue = m_Address
Do
m_Address = Range(m_Address).Offset(0, 1).Address
If Range(m_Address).Value = "" Then
Looper = Looper + 1
If Looper > NoOfContinuousEmptyColumns Then Exit Do
Else
Looper = 1
m_LastKnownColumnWithValue = m_Address
End If
Loop
m_LastKnownColumnWithValue = Range(m_LastKnownColumnWithValue).Offset(0, -1).Address
GetSecondLastColumnValue = Range(m_LastKnownColumnWithValue).Value
End Function
Example:
A B C D E F
1 abc def ab cd
2 abc def xy zx st
In C1, type =GetSecondLastColumnValue () and it will be populated with ab.
In C2, type the same formula and it will be populated with zx
This is just an example of what you can do with VBA. Do NOT use this function in production. It is insanely greedy and slow. It's just an illustration.
This subroutine is much faster:
' This method keeps on going to far right till it reaches the end of the columns
' Then it comes back to the last column with value, and hops to the column to its left, and remembers its value
' That value goes in column C. It loops through all rows you specify in startrow and endrow
Sub PopulateColumnC()
Dim StartRow As Integer, EndRow As Integer, RowLoop As Integer
StartRow = 1
EndRow = 3
Dim m_Address As String
For RowLoop = StartRow To EndRow
Range("A" & RowLoop).Select
m_Address = Selection.Address
Do
Selection.End(xlToRight).Select
If m_Address = Selection.Address Then
Selection.End(xlToLeft).Select
Exit Do
Else
m_Address = Selection.Address
End If
Loop
m_Address = Range(Selection.Address).Offset(0, -1).Address
Range("C" & RowLoop).Value = Range(m_Address).Value
Next
End Sub
For a project I'm looking to compare Col.A and Col.A. I've added the numbers using code but cant work out how to compare the two in Col. C for example if Col.A and Col B match I want Col.C to say "yes" or "No". also if I wanted to do this with Codes and not numbers would I add just string and not int? Or what if I wanted to match dates. Any help be great as I'm just getting back into VB
The code I've worked on is below
Private Sub CommandButton1_Click()
Cells.ClearContents
Range("A1") = "Column A"
Range("B1") = "Column B"
Range("C1") = "Column C"
Dim i As Boolean
For i = 2 To 25
Range("A" & i) = Int((10 - 2 + 1) * Rnd + 2)
If i < 26 Then
Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1) = _
Int((10 - 2 + 1) * Rnd + 2)
End If
Next i
End Sub
Can you use Excel's built in exact function?
FormulaR1C1 = "=EXACT(RC[-2],RC[-1])"
The function will return a value of TRUE or FALSE
Before the snippet FormulaR1C1 you'll want to input the destination cell for the formula
The RC[-2],[RC-1] says perform the "EXACT" calculation on the cells: 1 cell to the left and 2 cells to the left
I have wbk1.worksheet(1) and wbk2.worksheet(1).
wbk1.worksheet(1) has a list of values in column A
wbk2.worksheet(2) has the same list of values that may occur multiple times in column A with a number value in the offset(0,1) cell.
I need to do an index or match to find all of values in wbk2 and sum all of the offset(0,1) values. Then take that sum and put it in the offset(0,1) cell in wbk1.worksheets(1).
Example:
Workbook 1, sheet 1
Column A Column B
value 1
value 2
value 3
Workbook 2, sheet 1
Column A Column B
value 1 15
value 2 2
value 1 3
value 1 12
End Result:
Workbook 1, sheet 1
Column A Column B
value 1 30
value 2 2
value 3 0
I've tried doing a for each loop, but I'm still a noob to vb, so clearly not doing something right:
For Each x In rngWbk1
Set cellrngwbk2 = wbk2.Worksheets(1).Cells.Find(What:=x, LookIn:=xlValues)
If Not cellrngwbk2 Is Nothing Then
For Each y In rngwbk1
If y = cellrngwbk2 Then
total = total + cellrngwbk2.Offset(0, 1).Value
Else
End If
Next y
x.Offset(0, 1).Value = total
total = 0 'resets total value for next x value
Else
End If
next x
If VBA is not a requirement, a simple =SUMIF() statement has the same effect.
The function would look something like this:
=SUMIF([Wbk2.xlsx]Sheet1!A2:A5,Table1[[#This Row],[ID]],[Wbk2.xlsx]Sheet1!B2:B5)
There is more efficient way. You can use SUMIF formula to calculate values and then rewrite formula with result values. If rngWbk1 corresponds to the values in column A in wbk1.worksheets(1), you can use following code:
Dim frm As String
Dim startCell As String
startCell = Replace(rngWbk1.Cells(1, 1).Offset(0, -1).Address, "$", "")
frm = "=SUMIF('[" & wbk2.Name & "]" & wbk2.Worksheets(1).Name & "'!A:A," & startCell & ", '[" & wbk2.Name & "]" & wbk2.Worksheets(1).Name & "'!B:B)"
With rngWbk1.Offset(0, 1)
.Formula = frm
.Calculate
.Value = .Value
End With
If rngWbk1 doesn't correspond values in column A, you need to adjust startCell in example to startCell = "A2" and change With rngWbk1.Offset(0, 1) to sth like With wbk1.Worksheets(1).Range("B1:B100")