Call a macro based on text in column C (same row that is active).

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
Hi I was hoping someone could help me with this.

I am looking to call different macros depending on what text is in column C on the same row of a selected cell.

So when I select "Open" on Cell Y2, the macro will check text in cell C2 (-22) and call a macro depending on the text in that cell.

If ActiveCell.Offset(0, -22).Range("A1") = "Customer Concern" Call CC
If ActiveCell.Offset(0, -22).Range("A1") = "CAPA" Call CAPA
If ActiveCell.Offset(0, -22).Range("A1") = "Interruptions" Call Interruptions
If ActiveCell.Offset(0, -22).Range("A1") = "Supplier Non-Conformance" Call NCR
If ActiveCell.Offset(0, -22).Range("A1") = 'anything else "Please select a valid Quality Issue Type"
End sub

I know this is not correct but I am looking for something like this.

Thank you to anyone who can help.

What I have so far:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)



If Intersect(Target, Range("Y:Y")) Is Nothing Then Exit Sub

Select Case Target.Text

Case "Open": Call Complete_File

Case "Closed": Call Verify_Form

Case Else: Exit Sub

End Select



End Sub



Sub Complete_File()

'

' Complete_File Macro

'

Dim msg As String, ans As Variant, lr As Long, Sh As Worksheet, lo As ListObject



If Application.CountIf(Sheets("CC Database").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 20)), "") > 0 Then

MsgBox "Please Complete all manditory fields (Columns B to T)"

Range(Cells(Selection.Row, 25), Cells(Selection.Row, 25)).ClearContents

Exit Sub

End If

‘*****Add requested code here*****

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1New IdentifierOriginal IdentifierTypeDate OpenedOpened byEstimated Completion DateDivisionArea/Department/CellSourceDiscrepancy/Issue/ReasonWork Order/Job #PO#Model #QtyCorresponding NCR/CC/CAPA/Interruption #Customer/VendorPart Name / DescriptionPart Number / Document ReferenceSerial # / ProcessOwnerVerification/Action/RepairedWarranty Granted Y/N /ReprocessComments/NotesFlag DateStatusClosed DateMan HoursLabour CostMaterial CostTotal CostWork done in good faithTotal Days Active
2Q5000CC1234Customer Concern30-Oct-241st DivisionFrontErrorN/A123456N/A15555CustomerPart444444Open
Sheet1
Cells with Data Validation
CellAllowCriteria
Y2List=Sheet2!$A$1:$A$2
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I haven't tried to validate your existing code but this will add the functionality you described:
VBA Code:
         Select Case Cells(Selection.Row, "C")
         
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
            
         End Select

You don't need to use "Call", it's obsolete and included in the language for backwards compatibility to old code.
 
Upvote 0
Solution
Hi I was hoping someone could help me with this.

I am looking to call different macros depending on what text is in column C on the same row of a selected cell.

So when I select "Open" on Cell Y2, the macro will check text in cell C2 (-22) and call a macro depending on the text in that cell.

If ActiveCell.Offset(0, -22).Range("A1") = "Customer Concern" Call CC
If ActiveCell.Offset(0, -22).Range("A1") = "CAPA" Call CAPA
If ActiveCell.Offset(0, -22).Range("A1") = "Interruptions" Call Interruptions
If ActiveCell.Offset(0, -22).Range("A1") = "Supplier Non-Conformance" Call NCR
If ActiveCell.Offset(0, -22).Range("A1") = 'anything else "Please select a valid Quality Issue Type"
End sub

I know this is not correct but I am looking for something like this.

Thank you to anyone who can help.

What I have so far:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)



If Intersect(Target, Range("Y:Y")) Is Nothing Then Exit Sub

Select Case Target.Text

Case "Open": Call Complete_File

Case "Closed": Call Verify_Form

Case Else: Exit Sub

End Select



End Sub



Sub Complete_File()

'

' Complete_File Macro

'

