Multiply every cell based on currency - vba

Hello everyone and thanks for your time, I know I should be doing some work myself but I really don't know where to start.
I have a sheet of between 4000 to 5000 lines (changes every week), like below:
I need a code to go through the D column and change foreign values to KD, I will later delete column C (or to have the code change all symbols to KD, not important though), something like this code:
Sub test()
Dim cell As Range
For Each cell In ActiveSheet.UsedRange
Select Case
Case "$"
Range("UsedRange").Value = ActiveSheet.Evaluate("INDEX(UsedRange *0.3,)")
Case "€"
Range("UsedRange").Value = ActiveSheet.Evaluate("INDEX(UsedRange *0.34,)")
Case "GBP"
Range("UsedRange").Value = ActiveSheet.Evaluate("INDEX(UsedRange *0.4,)")
Case "AED"
Range("UsedRange").Value = ActiveSheet.Evaluate("INDEX(UsedRange *0.08,)")
Case "KD"
Do.Nothing
End Select
Next cell
End Sub
The below code seems to work, verification needed from the pros:
Sub subMultiply()
For Each cel In Range("C2:C" & Range("C2").End(xlDown).Row)
If cel.Value = "$" Then
cel.Offset(0, 1).Value = Val(cel.Offset(0, 1)) * 0.3
ElseIf cel.Value = "AED" Then
cel.Offset(0, 1).Value = Val(cel.Offset(0, 1)) * 0.083
ElseIf cel.Value = "€" Then
cel.Offset(0, 1).Value = Val(cel.Offset(0, 1)) * 0.34
ElseIf cel.Value = "GBP" Then
cel.Offset(0, 1).Value = Val(cel.Offset(0, 1)) * 0.42
End If
Next
ActiveSheet.Range("C:C").Replace "AED", "KD"
ActiveSheet.Range("C:C").Replace "GBP", "KD"
ActiveSheet.Range("C:C").Replace "$", "KD"
ActiveSheet.Range("C:C").Replace "€", "KD"
End Sub

This is one way of writing your code to check, and if the currency is not KD then the rate is multiplied, and the C column cell is changed to KD.
Store the rate of KD in a variable,
Use a loop to go through each row's C and D column together,
i would have an if statement to check each cell in the C column,
for example C2, and then if it is not KD, then use the variable with the KD rate and multiply this with the D2 column.
the C column cell, for example C2 is changed to KD
The loop would move to the next C and D row, for example C3 and D3

Related

Need to see what is different text between two cells

