Solved

Find value in one column and replace value in another

Posted on 2016-08-31
6
46 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 28

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 28

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

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 28

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now