correct macro autofill code for dynamic ranges

reader6886

New Member
Joined
Sep 10, 2020
Messages
22
Office Version
  1. 2019
Platform
  1. Windows
Hello all beginner here.

How do I change the following vb codes so that the macros can be used for sheets with dynamic ranges.

1 Dynamic autofill ( even though autofill was done with autofill dot ) macro reording produced the code with absolute range(G2:G799). How do i make it dynamic.So autofill on other file dont miss any cells or overshoot !

VBA Code:
 Range("G2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-2],65)"
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G799")
    Range("G2:G799").Select


2.how do I edit the table range to make it dynamic . Even though using the ctrl+**** + right/ left arrow produced the absolute range($A$1:$F$799) like below.
VBA Code:
Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$799"), , xlYes).name = _
        "Table1"
    Range("Table1[#All]").Select

3. what do I use instead of the following code/range(change G2:G1007) so that when used with files with more or less row/columns the formula does not produce unnecessary characters click here in rows with no hyperlinks .
VBA Code:
Range("G2").Select
    ActiveCell.FormulaR1C1 = "=HYPERLINK(RC[-3],""click here"")"
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G1007")
    Range("G2:G1007").Select


thanks in advance for tips.
 

reader6886

New Member
Joined
Sep 10, 2020
Messages
22
Office Version
  1. 2019
Platform
  1. Windows
How about
VBA Code:
Range("A1").CurrentRegion.RemoveDuplicates Columns:=4, Header:=xlYes
and
VBA Code:
   With ActiveWorkbook.WorkSheets("best movies ever").Range("A1").CurrentRegion
         .Sort .Parent.Range("E1"), xlAscending, , , , , , xlNo
   End With
THANK YOU !!!! works great.
You have taught me how to future proof my macros and make them 'universal' today!
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Watch MrExcel Video

Forum statistics

Threads
1,127,859
Messages
5,627,294
Members
416,237
Latest member
Tethys

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