Solved

Find value in one column and replace value in another

Posted on 2016-08-31
6
42 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

10 Experts available now in Live!

Get 1:1 Help Now