Adjust code to account for variable entry

Drawleeh

New Member
Joined
Sep 2, 2021
Messages
34
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hello, I have code that essentially will fill the address of a row when something is filled within column C, D and E. However currently it only works when either: something is copied into all 3 column cells adjacent to each other at the same time i.e information is copied into C3 D3 and E3 OR when information is entered into E3 then entered into D3 and then into C3.

1638968942226.png


The issue is, is that it doesn't produce the row address when those 3 adjacent fields are filled in any other way. Is there any way to adjust the following code to account for when there is something in all 3 cells of the 3 columns regardless of how that information is entered in which order?

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

    On Error GoTo ClearError
    
    Const fRow As Long = 2
    Const sCol As String = "C"
    Const bCols As String = "C:E"
    Const dCol As String = "A"
     
    Dim crg As Range
    Set crg = Columns(sCol).Resize(Rows.Count - fRow + 1).Offset(fRow - 1)
    Debug.Print "crg: " & crg.Address(0, 0)
    Dim irg As Range: Set irg = Intersect(crg, Target)
    If irg Is Nothing Then Exit Sub

    Dim brg As Range: Set brg = Columns(bCols)
    Dim srg As Range: Set srg = Intersect(irg.EntireRow, brg)
    
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    Dim arg As Range
    Dim rrg As Range
    Dim RowString As String
    
    For Each arg In srg.Areas
        For Each rrg In arg.Rows
            
            If Application.CountBlank(rrg) = 0 Then
                RowString = CStr(rrg.Row)
                RowString = "'" & RowString & ":" & RowString
                rrg.EntireRow.Columns(dCol).Value = RowString
            End If
        Next rrg
    Next arg

SafeExit:
     
    If Not Application.EnableEvents Then
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
    
    Exit Sub

ClearError:
    Debug.Print "Run-time error '" & Err.Number & "':" & Err.Description
    Resume SafeExit
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this

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

    Dim crg As Range
    Dim irg As Range
    Dim brg As Range
    Dim srg As Range
    
    Const fRow As Long = 2
    Const sCol As String = "C"
    Const bCols As String = "C:E"
    Const dCol As String = "A"
    
     
    'Set crg = Columns(sCol).Resize(Rows.Count - fRow + 1).Offset(fRow - 1)
    Set crg = Range(Range(sCol & fRow), Cells(Cells.Rows.Count, 5))
    'Debug.Print "crg: " & crg.Address(0, 0)
    Set irg = Intersect(crg, Target)
    If irg Is Nothing Then Exit Sub
    Set srg = Intersect(Target.EntireRow, crg)
    
    If Application.CountA(srg) = 3 Then
      Application.EnableEvents = False
      Set brg = Intersect(Columns(dCol), Target.EntireRow)
      brg.NumberFormat = "@"
      brg.Value = Target.EntireRow.Address(0, 0)
      Application.EnableEvents = True
    End If
    
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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