Search and Replace with Dynamic Columns (VBA)

CakeisaDie

New Member
Joined
May 23, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I run a report every week and have some Manual Adjustments I need to make frequently that also change frequently. I'm looking to automate this but I really don't understand VBA too well. There are also a bunch more rows being requested now so using a simple if formula is getting too heavy on my computer.

I found this solution which works for changing things in the same column
and this solution which does an offset but has a fixed output

I am imagining that mixing these 2 solutions and making some sort of reference would do what I need it to do but I'm struggling to make them work.

I'm also looking to copy paste the original value into the last column in this case I.

Data Used for Pivot Table A:E + I is what I'm looking for as a result of the helper cells.

1684881278976.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi CakeisaDie, your post isn't very clear on what you want to automate.
I would suggest that

1. you post example data with which you start

2. then explain what this manual adjustment is that you want to automate:
  • Which data does it look at,
  • what is the formula to make the adjustment
3. Then show in a next screenshot the result

4. Lastly a screenshot of how you want to present the data finally.
 
Upvote 0
I'm not sure how to explain it easier but let me try.
Start with this data.

VBA Test.xlsm
ABCDEFGHI
4YearSalespersonItemTrans DateInvc DtManual Adjustment IdentifiedWhich Column to Change (Offset?)New ValueOriginal Value
52023DSCake5/18/2023 05/18/23Manual Adjustment512/31/2023
62023DSCake5/18/2023 05/18/23
72023DSCake5/18/2023 05/18/23
82023DSCake5/18/2023 05/18/23Manual Adjustment3Cookies
92023DSCake5/18/2023 05/18/23
102023DSCake5/18/2023 05/18/23
112023DSCake5/18/2023 05/18/23Manual Adjustment1Lillian
122023DSCake5/18/2023 05/18/23
Sheet1 (2)


With the end result looking like this (where the colored cells are what was changed)

VBA Test.xlsm
ABCDEFGHI
1DATA used for Pivot TableHelper Cells
2
3Row Remains the Manual Adjustment Field
4YearSalespersonItemTrans DateInvc DtManual Adjustment IdentifiedWhich Column to Change (Offset?)New ValueOriginal Value
52023DSCake5/18/2023 12/31/23Manual Adjustment512/31/20235/18/2023
62023DSCake5/18/2023 05/18/23
72023DSCake5/18/2023 05/18/23
82023DSCookies5/18/2023 05/18/23Manual Adjustment3CookiesCake
92023DSCake5/18/2023 05/18/23
102023DSCake5/18/2023 05/18/23
11LillianDSCake5/18/2023 05/18/23Manual Adjustment1LillianLillian
122023DSCake5/18/2023 05/18/23
Sheet1 (2)


My inclination is that I could use one of the solutions in the links, if I could make the fixed offset of "-42" and Fixed "To-Be-Deleted" to refer to cells rather than just the fixed value or Make the AN AP G AN AP's reference instead of a fixed column.

What I've been doing has been having multiple helper cells and using formulas to create a new column for each change and then overwriting the change. I think Power Query can do this, but I think VBA is more likely to be cleaner and Formulas are getting too heavy now.

VBA Code:
Sub SearchReplace()
Dim i As Long
Dim Fnd As Range

Set Fnd = Range("AQ1")
For i = 1 To Application.CountIf(Range("AQ:AQ"), "Cancelled")
Set Fnd = Range("AQ:AQ").Find(Range("Cancelled").Value, Fnd, , xlWhole, , , False, , False)
Fnd.Offset(, -42).Value = "To-Be-Deleted"
Next i
End Sub


VBA Code:
Sub Picture2_Click()

  Dim i As Long
 
  For i = 4 To Range("E" & Rows.Count).End(3).Row
    If Range("AN" & i).Value <> "" And Range("AP" & i).Value <> "" Then
      Range("G" & i & ":AN" & i).Replace Range("AN" & i).Value, Range("AP" & i).Value, xlPart
    End If
  Next
End Sub
 
Upvote 0
sorry for the delay, busy with holls and other things...

Put this code in the code module for the sheet:
  1. Right click on the sheet name tab
  2. select 'View code'
  3. the VBA editor will open with the module opened
  4. paste the following code in the module
How it works:
once the user has entered a value in say G3 and H3, the macro will put the value from the appointed cell in I3
then it will put the value from H3 into the appointed cell
then it will add the 'Manually adjusted' label in F3

The macro will check if the column number in G3 is a valid number

It doesn't matter in which order Gx and Hx are filled out.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim vPrev As Variant, vNew As Variant, vOff As Variant
    Dim iTC As Integer, bErr As Boolean
    
    If Not Intersect(Target, Range("G:H")) Is Nothing Then
        If Target.Cells.Count = 1 Then
            'check which column has been changed, and set variables accordingly
            Select Case Target.Column
                Case 7  '"G"
                    vOff = Target
                    vNew = Target.Offset(0, 1)
                    iTC = 0
                Case 8  '"H"
                    vOff = Target.Offset(0, -1)
                    vNew = Target
                    iTC = -1
            End Select
            'check if both columns have been filled, else do nothing
            If Len(vOff) = 0 Or Len(vNew) = 0 Then Exit Sub
            'check if offset column contains a number <=5
            If (Not IsNumeric(vOff)) Or vOff > 5 Then
                MsgBox "This field can only contain a numeric offset (1 - 5)", _
                        Title:="Error " & Target.Offset(0, iTC).Address
                Target.Offset(0, iTC).Select
                Exit Sub
            End If
                
            'Everything is OK, so do the changes
            With Target.Offset(0, iTC)
                .Offset(0, 2) = .Offset(0, vOff - 7)  'put old value in column I
                .Offset(0, vOff - 7) = .Offset(0, 1)  'put new value in required column
                .Offset(0, -1) = "Manual adjustment"    'add lable that row has been adjusted
            End With
                
        End If
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
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