Insert formula In Last Used Column = VBA

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
318
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

baitmaster

Well-known Member
Joined
Mar 12, 2009
Messages
2,042
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
 

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
318
Office Version
  1. 365
Platform
  1. Windows
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.
 

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
318
Office Version
  1. 365
Platform
  1. Windows
Put both. Run the Sub only, the function is called from the sub

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.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,893
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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).
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,530
Messages
5,837,910
Members
430,520
Latest member
VenkateshRajaganesan

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
Top