Look for last column that has a specific value

alisoncleverly

New Member
Joined
Feb 20, 2020
Messages
28
Office Version
  1. 2013
Platform
  1. Windows
Hi everyone,

I tried the following code to do: If any Sales cells have "Title Transfer" then the 51st column would have an "x".

I have 8 columns named "Sales" in total. The 5st column is named "Title Transfer".

VBA Code:
Option Explicit
Public Const colTTransfer As Long = 51

Private Sub Worksheet_Change(ByVal Target As Range)

Dim lastColumn As Long
Dim counter As Long

lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column

    If Me.Cells(1, Target.Column).Value = "Sales" Then

        For counter = 1 To lastColumn

            If Me.Cells(Target.Row, counter).Value = "Title Transfer" Then

            Me.Cells(Target.Row, colTTransfer).Value = "x"

            End If

        Next counter

    End If

End Sub

However, I realised there was more to my original purpose and the above codes weren't flexible.
I'm wondering if there is a way to only return "x" for the last Sales column that has "Title Transfer"?

For example, assuming these events happen in the same row where:

1st Sales column has Green and 51st column remain blank
2nd Sales column has Title Transfer and 51st column has x
3rd Sales column has Rollup and 51st column turns blank
4th Sales column has Red and 51st column remains the same
5th Sales column has Title Transfer and 51st column now has x and so on

FYI:
1st Sales column is column N
2nd Sales column is column R
3rd Sales column is column V
4th Sales column is column Z
5th Sales column is column AD
6th Sales column is column AH
7th Sales column is column AL
8th Sales column is column AP

Really sorry that I couldn't include a sample here since my work laptop doesn't allow me to download any add-in, including XL2BB.

Please advise how I can make it work that way. Any help is highly appreciated! Thanks a lot!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Range, r1 As Range, lastColumn As Long, counter As Long
  Dim MaxCol As Variant, rg As Range, j As Long
   
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(Target.Column).Resize(, 3))
    Call DoCells(r)
  End If
  
  ' Get last column based on first row
  lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column
  If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then
    For counter = 1 To lastColumn
      If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") And IsEmpty(Me.Cells(Target.Row, counter).Value) Then
        Me.Cells(Target.Row, counter).Value = "Rollup"
      End If
    Next counter
  End If
   
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    If Target.CountLarge > 1 Then Exit Sub
    Set rg = Range("N" & Target.Row & ":AP" & Target.Row)
    MaxCol = 0
    For j = Columns("AP").Column To Columns("N").Column Step -4
      If Cells(Target.Row, j) <> "" Then
        If j > MaxCol Then MaxCol = j
      End If
    Next
    If MaxCol Mod 4 = 2 Then
      If Cells(Target.Row, MaxCol).Value = "Title Transfer" Then
        Cells(Target.Row, 51).Value = "x"
      Else
        Cells(Target.Row, 51).Value = ""
      End If
    End If
  End If
End Sub
 
Upvote 0
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Range, r1 As Range, lastColumn As Long, counter As Long
  Dim MaxCol As Variant, rg As Range, j As Long
  
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(Target.Column).Resize(, 3))
    Call DoCells(r)
  End If
 
  ' Get last column based on first row
  lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column
  If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then
    For counter = 1 To lastColumn
      If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") And IsEmpty(Me.Cells(Target.Row, counter).Value) Then
        Me.Cells(Target.Row, counter).Value = "Rollup"
      End If
    Next counter
  End If
  
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    If Target.CountLarge > 1 Then Exit Sub
    Set rg = Range("N" & Target.Row & ":AP" & Target.Row)
    MaxCol = 0
    For j = Columns("AP").Column To Columns("N").Column Step -4
      If Cells(Target.Row, j) <> "" Then
        If j > MaxCol Then MaxCol = j
      End If
    Next
    If MaxCol Mod 4 = 2 Then
      If Cells(Target.Row, MaxCol).Value = "Title Transfer" Then
        Cells(Target.Row, 51).Value = "x"
      Else
        Cells(Target.Row, 51).Value = ""
      End If
    End If
  End If
End Sub

Good morning Dante, thank you so much for your tremendous help so far. The codes work perfectly now!!!! Thanks a lot!!!!!!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Range, r1 As Range, lastColumn As Long, counter As Long
  Dim MaxCol As Variant, rg As Range, j As Long
 
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(Target.Column).Resize(, 3))
    Call DoCells(r)
  End If

  ' Get last column based on first row
  lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column
  If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then
    For counter = 1 To lastColumn
      If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") And IsEmpty(Me.Cells(Target.Row, counter).Value) Then
        Me.Cells(Target.Row, counter).Value = "Rollup"
      End If
    Next counter
  End If
 
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    If Target.CountLarge > 1 Then Exit Sub
    Set rg = Range("N" & Target.Row & ":AP" & Target.Row)
    MaxCol = 0
    For j = Columns("AP").Column To Columns("N").Column Step -4
      If Cells(Target.Row, j) <> "" Then
        If j > MaxCol Then MaxCol = j
      End If
    Next
    If MaxCol Mod 4 = 2 Then
      If Cells(Target.Row, MaxCol).Value = "Title Transfer" Then
        Cells(Target.Row, 51).Value = "x"
      Else
        Cells(Target.Row, 51).Value = ""
      End If
    End If
  End If
End Sub
Hi Dante, I came back hoping you can help me out with another favour.

Is there a way to incorporate the following conditions to the codes you wrote?

Currently I have this bit of codes (can be seen from the codes you provided)
VBA Code:
'Shipped without Title Transfer
  If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then
    For counter = 1 To lastColumn
      If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") And IsEmpty(Me.Cells(Target.Row, counter).Value) Then
        Me.Cells(Target.Row, counter).Value = "Rollup"
      End If
    Next counter
  End If

What it does is to automatically return "Rollup" for all empty Sales and Production cells if "Shipped" in Column AV (48th column), however, I found it not the most effective way to go around.

I'm also thinking of adding another condition to that bit of codes which is:

If in Day n, a cell in Column AY (51st column) has "x", and if in Day (n+1), that cell has "Shipped" in column AV (48th column), then all Sales and Production cells from Day(n+1) onwards will become "Shipped".

See below print screen of the desired output
Capture1.PNG


I'm also having an issue where if I put "x" in column AY first, then later on put "Shipped" in column AV, the "x" is wiped out and is replaced by the condition written by the codes mentioned above (If "Shipped" in AV then "Rollup" for all empty Sales and Production cells).

There'd never be a case where "Shipped" in column AV first then "x" in column AY later. It has to be the other way around which is "x" in column AY first then can be "Shipped" in column AV later.
Capture.PNG


Now thinking back, what I wrote contradicts what you provided, hmmm...

Just a quick note:
  1. I have 8 Days in total, each Day has Sales, Production, Day #, Status columns
  2. 1st Sales column is column N
  3. 1st Production column is column O
  4. All "Status" columns have formula in them
Can you please kindly advise? I really really appreciate your help so far! Thank you thank you thank you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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