vba vlookup and writing formula into cell

frostworks

New Member
Joined
Jan 28, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello! I have two tables here and am attempting to use vba to do two things:

1. lookup value in sheet 1 table 14 col 10, match against sheet 2 table 1 col 1, return sheet 2 table 1 col 6 value in sheet 1 table 14 col 60
2. write vlookup formula into sheet 2 table 1 col 6 to look up value in sheet 2 table 1 col 1 and return sheet 1 table 14 col 60

Can't seem to find a standardise/proper example online or maybe i'm too dense for it.. Can someone help me please?

Sheet 1 Table 14
1710383547344.png


Sheet 2 Table 1
1710383623053.png


Thanks in advance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hmm, that sounds pretty much just swapping the values into the other table then bringing them back by formula? If so, try this with a copy of your workbook.

VBA Code:
Sub Test()
  With Range("Table14[col 60]")
    .Cells(1).Formula2 = "=XLOOKUP([@[col 10]],Table1[col 1],Table1[col 6])&"""""
    .Value = .Value
  End With
  With Range("Table1[col 6]")
    .ClearContents
    .Cells(1).Formula2 = "=XLOOKUP([@[col 1]],Table14[col 10],Table14[col 60])&"""""
  End With
End Sub
 
Upvote 0
hello! it seems to work great but when i try to apply it to my real tables, it only picks up the very first row of each table. is there any possible reason why?

For some context on this request, Table 1 is a Power Query output of Table 14 merged with another table.
Table 1 Col 6 is where comments are entered by others, and in order for it to reflect properly the next time the query is run, i copy the comments from Table 1 Col 6 into Table 14 Col 60 and change Table 1 Col 6 to a lookup of Table 14 Col 60.
 
Upvote 0
What happens if you add this line?

Rich (BB code):
Sub Test2()
  With Range("Table14[col 60]")
    .ClearContents
    .Cells(1).Formula2 = "=XLOOKUP([@[col 10]],Table1[col 1],Table1[col 6])&"""""
    .Value = .Value
  End With
  With Range("Table1[col 6]")
    .ClearContents
    .Cells(1).Formula2 = "=XLOOKUP([@[col 1]],Table14[col 10],Table14[col 60])&"""""
  End With
End Sub
 
Upvote 0
i don't think i would want to clear the contents of table14 col 60 from the start, as table1 col 6 still looks up that.
i believe i will run into an issue whereby line 3 of table14 col60 is populated, clearing contents will result in table1 col6 returning a "" instead of the entry as table1 col6 starts with a lookup.

Am i making sense here?
 
Upvote 0
i don't think i would want to clear the contents of table14 col 60 from the start, as table1 col 6 still looks up that.
:confused: In your image table14 col 60 is blank and table1 col 6 has values so how could table1 col 6 be looking up values from table14 col 60?
 
Upvote 0
therefore the vba is to
1. look up and copy from table 1 col 6 to table 14 col 60, change to values only in table 14 col 60
2. clear table 1 col 6 and replace with a vlookup to table 14 col 60
 
Upvote 0
i'm reading your code as

VBA Code:
Sub Test2()

 'looking up and copying from table1 col6 to table14 col60
  With Range("Table14[col 60]")
    .Cells(1).Formula2 = "=XLOOKUP([@[col 10]],Table1[col 1],Table1[col 6])&"""""
 'keep as value
    .Value = .Value
  End With
  With Range("Table1[col 6]")
    .ClearContents
 'looking up values in table140 col60 and showing it in table1 col6
    .Cells(1).Formula2 = "=XLOOKUP([@[col 1]],Table14[col 10],Table14[col 60])&"""""
  End With
End Sub
 
Last edited by a moderator:
Upvote 0
  1. That doesn't explain how your Table1 has values when Table14 is blank.

  2. Perhaps you could give us the two sample tables with XL2BB as they are "before" the requested change and also the two tables as they are "after" the requested change with any values and/or formulas manually entered as you want them to finish? That way we can be more sure of our testing.

  3. When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

  4. I think that your reading of my code is correct. I must be missing something because as far as I can tell your code comments match your request.
1. lookup value in sheet 1 table 14 col 10, match against sheet 2 table 1 col 1, return sheet 2 table 1 col 6 value in sheet 1 table 14 col 60
2. write vlookup formula into sheet 2 table 1 col 6 to look up value in sheet 2 table 1 col 1 and return sheet 1 table 14 col 60
 
Upvote 0
Appreciate the help Peter!

1. Table 1 has values because there will be someone entering the comments. i need them to be transferred to Table 14 because i want it output again in Table 1 via Table 14, updated with whatever new comments/input in Table 1.

2. Unfortunately can't do that as my work laptop has high restrictions...

3. Noted on that, thanks for this time.

4. i did a workaround in the end using excel formula to create a dummy column reading results from col 6 and col 60, and having VBA to copy values from the dummy column into col 60. However i'm keen to know more about your code, what's the '&""""" ' portion for?
i think your code isn't working because some cells in the column has values and some doesn't. i think the formula cannot be replicated in the entire table if there are filled cells, so only row 1 works.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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