Comparing values from 2 sheets - excel

compare values from 1 column in a sheet to another column in a different sheet. If the value is the same, update the cell string to “Special”
I have a code that checks for type of each case I have, so when macros identifies that type it is the string in the cell changes for a specific name.
However, I need to create a new type named Special, when the value on alert_list.Range("E") is NOT "Multi" and the value on alert_list.Range("C") is "Corp". Then the value on column F should be compared to the value on column B in a sheet named child_list to see if it is the same value and if it is the same then a string Special need appear in the cell on the place of "Corp" on alert_list.Range("C").
I create a list on column B in a sheet named child_list and I need to compare this values with the information in another sheet named alert_list column F.
everything above is working I am trying to create another step to be able to have a new type named Special.
If ABC.Range("E" & i).Value <> "Multi" And ABC.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If ABC.Range("F" & i).Value <> TEXT.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = TEXT.Range("B" & k).Value Then
ABC.Range("C" & i).Value = "GOOD"
End If
Next
End If
Exit Sub
But for some reason unknown for me it is not working, I am not getting error However, or it jumps directly to exit sub or just nothing happen after run the code.

It's a bit tricky to explain in comments so here is an answer to explain.
You are executing an Exit Sub statement after your first If...Then...Else block.
If we use some proper indentation, it's much easier to identify this:
Dim Ch_lRow As Long
Ch_lRow = child_list.Cells(Rows.Count, 2).End(xlUp).Row
If alert_list.Range("E" & i).Value <> "Multi" And alert_list.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If alert_list.Range("F" & i).Value <> child_list.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = child_list.Range("B" & k).Value Then
alert_list.Range("C" & i).Value = "Special"
End If
Next
End If
Exit Sub
This additional block you want to add in is obviously within your first loop:
For i = 2 To lRow
...
Dim Ch_lRow As Long
Ch_lRow = child_list.Cells(Rows.Count, 2).End(xlUp).Row
If alert_list.Range("E" & i).Value <> "Multi" And alert_list.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If alert_list.Range("F" & i).Value <> child_list.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = child_list.Range("B" & k).Value Then
alert_list.Range("C" & i).Value = "Special"
End If
Next
End If
Exit Sub
Next i
So this means you have an Exit Sub within your loop, so, within the first iteration the code will fire Exit Sub and your code will do nothing as you put it.
Remove the exit sub from this location and put it outside your For i = 2 to lRow loop as required.

Related

remove rows from excel which have discontinued value in column

I have columns in my csv like this:
Id Name Price
1 Level X discontinued 34
3 Level Y Dicontinued 64
7 Level Z 94
I want to check if in column Name are discontinued or Dicontinued
If yes delete row, if not, dont do nothing, so my final result will be:
Id Name Price
7 Level Z 94
A solution can be running the following Excel Macro ExampleMacro with the following setup. This code will filter the first worksheet [here TotalList] copying the content in a second worksheet [here Filtered]
NOTE: please use the same names I used or change the code of the following macro accordingly if you prefer to change the names. Otherwise it will not work
Sub ExampleMacro()
Dim i As Integer
Dim j As Integer
Set ShMaster = ThisWorkbook.Sheets("TotalList")
Set ShSlave = ThisWorkbook.Sheets("Filtered")
'cleanup for next macro executions
ShSlave.UsedRange.Clear
'copying the headers
ShSlave.Range("A1").Value = ShMaster.Range("A1").Value
ShSlave.Range("B1").Value = ShMaster.Range("B1").Value
ShSlave.Range("C1").Value = ShMaster.Range("C1").Value
'searching what to keep
j = 2
For i = 2 To ShMaster.UsedRange.Rows.Count
'MsgBox "value is " & InStr(1, (Range("B" & i).Value), "discontinued")
If InStr(1, (ShMaster.Range("B" & i).Value), "discontinued") = 0 Then
While ShSlave.Range("C" & j).Value <> ""
j = j + 1
Wend
ShSlave.Range("A" & j).Value = ShMaster.Range("A" & i).Value
ShSlave.Range("B" & j).Value = ShMaster.Range("B" & i).Value
ShSlave.Range("C" & j).Value = ShMaster.Range("C" & i).Value
End If
Next i
End Sub

How to Split and Copy its values

I am a VBA beginner, and am stunned with splitting and copying.
I have a workbook (book1) which contains some student's name in column A and Pay school fee Date in Column B. Some names have more than one date, while some have only one:
A...............B.
Kaka.........10/07/2018
Thy............12/05/2018, 15/08/2018, 15/08/2018
Then I want to search their name in another workbook and copy its entire row to new workbook. All help is welcome; thanks in advance.
You can perform operation this way although You had not show Your code but I have written my own code as my understand about code.
Sub transferData()
'Declare an array
Dim LArray() As String
'Start For loop
For i = 1 To Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A" & i) = Sheets("Sheet1").Range("A" & i).Value
'Check Condition for multiple date exits or not
If (UBound(Split(Sheets("Sheet1").Range("B" & i).Value, ",")) > 0) Then
LArray = Split(Sheets("Sheet1").Range("B" & i).Value, ",")
'Split Date and store inside Array
For j = 0 To UBound(LArray)
Sheets("Sheet2").Cells(i, 2 + j) = LArray(j)
Next j
Else
Sheets("Sheet2").Range("B" & i) = Sheets("Sheet1").Range("B" & i).Value
End If
Next i
End Sub
If You find it helpful then pleasure for me and vote my answer or in case any doubt feel free to ask me.
thanks

