Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Good morning,
Please could someone help me with this. I currently have the following piece of code in my workbook: Sub ProdCheck() Dim Answer As String If Range("B2").Value = "ESX" Then Range("AB2").Value = 10 '*****this is where the ELSEIF argument of the code should be***** Else: Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Does this product exist?") If Answer = vbNo Then MsgBox "You pressed NO!" Range("AB2").Value = 0 Else MsgBox "You pressed Yes!" Range("AB2").Value = 1000 End If End If End Sub Whilst the code works, it has two issues with regards to what I am trying to achieve: 1.) I need it to loop through the whole worksheet performing the if statement. ie: not just row 2, but row 2-last row on worksheet 2.) in the IF statement itself, I need to include an ELSE IF (see commented secion in code), and for the code to check whether the value in the cell "B" it is checking is in ColumnA of worksheet2, and if so for "AB2" = 100, if not - then continue to the ELSE part of the current code Please can someone help with this, I am completely stuck Really appreciate it Regards, |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry, just saw an area of my request that may cause confusion:
2.) in the IF statement itself........... and if so for cell "AB" in the row it is checking to = 100, if not......... What I am looking for is for the Cell AB of the each line to either equal 10, 1000, 100 or 0 depending on the results of the code Thanks again in advance for any help |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi,
I think i got it. A couple of points. 1. I created the string MyNote so change to suit 2. This operates on Sheet1 so change that to suit 3. ChkRange is the range on worksheet 2 where we look up the value so change that to the correct sheet and range Sub ProdCheck() Set Sht = Sheets("Sheet1") ' change to suit Set ChkRange = Sheets("Sheet2").Range("A1:A10000") 'Chng to suit MyNote = "Some text" Dim Answer As String Dim LastRow As Long Dim C As Range LastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row Set MyRange = Range("B2:B" & LastRow) For Each C In MyRange If UCase(C.Value) = "ESX" Then C.Offset(, 26).Value = 10 ElseIf WorksheetFunction.CountIf(ChkRange, C.Value) 0 Then C.Offset(, 26).Value = 100 Else Answer = MsgBox(MyNote, vbQuestion + vbYesNo, _ "Does this product exist?") If Answer = vbNo Then MsgBox "You pressed NO!" 'I think the user knows that C.Offset(, 26).Value = 0 Else MsgBox "You pressed Yes!" 'I think the user knows that C.Offset(, 26).Value = 1000 End If End If Next End Sub -- Mike When competing hypotheses are otherwise equal, adopt the hypothesis that introduces the fewest assumptions while still sufficiently answering the question. "PVANS" wrote: Good morning, Please could someone help me with this. I currently have the following piece of code in my workbook: Sub ProdCheck() Dim Answer As String If Range("B2").Value = "ESX" Then Range("AB2").Value = 10 '*****this is where the ELSEIF argument of the code should be***** Else: Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Does this product exist?") If Answer = vbNo Then MsgBox "You pressed NO!" Range("AB2").Value = 0 Else MsgBox "You pressed Yes!" Range("AB2").Value = 1000 End If End If End Sub Whilst the code works, it has two issues with regards to what I am trying to achieve: 1.) I need it to loop through the whole worksheet performing the if statement. ie: not just row 2, but row 2-last row on worksheet 2.) in the IF statement itself, I need to include an ELSE IF (see commented secion in code), and for the code to check whether the value in the cell "B" it is checking is in ColumnA of worksheet2, and if so for "AB2" = 100, if not - then continue to the ELSE part of the current code Please can someone help with this, I am completely stuck Really appreciate it Regards, |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Mike, it works absolutely perfectly.
Thank you so much, that really is brilliant!!!! Have a good day, Regards |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Glad I could help and thanks for the feedback
-- Mike When competing hypotheses are otherwise equal, adopt the hypothesis that introduces the fewest assumptions while still sufficiently answering the question. "PVANS" wrote: Mike, it works absolutely perfectly. Thank you so much, that really is brilliant!!!! Have a good day, Regards |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
for loop with if statement | Excel Worksheet Functions | |||
Do i need a Next or Loop statement ? | Excel Programming | |||
loop Statement | Excel Programming | |||
If statement - Loop? | Excel Discussion (Misc queries) | |||
Help with a loop VBA with an if statement | Excel Programming |