Modifying VBA Code to Include All Used Rows (Dynamic, Not Static)

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
I import daily data into Excel for a morning report and the data does not always take up the same amount of rows. One day, it could go down to row 79 but the next day, it could go down to row 90. How can I modify the VBA below so that the code looks at all used rows instead of this static range?

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Selection.FormulaR1C1 = "=RIGHT(RC[-6],LEN(RC[-6])-FIND(""-"",RC[-6],1))"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G79")
Range("G2:G79").Select
Selection.Copy[/FONT]
 
The code you provided puts that formula into column G.

Here is the full code from my workbook:

' Inserts RIGHT formula into G2, applies result to A2, and deletes leftovers in column G
Range("G2").Select
Selection.FormulaR1C1 = "=RIGHT(RC[-6],LEN(RC[-6])-FIND(""-"",RC[-6],1))"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G79")
Range("G2:G79").Select
Selection.Copy
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:G").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A1").Select
 
Last edited:
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
No, my code puts the RIGHT formula into column G then copies the results down to row 79. Then it pastes values into column A and deletes the contents of column G.

Your code only affects rows 1-79 and then does weird things to 80 and below.
 
Upvote 0
That's because you have not changed your code to what I suggested & therefore only works on rows2 to 79.
Replace the last section of your code with

Rich (BB code):
    'Centers all data in column A, C, D, and E
    Range("A:A,C:E").EntireColumn.HorizontalAlignment = xlCenter
    
    With Range("G2:G" & Range("A" & Rows.Count).End(xlUp).Row)
      .FormulaR1C1 = "=RIGHT(RC[-6],LEN(RC[-6])-FIND(""-"",RC[-6],1))"
      .Copy
      Range("A2").PasteSpecial xlPasteValues
      .ClearContents
    End With
    Range("A1").Select

End Sub
 
Upvote 0
I swear I inserted your code but I must have screwed with another line.

Thank you very much! This was very helpful.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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