Insert formula In Last Used Column = VBA

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
287
Office Version
  1. 2013
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 a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

baitmaster

Well-known Member
Joined
Mar 12, 2009
Messages
2,039
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
287
Office Version
  1. 2013
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
287
Office Version
  1. 2013
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
36,978
Office Version
  1. 2016
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).
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,455
Members
417,025
Latest member
MusterDuster

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