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
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
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:

Catlover22

New Member
Joined
Apr 27, 2016
Messages
5
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"
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
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)?
 

Catlover22

New Member
Joined
Apr 27, 2016
Messages
5
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"
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
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
 

Forum statistics

Threads
1,082,309
Messages
5,364,425
Members
400,802
Latest member
RichBRich

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top