Searching for values in a column

MazExpress

Board Regular
Joined
Aug 5, 2020
Messages
56
Office Version
  1. 2010
Platform
  1. Windows
I have a monthly sheet (Picture 1) that has 2 columns of concern, F & G. How to copy the values in column G to another workbook (Picture 2) in the column of the current month?
Knowing that:
1- The order of names in the first sheet is different from month to another.
2- Sometimes there are no names available in column F, so the corresponding values are useless.
3- Some names and values maybe unwanted (like P in Picture 1). In other words, I want to only copy the values in column G which have names in the second sheet.

Any help would be much appreciated.
 

Attachments

  • Capture15-81.JPG
    Capture15-81.JPG
    32.4 KB · Views: 3
  • Capture15-82.JPG
    Capture15-82.JPG
    39.4 KB · Views: 4

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Book1
ABCDEFG
1Agent NameValueAgent NameAugust
2X55X5555
3Y62Y6262
4Z25Z2525
5A17A1717
620C5858
7C58F6464
8F64
9P80Using Index and MatchUsing Vlookup
10
11
12
13
Sheet4
Cell Formulas
RangeFormula
E2:E7E2=INDEX($B$2:$B$9,MATCH(D2,$A$2:$A$9,0))
F2:F7F2=VLOOKUP(D2,A:B,2,0)
 
Upvote 0
I'm so sorry. I forgot to mention that I want to build a vba code for that task to be automated.
 
Upvote 0
Place the following macro in a regular module in your monthly sheet workbook. Change the name of your destination workbook (in red) and the sheet names (in blue) to suit your needs.
Rich (BB code):
Sub CopyValues()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object, WS1 As Worksheet, WS2 As Worksheet, Val As String, arr1 As Variant, arr2 As Variant, i As Long, fnd As Range
    Set WS1 = ThisWorkbook.Sheets("Sheet1")
    Set WS2 = Workbooks("DestinationWorkBook.xlsx").Sheets("Sheet1")
    Set fnd = WS2.Rows(1).Find(MonthName(Month(Date)))
    If Not fnd Is Nothing Then
        arr2 = WS2.Range("A2", WS2.Range("A" & WS2.Rows.Count).End(xlUp))
        arr1 = WS1.Range("F2", WS1.Range("F" & WS1.Rows.Count).End(xlUp)).Resize(, 2).Value
        Set RngList = CreateObject("Scripting.Dictionary")
        For i = LBound(arr1) To UBound(arr1)
            Val = arr1(i, 1)
            If Val <> "" Then
                RngList.Add Key:=Val, Item:=arr1(i, 2)
            End If
        Next i
        For i = LBound(arr2) To UBound(arr2)
            Val = arr2(i, 1)
            If RngList.exists(Val) Then
                WS2.Cells(i + 1, fnd.Column) = RngList(Val)
            End If
        Next i
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks a lot for your effort. I'll try it and tell you the result.
 
Upvote 0
After changing the names of my destination workbook and sheet, I got the message "Run-time error 9 : Subscript out of range" !!
 
Upvote 0
Are both workbooks open? When you click "Debug" which line of code is highlighted in yellow?
 
Upvote 0
Yes, both are open. I've provided more images which may explain my case more clearly.
I want to copy the monthly net profits (in Picture 2) to the corresponding month (in Picture 1).
 

Attachments

  • Capture16-82.JPG
    Capture16-82.JPG
    30.2 KB · Views: 3
  • Capture16-81.JPG
    Capture16-81.JPG
    73.9 KB · Views: 4
  • Capture16-83.JPG
    Capture16-83.JPG
    69.1 KB · Views: 4
Last edited:
Upvote 0
Try this revised version. In your samples in your original post, you use the full month name (August). In your last post, you use the abbreviation (Aug). I have modifed the macro to account for this (in blue). Also, for the destination workbook name you have entered the full path but it should be only the name as in the macro below (in red):
Rich (BB code):
Sub CopyValues()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object, WS1 As Worksheet, WS2 As Worksheet, Val As String, arr1 As Variant, arr2 As Variant, i As Long, fnd As Range
    Set WS1 = ThisWorkbook.Sheets("Sheet1")
    Set WS2 = Workbooks("Actual.xlsx").Sheets("Sheet1")
    Set fnd = WS2.Rows(1).Find(Left(MonthName(Month(Date)), 3))
    If Not fnd Is Nothing Then
        arr2 = WS2.Range("A2", WS2.Range("A" & WS2.Rows.Count).End(xlUp))
        arr1 = WS1.Range("F2", WS1.Range("F" & WS1.Rows.Count).End(xlUp)).Resize(, 2).Value
        Set RngList = CreateObject("Scripting.Dictionary")
        For i = LBound(arr1) To UBound(arr1)
            Val = arr1(i, 1)
            If Val <> "" Then
                RngList.Add Key:=Val, Item:=arr1(i, 2)
            End If
        Next i
        For i = LBound(arr2) To UBound(arr2)
            Val = arr2(i, 1)
            If RngList.exists(Val) Then
                WS2.Cells(i + 1, fnd.Column) = RngList(Val)
            End If
        Next i
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
When I ran the revised macro, nothing happened. Everything stays as it is. No changes , no bugs.
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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