Beginner Struggles with VBA

JackJr95

New Member
Joined
Dec 4, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am trying to utlize the recording feature to learn about the code that controls excel via taking an action and then reviewing the code that is recorded.

Recently I was able to get the following code to work:

Sheets("GEN INCENTIVE").Select
Range("AV1").Select
Columns("AV:AV").Select
Selection.Insert Shift:=xlToRight
Range("AV2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/100"
Range("AV2").Select
Selection.Copy
Range("AV2:AV268").Select
ActiveSheet.Paste
Selection.Style = "Percent"
Selection.Copy
Range("AU2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.Style = "Percent"

I was concerned that the bolded text would prevent the code from being executed if a new data sheet was used that had more than 267 rows, I wanted the selection to go all the way down to the end of the data hop right and then copy and paste that formula across that range.

The advice I got was to name my worksheets with something like this:

Dim GenIncentive As Worksheet

Set GenWS = Worksheet("GEN INCENTIVE")

What exactly does this do in terms of the code listed above, do I need to reference GenWS every time there is a line? What is the point of esatlbishing those variables.
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,029
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub JackJr()
   With Sheets("GEN INCENTIVE")
      .Columns("AV").Insert Shift:=xlToRight
      With .Range("AV2", .Range("AU" & Rows.Count).End(xlUp))
         .FormulaR1C1 = "=RC[-1]/100"
         .Style = "Percent"
         .Value = .Value
      End With
   End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,755
Messages
5,597,929
Members
414,193
Latest member
bb60

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