VBA / Using Last column in formula

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello Dear community,

I can use VBA to find last column index number. But I want to use it to paste formula to cell A1.
For example, instead of B2:D2 I want VBA to paste formula to A1 with last nonblank column name.
If last column is: 6 / I want formula to be B2:F2

Can anyone help me please?
Thanks in advance.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try:

VBA Code:
Sub test()
  Dim lc As Long
  Dim rng As Range
  
  'use row 1 to find last column
  lc = Cells(1, Columns.count).End(xlToLeft).Column
  Set rng = Range("B2", Cells(lc, 2))
  
  'In the rng object you have the range of cells to use in your formula:
  MsgBox "Range: " & rng.Address
  
End Sub
 
Upvote 0
Solution
Try:

VBA Code:
Sub test()
  Dim lc As Long
  Dim rng As Range
 
  'use row 1 to find last column
  lc = Cells(1, Columns.count).End(xlToLeft).Column
  Set rng = Range("B2", Cells(lc, 2))
 
  'In the rng object you have the range of cells to use in your formula:
  MsgBox "Range: " & rng.Address
 
End Sub
Thank you for your time. 1 question.
I am trying to copy paste cell E3's adress. ( currently E3 = A1:D33 )
How can I copy paste that range in VBA? (A1:D33)
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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