Solved

Find value in one column and replace value in another

Posted on 2016-08-31
6
50 Views
Last Modified: 2016-08-31
Hi Experts

I am having trouble finding a solution to my task so I am once again hoping that I might get some assistance here.  
I require a VBA script to find the values listed in Column D and replace whatever is currently in Column F with the value "30-03-AM".  
The sheet name is constant "COMBINATION-DLY" and the columns that the data will appear in is always the same.  What changes
from day to day is the row that the data appears.  It varies from day to day based on the total number of records.  Also there is a chance that a value may NOT be there every day.  

Any help would be greatly appreciated.  

Andy

Column D      Column F
252881            30-03-AM
252889            30-03-AM
252890            30-03-AM
207554            30-03-AM
233230            30-03-AM
252844            30-03-AM
253279            30-03-AM
257168            30-03-AM
261858            30-03-AM
0
Comment
Question by:spudmcc
  • 4
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41778438
Please try this....
Sub ReplaceValues()
Dim lr As Long
Dim rng As Range
lr = Cells(Rows.Count, 4).End(xlUp).Row
ActiveSheet.AutoFilterMode = 0
With Range("D1:D" & lr)
   .AutoFilter field:=1, Criteria1:="<>"
   On Error Resume Next
   Set rng = Range("F2:F" & lr).SpecialCells(xlCellTypeVisible)
   If Not rng Is Nothing Then rng.Value = "30-03-AM"
End With
ActiveSheet.AutoFilterMode = 0
End Sub

Open in new window

0
 

Author Comment

by:spudmcc
ID: 41778464
Not sure if I it was clear that there are other values in both columns.  I can't do a global type find and replace.  It must be where the specific value in column D (one of those listed) is found and then the replacement can be done.  Column D might contains thousands of other values.  I am sorry I wasn't more clear.  

A
0
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41778470
Where are those criteria for col. D placed in the workbook, on another Sheet?
Or you want to use them as hard coded values in the code?

Can you upload a sample workbook with some dummy data to work with?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 29

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41778548
If you list your criteria for column D on a separate sheet in column A (e.g. in column A on Sheet2 in the attached), then you may use the following code to get the desired output.
Please change the sheet names used in the code as per your requirement.
Sub ReplaceValues()
Dim lr As Long, dlr As Long, i As Long
Dim rng As Range
Dim arrFilters() As String
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 4).End(xlUp).Row
With Worksheets("Sheet2")
   dlr = .Cells(Rows.Count, 1).End(xlUp).Row
   ReDim arrFilters(1 To dlr)
   For i = 1 To dlr
      arrFilters(i) = CStr(.Range("A" & i).Value)
   Next i
End With
ActiveSheet.AutoFilterMode = 0
With Worksheets("Sheet1").Range("A1").CurrentRegion
   .AutoFilter field:=4, Criteria1:=arrFilters, Operator:=7
   On Error Resume Next
   Set rng = Range("F2:F" & lr).SpecialCells(xlCellTypeVisible)
   If Not rng Is Nothing Then rng.Value = "30-03-AM"
End With
ActiveSheet.AutoFilterMode = 0
Application.ScreenUpdating = True
End Sub

Open in new window

Click the button "Replace Values" on Sheet1 to run the code.
Replace-Values.xlsm
0
 

Author Closing Comment

by:spudmcc
ID: 41778629
You are fantastic.  This is exactly what I needed to complete this project.  Thank you for your prompt response and sharing your knowledge.  

Andy
0
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41778633
You're welcome Andy! Glad to help.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question