In Excel I have
Column A (Address: example POBOX1234ATLANTAGA30374)
Column B (Address: example POBOX2345ATLANTAGA30384)
I need to make a Column C that shows the difference between the two.
For example, highlight 1234 and 7 as a different font color. I'm open for any ideas on how to do it.
This should do the trick:
Sub CompareCells(c1 As Range, c2 As Range)
Dim p As Long
If c1.Cells.Count + c2.Cells.Count <> 2 Then _
MsgBox "Must specify two single cells.": Exit Sub
For p = 1 To IIf(Len(c2) < Len(c1), Len(c2), Len(c1))
If Mid(c1, p, 1) <> Mid(c2, p, 1) Then c2.Characters(p, 1).Font.Color = vbRed
Next p
End Sub
If your values are in cells A1 and A2, you could use it like this:
CompareCells [a1], [a2]
Sub CompareInColor()
ActiveSheet.Range("C1").Value = ActiveSheet.Range("A1").Value
For i = 1 To Len(ActiveSheet.Range("A1").Value)
If (ActiveSheet.Range("A1").Characters(i, 1).Text <> ActiveSheet.Range("B1").Characters(i, 1).Text) Then
ActiveSheet.Range("C1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i
End Sub
Sub CompareInColorFlip()
ActiveSheet.Range("D1").Value = ActiveSheet.Range("B1").Value
For i = 1 To Len(ActiveSheet.Range("B1").Value)
If (ActiveSheet.Range("B1").Characters(i, 1).Text <> ActiveSheet.Range("A1").Characters(i, 1).Text) Then
ActiveSheet.Range("D1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i
End Sub
This compares A1 and B1 only... Loop through your rows if you have many of them. Also, I assumed that the length of A1 and A2 is the same, otherwise an out-of-range index error may evolve.

Edit value of cells in a specific column if a condition is not met using Excel VBA

I need a macro that will apply the below-mentioned formula in column J if the value of a cell in column C is "Hits_US" and the value of a cell in column D is "harry". Below is the formula
=((Column G*32)+(Column H*28)+300)/60
Please note that there will be other values in column J. So, only if the condition is met, the formula has to be applied.
I tried to do this in parts. I first tried to multiply the value in column G by 32. But it did not work.
For i = 1 To 10
If Sheets(1).Range("C" & i).Value = "Hits_US" And Range("D" & i).Value <> "harry" Then
Cells(i, 10) = Cells(i, 7) * 32
End If
Next i
You should be able to avoid a loop
Sheets(1).Range("J1:J10").Formula = "=IF(AND(C1=""Hits_US"",D1=""Harry""),(G1*32+H1*28+300)/60,"""")"
For all rows in J, based on how many entries are in column C
With Sheets(1)
.Range("J1:J" & .Range("C" & Rows.Count).End(xlUp).Row).Formula = "=IF(AND(C1=""Hits_US"",D1<>""Harry""),(G1*32+H1*28+300)/60,"""")"
End With
If you want to leave a formula in cells then I'd go with R1C1 notation:
Sheets(1).Range("J1:J10").FormulaR1C1 = "=IF(AND(RC3=""Hits_US"",RC4=""harry""),(RC7*32+RC8*28+300)/60,"""")"
While if you want to leave only the formula results then you have to possibilities:
have formulas do the math and then leave only values
With Sheets(1).Range("J1:J10")
.FormulaR1C1 = "=IF(AND(RC3=""Hits_US"",RC4=""harry""),(RC7*32+RC8*28+300)/60,"""")"
.Value = .Value
End With
have VBA do the math and write its results directly
With Sheets(1)
For i = 1 To 10
If .Range("C" & i).Value = "Hits_US" And .Range("D" & i).Value = "harry" Then
.Cells(i, 10) = (.Cells(i, 7) * 32 + .Cells(i, 8) * 28 + 300) / 60
End If
Next
End With

Remove row base on criteria?

I have some values on column A such as:
"A" "B"
1 ok
1 ok
1 me
2 next
2 next
2 next
My code goes and and color the row on "A" if it is all the same, what i want is if column "A" have all one's to check column "B" for the last value which is "me" if it's there, leave those rows with "1" in column A alone, if it's not, delete all the rows that have "1". Not sure how to accomplish that. any help is appreciated.
Dim i As Long
Dim initialPlaceHolderValue As String
Set UsedRng = ActiveSheet.UsedRange
FirstRow = UsedRng(1).Row
LastRow = UsedRng(UsedRng.Cells.Count).Row
r = WorksheetFunction.RandBetween(180, 255)
g = WorksheetFunction.RandBetween(180, 255)
b = WorksheetFunction.RandBetween(180, 255)
initialPlaceHolderValue = Cells(FirstRow + 1, 1).Value
For i = FirstRow + 1 To LastRow
myColor = RGB(r, g, b)
If Cells(i, 1).Value = initialPlaceHolderValue Then
Debug.Print Cells(i, 19).Value
Cells(i, 1).EntireRow.Interior.Color = myColor
Else
Dim myRange As Range
initialPlaceHolderValue = Cells(i, 1).Value
r = WorksheetFunction.RandBetween(180, 255)
g = WorksheetFunction.RandBetween(180, 255)
b = WorksheetFunction.RandBetween(180, 255)
Cells(i, 1).EntireRow.Interior.Color = RGB(r, g, b)
End If
Next i
The following code should achieve what you want (at least what I think you want, your question is not very easily understandable).
Sub RemoveIfNot1AndMe()
For Each cell In Range("Your Range In Column A")
If (cell.Value = "1") Then
If (Range(cell.Address).Offset(0, 1).Value <> "me") Then
Rows(cell.Row).EntireRow.Delete
End If
End If
Next cell
End Sub
Explanations
The loop will go through every cell in your row (could be your column) and if the value is 1 it will check if the cell next to it contains me and if it doesn't delete it.
Something like
"A" "B"
1 ok
1 ok
1 me
2 next
2 next
2 next
Will end up looking like this
"A" "B"
1 ok
1 ok
2 next
2 next
2 next
EDIT
Sub RemoveIfNot1AndMe()
Dim deleteRowsWithValue1 As Boolean
deleteRowsWithValue1 = False
For Each cell In Range("Your range")
If (cell.Value = "1") Then
If (Range(cell.Address).Offset(0, 1).Value = "me") Then
deleteRowsWithValue1 = True
End If
End If
Next cell
If (deleteRowsWithValue1) Then
For i = 1 To Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
If (Range("A" & i).Value = "1") Then
Rows(i).EntireRow.Delete
i = i - 1
End If
Next
End If
End Sub
Something like
"A" "B"
1 ok
1 ok
1 me
2 next
2 next
2 next
Will end up looking like this
"A" "B"
2 next
2 next
2 next
Here is a very basic, brute force approach for you:
Find if there is such a pair of cells: "1" in A-col and "m" in B-col
If Such a pair exist then look for any row with "1" in A-col and NOT "m" in B-column; WARNING: start this at the bottom of the range and look up to the top of the worksheet (otherwise deleting rows is likely to mess up your logic). Delete any such rows.
Redo the LastRow = ... bit since it will become smaller if you deleted any lines.
Do this between the LastRow = ... line and `r = ..." line.
Good luck and share with us your success.
And, btw, it's a good practice to declare ALL the variables if you do declare them at all (as you certainly should). Also, there is no need to declare any WITHIN a loop, over and over again, as you have done there with myRange; just move it to the top of the sub.

How to fill up empty lines in Excel Table with one existing value?

I have an Excel Table, with some columns. But at the moment a have a problem with column Duration.
When I scrolled down the table, i have unexpectedly noticed, that many IDs have empty lines, and only one line of this ID has an actual value.
Is it possible to fill up other empthy lines with this only one existing value using VBA? That means, that all empty values for ID6979960 should be filled up with a value 42:15:56, and so on.
Without that, my other calculations in my table, don't work properly.
I don't know exactly how copying of values works in VBA.
Public Sub FillEmpty()
Dim finded As Range
Dim Sheet As Worksheet
Set Sheet = ActiveSheet 'or any other sheet -> .Sheets("")
With Sheet
lastrow = .Cells(1, 1).End(xlDown).Row
For i = 1 To lastrow
If StrComp(.Cells(i, 2).Value, "") = 0 Then
Set finded = .Columns(2).Find("*", after:=.Cells(i, 2), LookIn:=xlValues)
ID = .Cells(finded.Row, 1).Value
Filler = .Cells(finded.Row, 2).Text
Else
ID = .Cells(i, 1).Value
Filler = .Cells(i, 2).Text
End If
Index = i
While ID = .Cells(Index, 1).Value
.Cells(Index, 2).Value = Filler
Index = Index + 1
Wend
Next i
End With
End Sub
Made it real quick so not the most optimal solution. I tested it with your example and it works. Not sure with many more rows. Check it and let me know if it works for you.
Sub fillerv2()
rowscnt = 1000
tmi = 1
tm = ""
For i = 1 To rowscnt
If tm <> Cells(i, 1).Value Then
For o = tmi To i - 1
If IsEmpty(Cells(o, 2).Value) = False Then
Pattern = Cells(o, 2).Value
Exit For
End If
Next o
For o = tmi To i - 1
Cells(o, 2).Value = Pattern
Next o
tm = Cells(i, 1).Value
tmi = i
End If
Next i

Copy certain values from one to another column and deleting the original value

I want to copy values from one column to another column (into the same row) if the cell contains the word IN and delete the original value. If not, the code should proceed to the next row and perform a new test. Thus the cell in the target column will remain empty.
When I run the code in Excel nothing happens, so I don't know what is wrong.
Ideally the code should jump to the next column (8) and do the same search and paste the value into the same column (5) when it is done with the first column, but this I haven't started with yet. So I do appreciate tips for that as well :)
Sub Size()
Dim i As Integer, a As String
i = 2
a = "IN"
Do While Cells(i, 7).Value <> ""
If InStr(Cells(i, 7), a) Then
'copying the value to another column but within the same row
Cells(i, 7).Copy Cells(i, 5)
Cells(i, 7).Clear
i = i + 1
Else
i = i + 1
End If
Loop
End Sub
I found out that my first cell in column 7 was empty and thus the Do While Cells(i, 7).Value <> "" wasn't working. Hence I'm refering to a different column that always contain data. Note that the solution code also jumps to the 2 next columns in order to search for the same word.
Sub Size()
Dim i As Integer, a As String
j = 0
i = 1
a = "IN"
Range("A1").Offset(i, 0).Select
For j = 0 To 2
Do Until Selection.Value = ""
If InStr(Range("G1").Offset(i, j).Value, a) Then
Range("E1").Offset(i, 0).Value = Range("G1").Offset(i, j).Value
Range("G1").Offset(i, j).Clear
i = i + 1
Range("A1").Offset(i, 0).Select
Else
i = i + 1
Range("A1").Offset(i, 0).Select
End If
Loop
i = 1
Range("A1").Offset(i, 0).Select
Next j
End Sub