Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default VBA Loop If statement

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default VBA Loop If statement

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA Loop If statement

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default VBA Loop If statement

Mike, it works absolutely perfectly.

Thank you so much, that really is brilliant!!!!

Have a good day,

Regards
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA Loop If statement

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop with if statement Steve Excel Worksheet Functions 1 February 17th 10 07:56 PM
Do i need a Next or Loop statement ? Corey Excel Programming 1 March 22nd 07 03:22 AM
loop Statement Nigel Excel Programming 0 April 4th 06 04:10 PM
If statement - Loop? George Excel Discussion (Misc queries) 1 March 14th 06 07:06 AM
Help with a loop VBA with an if statement kixelsid Excel Programming 10 February 28th 06 07:15 PM


All times are GMT +1. The time now is 09:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"