Dim msg As String, ans As Variant, lr As Long, Sh As Worksheet, lo As ListObject



If Application.CountIf(Sheets("CC Database").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 20)), "") > 0 Then

MsgBox "Please Complete all manditory fields (Columns B to T)"

Range(Cells(Selection.Row, 25), Cells(Selection.Row, 25)).ClearContents

Exit Sub

End If

‘*****Add requested code here*****

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1New IdentifierOriginal IdentifierTypeDate OpenedOpened byEstimated Completion DateDivisionArea/Department/CellSourceDiscrepancy/Issue/ReasonWork Order/Job #PO#Model #QtyCorresponding NCR/CC/CAPA/Interruption #Customer/VendorPart Name / DescriptionPart Number / Document ReferenceSerial # / ProcessOwnerVerification/Action/RepairedWarranty Granted Y/N /ReprocessComments/NotesFlag DateStatusClosed DateMan HoursLabour CostMaterial CostTotal CostWork done in good faithTotal Days Active
2Q5000CC1234Customer Concern30-Oct-241st DivisionFrontErrorN/A123456N/A15555CustomerPart444444Open
Sheet1
Cells with Data Validation
CellAllowCriteria
Y2List=Sheet2!$A$1:$A$2
This will check what you have entered into column Y and call the procedure depending on what is in column C.

Call a macro based on text in column C.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1New IdentifierOriginal IdentifierTypeDate OpenedOpened byEstimated Completion DateDivisionArea/Department/CellSourceDiscrepancy/Issue/ReasonWork Order/Job #PO#Model #QtyCorresponding NCR/CC/CAPA/Interruption #Customer/VendorPart Name / DescriptionPart Number / Document ReferenceSerial # / ProcessOwnerVerification/Action/RepairedWarranty Granted Y/N /ReprocessComments/NotesFlag DateStatusClosed DateMan HoursLabour CostMaterial CostTotal CostWork done in good faithTotal Days Active
2Q5000CC1234Customer Concern30-Oct-241st DivisionFrontErrorN/A123456N/A15555CustomerPart444444Open
3Q5001CC1235Interruptions31-Oct-242nd DivisionFrontErrorN/A123457N/A15555CustomerPart444445Open
4Q5002CC1236Supplier Non-Conformance01-Nov-243rd DivisionFrontErrorN/A123458N/A15555CustomerPart444446Open
5Q5003CC1237CAPA02-Nov-244th DivisionFrontErrorN/A123459N/A15555CustomerPart444447Open
6Q5004CC1238Customer Concern03-Nov-245th DivisionFrontErrorN/A123460N/A15555CustomerPart444448Open
7Q5005CC1239Customer Concern04-Nov-246th DivisionFrontErrorN/A123461N/A15555CustomerPart444449Open
8Q5006CC1240Customer Concern05-Nov-247th DivisionFrontErrorN/A123462N/A15555CustomerPart444450Open
Sheet2
Cells with Data Validation
CellAllowCriteria
Y2:Y8ListOpen,Closed


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  If Target.CountLarge > 1 Then
    Exit Sub
  End If

  If Not Intersect(Target, Range("Y:Y")) Is Nothing Then
  
    If Target.Value <> "Open" Then
      Exit Sub
    End If

    Select Case Target.Offset(0, -22).Value
    
      Case "Customer Concern": CC

      Case "CAPA": CAPA
      
      Case "Interruptions": Interruptions
      
      Case "Supplier Non-Conformance": NCR
      
      Case Else:
        
        MsgBox "Please select a valid Quality Issue Type", vbOKOnly, "Warning!"
        
    End Select

  End If

End Sub

Public Sub CC()
  MsgBox "CC"
End Sub

Public Sub CAPA()
  MsgBox "CAPA"
End Sub

Public Sub Interruptions()
  MsgBox "Interruptions"
End Sub

Public Sub NCR()
  MsgBox "NCR"
End Sub
 
