Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90280

How to create a VBA code that references a value in another workbook and then fills in a column in the current workbook based off of that value?

$
0
0

I have two workbooks. One is the original that I am referencing from and the second is the new macro enabled book that is taking the information from the original workbook, with added additional columns.

The original workbook which is an xlsx book that I am referencing from looks like this from columns A to G

Level Code ID Location Country Type Last 
  0     0   1  London  England  Yes  ?
  0     0   1  London  England   No  ?
  0     0   2  Mumbai  India    Yes  ?
  0     0   2  Mumbai  India    No   ?
  0     0   3  Toronto Canada   Yes  ?
  0     0   4  Malibu  USA      No   ?

My macrobook that contains the VBA code looks like this from columns A to L, and contains the same information from original workbook with new added columns.

Record Year Level Code ID Location Country Type Yes Type No       A       Platform Last
1000   2019  0     0   1  London   England                    Available      X       ?
1000   2019  0     0   2  Mumbai   India                      Available      X       ? 
1000   2019  0     0   3  Toronto  Canada                     Available      X       ?
1000   2019  0     0   4  Malibu   USA                        Available      X       ?

I am trying to reduce the number of rows from the original file in the new file by bringing in the different Types: "Yes" and "No" as their own headers in the new file. What I want to do is count the number of times yes and no appears for each ID, and then record that under "Type Yes" and "Type No" in the macrobook. For example, if ID 1 has both a Yes and No in the original workbook, then I would want to vlookup that ID in the macrobook, and insert "True" under both Type Yes and Type No. I figured If there is a both a yes and no, we can find that out by counting if there are 2 duplicates of the ID. If the ID does not repeat and the Type value = "Yes" and not "No" in the original workbook, then I want to vlookup that ID in the new file and write True in the "Type Yes" column and False in the "Type No" column. This also applies vice versa. The output needs to look like this.

Record Year Level Code ID Location Country Type Yes Type No       A         Platform Last
1000   2019  0     0   1  London   England    True  True       Available      X       ?
1000   2019  0     0   2  Mumbai   India      True  True       Available      X       ? 
1000   2019  0     0   3  Toronto  Canada     True   False     Available      X       ?
1000   2019  0     0   4  Malibu   USA        False  True     Available       X       ?

My code looks like this but I'm kind of stuck. Any suggestions? Thanks.

Sub ValueType()

Dim cell As Range

    Dim lookFor As Range
    Dim srchRange As Range

    Dim i As Long
    Dim lastrow As Long

    Dim book1 As Workbook
    Dim book2 As Workbook

    Dim book2Name As String
    book2Name = "original.xlsx"'modify it as per your requirement

    Dim book2NamePath As String
    book2NamePath = ThisWorkbook.Path & "\"& book2Name

    Set book1 = ThisWorkbook

    If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)
    Set book2 = Workbooks(book2Name)

For i = 2 To lastrow

If CountIf(book2.Sheets(1).Cells(i, 3).Value) = 2 Then
VLookup(book2.Sheets(1).Cells(i, 3).Value, book1.Sheets(1).Cells(i,5).Value, 2, False)


End Sub


Viewing all articles
Browse latest Browse all 90280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>