How can I test for Three Conditions, and then delete an entire row if all conditions are TRUE?

I have a sheet that I want to check the language in Column R for <> ‘Cash’; if ‘Cash’ do skip to the next row. If Column R <> ‘Cash’, check Column A for duplicate ID (there may or may not be duplicates). If duplicates are found, I want to check Column K for positive/negative values, like 100000 & -100000, then delete the entire row where the negative value appears in Column K. How can I do that?
Following the roles described above, row 6 would be deleted in the image below.
I could use VBA in Excel, or SQL/VBA in Access.
This seems to work pretty well.
Sub TryMe()
Dim i As Long
Dim j As Long
Dim ws As Worksheet
Set ws = Sheet1
For i = 2 To 13
For j = 2 To 13
If ws.Range("A" & i).Value = ws.Range("A" & j).Value Then
If ws.Range("A" & i).Offset(0, 10).Value = -(ws.Range("A" & j).Offset(0, 10).Value) Then
If ws.Range("A" & i).Offset(0, 17).Value <> "Cash" Then
Rows(i).EntireRow.Delete
End If
End If
Exit For
End If
Next j
Next i
End Sub

If function to write to either sheet1 or both sheet1 and sheet2

I'm trying to write an if function into a save button on a user-form so that if the data entered into the user-form is already on sheet 2 then it only gets written to sheet 1. But if it does not exist on sheet 2 then the data from the user-form gets written to both sheet 1 and sheet 2. This is because I want the data on sheet 2 to act like a sort of database and obviously do not want duplicates. I've made the write procedures into two separate modules (I figured this would make it easier to differentiate). Here is my code (Be gentle I'm still learning)
Sub writetosheet1()
Dim i As Integer
i = 1
While ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value <> ""
i = i + 1
Wend
ThisWorkbook.Worksheets("Sheet1").Range("a" & i).Value = UserForm1.txt1.Value
ThisWorkbook.Worksheets("Sheet1").Range("b" & i).Value = UserForm1.txt2.Value
ThisWorkbook.Worksheets("Sheet1").Range("c" & i).Value = UserForm1.txt3.Value
ThisWorkbook.Worksheets("Sheet1").Range("d" & i).Value = UserForm1.txt4.Value
ThisWorkbook.Worksheets("Sheet1").Range("e" & i).Value = UserForm1.txt5.Value
End Sub
Sub writetosheet2()
Dim i As Integer
i = 1
While ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value <> ""
i = i + 1
Wend
ThisWorkbook.Worksheets("Sheet2").Range("a" & i).Value = UserForm1.txt1.Value
ThisWorkbook.Worksheets("Sheet2").Range("b" & i).Value = UserForm1.txt2.Value
ThisWorkbook.Worksheets("Sheet2").Range("c" & i).Value = UserForm1.txt4.Value
ThisWorkbook.Worksheets("Sheet2").Range("d" & i).Value = UserForm1.txt5.Value
End Sub
Private Sub CMDSAVE_Click()
Dim id As Long
id = txt1.Value
If id <> Sheets("Sheet2").Range("a:a").Value Then
Call writetosheet1
Call writetosheet2
Else
Call writetosheet1
End If
End Sub
Any help on this would be fantastic! Thanks.
I think that you can not compare one single value with whole range like this:
If id <> Sheets("Sheet2").Range("a:a").Value Then
You need to go trough all cells in that range separately.
If Application.CountIf(Sheet2.Range("A:A"), id) > 0 then
'write only to sheet1
else
'write to both sheets
end if

Excel macro - unable to call cell value

In my worksheet some cells values are based on other cells
Info worksheet
A1: 5
B1: =A1
Design worksheet
A1:
Is there a way to copy and read the value in B1? I'm trying to use the value in a for loop, with no luck.
Sheets("Info").Select
For i = 1 to 5
If Range("B" & i).Value <> 0 Then
Range("B" & i).Copy Destination:=Sheets("Design").Range("A" & x)
'Sheets("Design").Range("A" & x).Value = Sheets("Offerte").Range("B" & i).Value
x = x + 1
End If
Next i
Your example doesn't seem to match the code well. The line
If Range("B" & i).Value = 1 Then
means that nothing will be copied in your example. It's looking for a cell with 1 in it. Why do you need that If statement at all?
EDIT I am guessing you're just checking that there's something in the cell to copy? I would probably do it this way:
If Trim(Range("B" & i).Value) <> "" Then
Also - did you miss out setting x=1?
There is more than one way to do it. One of them is using 'offset', which is a function that really worth understand. It basically points to a amount of rows / columns from the original cell.
Sub test()
Dim oCell As Excel.Range
Dim i As Integer
Dim x As Integer
Set oCell = Sheets("Info").Range("B1")
x = 1
For i = 1 To 5
If oCell.Offset(i, 0).Value = 1 Then
oCell.Offset(i, 0).Copy Destination:=Sheets("Design").Range("A" & x)
x = x + 1
End If
Next i
End Sub
Besides, you can assert the value instead of using the copy property. Notice it won't work unless x is an integer > 0.
Sub test2()
Sheets(3).Select
x = 1
For i = 1 To 5
If Range("B" & i).Value = 1 Then
Sheets(4).Range("A" & x).Value = Range("B" & i).Value
'Sheets("Design").Range("A" & x).Value = Sheets("Offerte").Range("B" & i).Value
x = x + 1
End If
Next i
End Sub

Resources