Need Help with Creating an If-Then Statement

Catlover22

New Member
Joined
Apr 27, 2016
Messages
5
I just started learning the ropes to VBA and don't fully understand it yet. I am trying to create something that will change the value of two different cells, based on the contents of one cell.

Specifically, if the text in column B says "shoecare", then the contents in column E need to change to "48" and the contents in column F need to change to "16"

Then I need to repeat this for a couple other scenarios exactly like this except with different contents. I need for the entire column to be checked.

What I have so far:
-The first part is supposed to be IF B3 says shoecare, then change E3 and F3.
-The second part (to my understanding) is supposed to say essentially "repeat this down the entire column"
I get a "Block If without End If" error message
-Also, if I need to repeat this for multiple different instances, can I just copy this code and replace what variables I need in the correct spots?

Sub Shoecare()

If Range("B3").Value = "Shoecare" Then
Range("E3").Value = "48" And Range("F3").Value = "16"

Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("B" & i).Value = "Shoecare" Then
Range("E" & i).Value = "48" And Range("F" & i).Value = "16"

End If
Next i

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the forum!

You have chosen to loop through the cells your code will operate on. Nothing wrong with that, but there are much faster ways which you can learn later. For now, you could use something like this after you replace Scenario2, Scenario3 with your other keywords, and enter the companion values you want in cells E & F for those scenarios:

Untested.
Code:
Sub Shoecare()
Dim LastRow As Long
Dim i As Long, j As Long
Dim Scenarios As Variant
Scenarios = Array("Shoecare", "Scenario2", "Scenario3") '<-- add as many as you want
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LBound(Scenarios) To UBound(Scenarios)
    For j = 2 To LastRow
        If Range("B" & j).Value = Scenarios(i) Then
            Select Case Scenarios(i)
                Case "Shoecare"
                    Range("E" & j).Value = "48"
                    Range("F" & j).Value = "16"
                Case "Scenario2"
                    Range("E" & j).Value = "Scenario2 value here"
                    Range("F" & j).Value = "Scenario2 value here"
                Case "Scenario3"
                    Range("E" & j).Value = "Scenario3 value here"
                    Range("F" & j).Value = "Scenario3 value here"  '<--- add any additional Scenarios after this
            End Select
        End If
    Next j
Next i
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
This worked perfectly!!!

The next question I would have is how to delete random text that imports in when I open the original file.

I would need it to find this text within the document and delete it out.
Preferably, if the cells beneath this text would automatically shift cells up, so that i have a continuous list of data without the text being in it.

The text looks something like this:

"Section
Floorplan width height depth
PlanogramSET"
 
Upvote 0
This worked perfectly!!!

The next question I would have is how to delete random text that imports in when I open the original file.

I would need it to find this text within the document and delete it out.
Preferably, if the cells beneath this text would automatically shift cells up, so that i have a continuous list of data without the text being in it.

The text looks something like this:

"Section
Floorplan width height depth
PlanogramSET"
Can that text appear in more than one cell? Is the text part of longer text in the cell or is it the entire text in the cell(s)?
 
Upvote 0
Can that text appear in more than one cell? Is the text part of longer text in the cell or is it the entire text in the cell(s)?

Yes. Different cells are filled. Maybe A1 and B2:6 and each cell contains 1 - 2 words. i.e. "Fixture Type" "Width" Height"
 
Upvote 0
Yes. Different cells are filled. Maybe A1 and B2:6 and each cell contains 1 - 2 words. i.e. "Fixture Type" "Width" Height"
In post #3 you have a phrase with 6 words, so I'm a bit confused. Can you post a sample of your data and a more detailed explanation of what you want to accomplish. You can use one of the links below to post the data:

Excel Jeanie link: Download
MrExcel HTML Maker link: http://www.mrexcel.com/forum/2545970-post2.html
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top