Auto adjusting range in VBA

walt96

New Member
Joined
Jun 30, 2017
Messages
1
Hi Everyone,

I am trying to create a code that adjusts to a changing range. In the second to last column I am trying to sort data (A to Z) that changes every time. In the last column I am creating group numbers based on the second to last column. The amount of rows and columns can change based on the data. Does anyone know what code I should use to adjust for dynamic rows and columns?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi walt96,

Welcome to the Forum.

You have not given us very much to work with. That said, this is a very generic code that will return the Last Row with data in Column C and the Last Column with data in Row 4 on the active worksheet. There are many other ways to accomplish this, you may want to try a Google search to find other methods...

Code:
Sub test()


    Dim LastRow As Long
    Dim LastColumn As Long


    LastRow = Cells(Rows.Count, "C").End(xlUp).Row
    LastColumn = Cells(4, Columns.Count).End(xlToLeft).Column
    
    MsgBox "The last row with data in Column C is: " & LastRow & _
    vbNewLine & "The last column with data in Row 4 is: " & LastColumn


End Sub

I hope this helps.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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