Upvote 0
I haven't tried to validate your existing code but this will add the functionality you described:
VBA Code:
         Select Case Cells(Selection.Row, "C")
        
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
           
         End Select

You don't need to use "Call", it's obsolete and included in the language for backwards compatibility to old code.
I implemented the above code but got a Compile error listed below:

error.JPG


VBA Code:
Sub Complete_File()
'
' Complete_File Macro
'
  Dim msg As String, ans As Variant, lr As Long, Sh As Worksheet, lo As ListObject
 
  If Application.CountIf(Sheets("Q Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 20)), "") > 0 Then
  MsgBox "Please Complete all manditory fields (Columns B to T)"
Range(Cells(Selection.Row, 25), Cells(Selection.Row, 25)).ClearContents
    Exit Sub
  End If

msg = "Your Quality File will be finalized," & vbCrLf & "" & vbCrLf & " Do you wish to continue?"

    ans = MsgBox(msg, vbYesNo)

    Select Case ans

         Select Case Cells(Selection.Row, "C") '****Error on this line****
         
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
            
         End Select
         
            Case vbNo
        GoTo Quit:
    End Select

Quit:
End Sub
 
Upvote 0
This will check what you have entered into column Y and call the procedure depending on what is in column C.

Call a macro based on text in column C.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1New IdentifierOriginal IdentifierTypeDate OpenedOpened byEstimated Completion DateDivisionArea/Department/CellSourceDiscrepancy/Issue/ReasonWork Order/Job #PO#Model #QtyCorresponding NCR/CC/CAPA/Interruption #Customer/VendorPart Name / DescriptionPart Number / Document ReferenceSerial # / ProcessOwnerVerification/Action/RepairedWarranty Granted Y/N /ReprocessComments/NotesFlag DateStatusClosed DateMan HoursLabour CostMaterial CostTotal CostWork done in good faithTotal Days Active
2Q5000CC1234Customer Concern30-Oct-241st DivisionFrontErrorN/A123456N/A15555CustomerPart444444Open
3Q5001CC1235Interruptions31-Oct-242nd DivisionFrontErrorN/A123457N/A15555CustomerPart444445Open
4Q5002CC1236Supplier Non-Conformance01-Nov-243rd DivisionFrontErrorN/A123458N/A15555CustomerPart444446Open
5Q5003CC1237CAPA02-Nov-244th DivisionFrontErrorN/A123459N/A15555CustomerPart444447Open
6Q5004CC1238Customer Concern03-Nov-245th DivisionFrontErrorN/A123460N/A15555CustomerPart444448Open
7Q5005CC1239Customer Concern04-Nov-246th DivisionFrontErrorN/A123461N/A15555CustomerPart444449Open
8Q5006CC1240Customer Concern05-Nov-247th DivisionFrontErrorN/A123462N/A15555CustomerPart444450Open
Sheet2
Cells with Data Validation
CellAllowCriteria
Y2:Y8ListOpen,Closed


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  If Target.CountLarge > 1 Then
    Exit Sub
  End If

  If Not Intersect(Target, Range("Y:Y")) Is Nothing Then
 
    If Target.Value <> "Open" Then
      Exit Sub
    End If

    Select Case Target.Offset(0, -22).Value
   
      Case "Customer Concern": CC

      Case "CAPA": CAPA
     
      Case "Interruptions": Interruptions
     
      Case "Supplier Non-Conformance": NCR
     
      Case Else:
       
        MsgBox "Please select a valid Quality Issue Type", vbOKOnly, "Warning!"
       
    End Select

  End If

End Sub

Public Sub CC()
  MsgBox "CC"
End Sub

Public Sub CAPA()
  MsgBox "CAPA"
End Sub

Public Sub Interruptions()
  MsgBox "Interruptions"
End Sub

Public Sub NCR()
  MsgBox "NCR"
End Sub
This works but does not account for another option in Column Y like my original. I do need another Macro called for if the cell states "Closed". I am not sure how to modify this to account for that unfortunately.
 
Upvote 0
That because you put my code in an invalid location, which is described in the error message.
Rich (BB code):
    ans = MsgBox(msg, vbYesNo)

    Select Case ans
You cannot put any code here===>
         Select Case Cells(Selection.Row, "C") 
       
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
          
         End Select
<===to here       
            Case vbNo
        GoTo Quit:
    End Select

Quit:
End Sub

Also, don't use GoTo. Ever.

I think you want this (remove ans)
VBA Code:
    If MsgBox(msg, vbYesNo) = vbYes Then

         Select Case Cells(Selection.Row, "C") '****Error on this line****
       
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
          
         End Select

   End If

End Sub
 
Upvote 0
That because you put my code in an invalid location, which is described in the error message.
Rich (BB code):
    ans = MsgBox(msg, vbYesNo)

    Select Case ans
You cannot put any code here===>
         Select Case Cells(Selection.Row, "C")
      
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
         
         End Select
<===to here      
            Case vbNo
        GoTo Quit:
    End Select

Quit:
End Sub

Also, don't use GoTo. Ever.

I think you want this (remove ans)
VBA Code:
    If MsgBox(msg, vbYesNo) = vbYes Then

         Select Case Cells(Selection.Row, "C") '****Error on this line****
      
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
         
         End Select

   End If

End Sub
I do require that code at the beginning to make sure the user has filled out Columns B to T and to verify if they wish to continue before even calling another Macro. I do not see how to accomplish this if your code is first...

VBA Code:
Sub Complete_File()
'
' Complete_File Macro
'
  Dim msg As String, ans As Variant, lr As Long, Sh As Worksheet, lo As ListObject
 
  If Application.CountIf(Sheets("Q Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 20)), "") > 0 Then
  MsgBox "Please Complete all manditory fields (Columns B to T)"
Range(Cells(Selection.Row, 25), Cells(Selection.Row, 25)).ClearContents
    Exit Sub
  End If

msg = "Your Quality File will be finalized," & vbCrLf & "" & vbCrLf & " Do you wish to continue?"

    If MsgBox(msg, vbYesNo) = vbYes Then

    Select Case ans

         Select Case Cells(Selection.Row, "C")
         
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
            
         End Select
         
End If

End Sub
 
Upvote 0
I do require that code at the beginning to make sure the user has filled out Columns B to T and to verify if they wish to continue before even calling another Macro. I do not see how to accomplish this if your code is first...

VBA Code:
Sub Complete_File()
'
' Complete_File Macro
'
  Dim msg As String, ans As Variant, lr As Long, Sh As Worksheet, lo As ListObject
 
  If Application.CountIf(Sheets("Q Log").Range(Cells(Selection.Row, 1), Cells(Selection.Row, 20)), "") > 0 Then
  MsgBox "Please Complete all manditory fields (Columns B to T)"
Range(Cells(Selection.Row, 25), Cells(Selection.Row, 25)).ClearContents
    Exit Sub
  End If

msg = "Your Quality File will be finalized," & vbCrLf & "" & vbCrLf & " Do you wish to continue?"

    If MsgBox(msg, vbYesNo) = vbYes Then

    Select Case ans

         Select Case Cells(Selection.Row, "C")
        
            Case "Customer Concern": CC
            Case "CAPA": CAPA
            Case "Interruptions": Interruptions
            Case "Supplier Non-Conformance": NCR
            Case Else: MsgBox "Please select a valid Quality Issue Type"
           
         End Select
        
End If

End Sub
Never mind. I see the line I missed removing. Thank you very much!
 
Upvote 0
I do require that code at the beginning to make sure the user has filled out Columns B to T and to verify if they wish to continue before even calling another Macro. I do not see how to accomplish this if your code is first...
I wrote it for you. See the code in my most recent post.
 
Upvote 0

Forum statistics

Threads
1,223,867
Messages
6,175,077
Members
452,611
Latest member
bls2024

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