Adding Another If Statement to a Macro

JamesA11

New Member
Joined
Oct 2, 2020
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
Hello

I have a macro which works ok but I want to add an additional If statement.

At present this line
Code:
ThisCell1.Offset(, 18).Value = 2
just adds the value 2 but I would like to change it so that it adds a 1 if the value in ThisCell2.(Offset(,3)is 1 or a 2 if the value in ThisCell2.(Offset(,3) is 2.

I've tried a few ways but I'm very much a VBA novice and don't seem to be able to add the If within the If.

Thank you

VBA Code:
Option Compare Text

Sub InsertDetails()

Dim ThisCell1 As Range
Dim ThisCell2 As Range
    For Each ThisCell1 In ThisWorkbook.Sheets("Sheet1").Range("A5:A1000")
    'This is the range of cells to check
        For Each ThisCell2 In ThisWorkbook.Sheets("Sheet2").Range("A1:A10")
        'This is the range of cells to compare
            If ThisCell1 <> "" And ThisCell1.Value = ThisCell2.Value Then
                ThisCell1.Offset(, 16).Value = "BK"
                ThisCell1.Offset(, 17) = ThisCell2.Offset(, 4).Value
                ThisCell1.Offset(, 18).Value = 2
                Exit For
                End If
            Next ThisCell2
        Next ThisCell1

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What should happen if ThisCell.Offset(,3) isn't a 1 or a 2?
 
Upvote 0
Hello Fluff, if it's not 1 or 2 the macro shouldn't insert anything. In practice if it's not 1 or 2 it will be blank. Thank you
 
Upvote 0
I that case you can just use
VBA Code:
                ThisCell1.Offset(, 18).Value = ThisCell2.Offset(, 3).Value
 
Upvote 0
Solution

Forum statistics

Threads
1,215,064
Messages
6,122,939
Members
449,094
Latest member
teemeren

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