Replace Cell Refs with variable names

Status
Not open for further replies.

Deborah Holt

New Member
Joined
Jul 2, 2018
Messages
4


Hi all,

I've composed this macro which does what I need; (finds last used column in row4 and drags the contents 1 column to the right). In this case, last used column= BA

Sub newmonth_book()
Sheets("Book").Select
Range(Cells(4, 53), Cells(8, 53)).AutoFill Destination:=Range(Cells(4, 53),Cells(8, 54)), Type:=xlFillDefault
End Sub

However, I want to replace the values of 53 with a variable name as the lastused column is different every time.
I can establish the last used column using this;

Sub BOOK()
'Find the last used column in a Row: row 4 in this example
Sheets("Book").Select
Dim LastCol As Long
With ActiveSheet
LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
End With

Dim NextCol As Long
NextCol = LastCol + 1

MsgBox (NextCol)
End Sub

However,when I replace the 'hard-coded' values with names, I can't get it towork and I get this message "Application-defined or object-definederror"
This is my code

Sub newmonth_book()
Sheets("Book").Select
Range(Cells(4, LastCol), Cells(8, LastCol)).AutoFillDestination:=Range(Cells(4, LastCol), Cells(8, NextCol)), Type:=xlFillDefault
End Sub

Can anyone advise how I get this to work?



Thanks in advance
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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