If statement in VBA

stoure

New Member
Joined
Dec 23, 2016
Messages
26
Hi!
I'd like to implement this code in VBA. I didn't succeed to do it.
case1 : MENU PRINCIIPAL!J5=3
IF C6="X" then I6="X"
ELSEIF D6="X"then G6="X"
elseif E6="X" then H6="X"
elseif F6="X" then M6="X"
Case 2 : MENU PRINCIPAL!J5=4
IF C6="X" then M6="X"
ELSEIF D6="X"then G6="X"
elseif E6="X" then H6="X"
elseif F6="X" then Q6="X"
Case 3 : MENU PRINCIPAL!J5=5
IF C6="X" then Q6="X"
ELSEIF D6="X"then G6="X"
elseif E6="X" then H6="X"
elseif F6="X" then U6="X"
Case 4 : MENU PRINCIPAL!J5=6
IF C6="X" then U6="X"
ELSEIF D6="X"then G6="X"
elseif E6="X" then H6="X"
elseif F6="X" then Y6="X"
Thanks
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

stoure

New Member
Joined
Dec 23, 2016
Messages
26
Dear experts,
I Succeeded to write this code below, but it's not working well, can something tell me where is the problem :
Code:
Sub test()
ThisWorkbook.Activate
Range("J5").Select
If Range("J5").Value = "3" Then
 Sheets("Quart").Activate
    If Range("C6").Value = "X" Then
    Range("I6").Value = "X"
    ElseIf Range("D6").Value = "X" Then
    Range("G6").Value = "X"
    ElseIf Range("E6").Value = "X" Then
    Range("H6").Value = "X"
    ElseIf Range("F6").Value = "X" Then
    Range("M6").Value = "X"
    End If
    
ElseIf Range("J5").Value = "4" Then
Sheets("Quart").Activate
    If Range("C6").Value = "X" Then
    Range("M6").Value = "X"
    ElseIf Range("D6").Value = "X" Then
    Range("G6").Value = "X"
    ElseIf Range("E6").Value = "X" Then
    Range("H6").Value = "X"
    ElseIf Range("F6").Value = "X" Then
    Range("Q6").Value = "X"
    End If
ElseIf Range("J5").Value = "5" Then
Sheets("Quart").Activate
    If Range("C6").Value = "X" Then
    Range("Q6").Value = "X"
    ElseIf Range("D6").Value = "X" Then
    Range("G6").Value = "X"
    ElseIf Range("E6").Value = "X" Then
    Range("H6").Value = "X"
    ElseIf Range("U6").Value = "X" Then
    Range("Q6").Value = "X"
    End If
ElseIf Range("J5").Value = "4" Then
Sheets("Quart").Activate
    If Range("C6").Value = "X" Then
    Range("U6").Value = "X"
    ElseIf Range("D6").Value = "X" Then
    Range("G6").Value = "X"
    ElseIf Range("E6").Value = "X" Then
    Range("H6").Value = "X"
    ElseIf Range("F6").Value = "X" Then
    Range("Y6").Value = "X"
    End If
End If

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,755
Office Version
  1. 365
Platform
  1. Windows
Bit of a guess as you haven't given us much to go on, but try this
Code:
Sub test()
   If Sheets("MENU PRINCIPAL").Range("J5").Value = "3" Then
      Sheets("Quart").Activate
      If Range("C6").Value = "X" Then
         Range("I6").Value = "X"
      ElseIf Range("D6").Value = "X" Then
         Range("G6").Value = "X"
      ElseIf Range("E6").Value = "X" Then
         Range("H6").Value = "X"
      ElseIf Range("F6").Value = "X" Then
         Range("M6").Value = "X"
      End If
   ElseIf Sheets("MENU PRINCIPAL").Range("J5").Value = "4" Then
      Sheets("Quart").Activate
      If Range("C6").Value = "X" Then
         Range("M6").Value = "X"
      ElseIf Range("D6").Value = "X" Then
         Range("G6").Value = "X"
      ElseIf Range("E6").Value = "X" Then
         Range("H6").Value = "X"
      ElseIf Range("F6").Value = "X" Then
         Range("Q6").Value = "X"
      End If
   ElseIf Sheets("MENU PRINCIPAL").Range("J5").Value = "5" Then
      Sheets("Quart").Activate
      If Range("C6").Value = "X" Then
         Range("Q6").Value = "X"
      ElseIf Range("D6").Value = "X" Then
         Range("G6").Value = "X"
      ElseIf Range("E6").Value = "X" Then
         Range("H6").Value = "X"
      ElseIf Range("U6").Value = "X" Then
         Range("Q6").Value = "X"
      End If
   ElseIf Sheets("MENU PRINCIPAL").Range("J5").Value = "6" Then
      Sheets("Quart").Activate
      If Range("C6").Value = "X" Then
         Range("U6").Value = "X"
      ElseIf Range("D6").Value = "X" Then
         Range("G6").Value = "X"
      ElseIf Range("E6").Value = "X" Then
         Range("H6").Value = "X"
      ElseIf Range("F6").Value = "X" Then
         Range("Y6").Value = "X"
      End If
   End If

End Sub
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,882
Office Version
  1. 365
Platform
  1. Windows
Cross-posted here: https://www.excelforum.com/excel-programming-vba-macros/1213279-offset-for-vba.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here: http://www.mrexcel.com/forum/board-announcements/99490-forum-rules.html).

This way, other members can see what has already been done in regard to a question, and do not waste time working on a question that may already be answered.

For a more complete explanation on cross-posting, see here: http://www.excelguru.ca/content.php?184).
 

Watch MrExcel Video

Forum statistics

Threads
1,122,224
Messages
5,594,916
Members
413,952
Latest member
JGer

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
Top