1

I am not a programmer, but I am trying to learn something of VBA code in excel for purpose of my job, so I have a question, is it possible to shorten this recorded macro like offseting it or something?

Range("B2").Select
ActiveCell.FormulaR1C1 = _
    "=IFERROR(R[25]C[8]:R[25]C[9],R[25]C[9])"
Range("C2").Select
ActiveCell.FormulaR1C1 = _
    "=IFERROR(R[25]C[9]:R[25]C[10],R[25]C[10])"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
    "=IFERROR(R[25]C[10]:R[25]C[11],R[25]C[11])"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
    "=IFERROR(R[25]C[11]:R[25]C[12],R[25]C[12])"
Range("F2").Select
.
.
.
.etc
Teamothy
  • 2,000
  • 3
  • 16
  • 26

1 Answers1

1

@FoxfireAndBurnsAndBurns mentioned on how to avoid select. Besides that you need to know few tips and tricks when you are coding. You will discover them in due time provided you continuously work with VBA code.

TIP: Look for patterns in your code. If required, use a loop to perform repeated task. In your code, there is a pattern. However no loop is rquired. You can enter the formula in the entire range in ONE GO

Your code can be written in one line! Let's say you want to enter the formula from Col B to Col F.

Range("B2:F2").Formula = "=IFERROR(J27:K27,K27)"
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250