Insert formula In Last Used Column = VBA

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
330
Office Version
  1. 365
Platform
  1. Windows
Is there any way to create a <acronym title="visual basic for applications">VBA</acronym> that will insert a VLOOKUP statement in cell #2 of in the last used column used in Sheet 1. I would run the VBA several times a week, but the last column will always be different.

The VLookup that I would like to use is:

=VLOOKUP($H:$H,Sheet2!$D:$O,12,FALSE)

Thank you.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
A function to return the ACTUAL last used column (not the last column of the "used range", which can be completely different) is
Code:
Function lastUsedColumn(ws As Worksheet) As Long

On Error Resume Next
    lastUsedColumn = ws.Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
On Error GoTo 0

End Function

with this you could use
Code:
Sub writeVL()
Sheet1.Cells(2, lastUsedColumn(Sheet1)).Formula = "=VLOOKUP($H:$H,Sheet2!$D:$O,12,FALSE)"
End Sub
 
Upvote 0
A function to return the ACTUAL last used column (not the last column of the "used range", which can be completely different) is
Code:
Function lastUsedColumn(ws As Worksheet) As Long

On Error Resume Next
    lastUsedColumn = ws.Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
On Error GoTo 0

End Function

with this you could use
Code:
Sub writeVL()
Sheet1.Cells(2, lastUsedColumn(Sheet1)).Formula = "=VLOOKUP($H:$H,Sheet2!$D:$O,12,FALSE)"
End Sub

Thank you for this. Do I need to put both codes into VBA or only the 2nd one? I am not sure what the first one does.
 
Upvote 0
I added both of the codes but it does not work. Neither are showing in my Macro list to run after adding the code in VBA.
The function won't show there (it is not a macro), but the Sub writeVL should show there. Where did you install the code at? You should have put it in a General Module (Insert/Module from the VB editor's menu bar).
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

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