Typing out Excel formula in Excel Macro

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
I have this line of code that I require an excel macro to type out, and drag and drop down.
VBA Code:
=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),"UP",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),"DOWN",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),"LEFT",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),"RIGHT","NO"))))

Recording an excel macro it turned out to be:
VBA Code:
ws.Range("I3").FormulaR1C1 = "=IF(AND(RC[-4]>RC[-6],RC[-2]>RC[-4],RC[-3]>RC[-5],RC[-1]>RC[-3]),""UP"",IF(AND(RC[-6]>RC[-4],RC[-4]>RC[-2],RC[-5]>RC[-3],RC[-3]>RC[-1]),""DOWN"",IF(AND(RC[-4]>RC[-6],RC[-2]>RC[-4],RC[-5]>RC[-3],RC[-3]>RC[-1]),""LEFT"",IF(AND(RC[-6]>RC[-4],RC[-4]>RC[-2],RC[-3]>RC[-5],RC[-1]>RC[-3]),""RIGHT"",""NO""))))"

I'm just wondering why the excel macro types it out as "RC" instead of the cells such as E3, C3 (feels more difficult to read, when reading through the excel macro code). Is this a "best practices" way of writing the formula in the excel macro, is the code above fine, or is there a better (more efficient) way to write it out? Thanks!

Complete code is below:
VBA Code:
Sub Breakthrough()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Main")
  
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  

    ws.Range("I3").FormulaR1C1 = "=IF(AND(RC[-4]>RC[-6],RC[-2]>RC[-4],RC[-3]>RC[-5],RC[-1]>RC[-3]),""UP"",IF(AND(RC[-6]>RC[-4],RC[-4]>RC[-2],RC[-5]>RC[-3],RC[-3]>RC[-1]),""DOWN"",IF(AND(RC[-4]>RC[-6],RC[-2]>RC[-4],RC[-5]>RC[-3],RC[-3]>RC[-1]),""LEFT"",IF(AND(RC[-6]>RC[-4],RC[-4]>RC[-2],RC[-3]>RC[-5],RC[-1]>RC[-3]),""RIGHT"",""NO""))))"

    Range("C3:L" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
  
    Range("I2").Select
    Dim LastRow2 As Long
LastRow2 = Range("A" & Rows.Count).End(xlUp).Row
Selection.AutoFilter
ActiveSheet.Range("$A$2:$L$" & LastRow2).AutoFilter Field:=9, Criteria1:="UP"
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Add Key:=Range( _
"J2:J" & LastRow2), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Main").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

  
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Your code is using the R1C1 format because that's what the recorder told it to do. You could have used:
VBA Code:
ws.Range("I3").Formula = "=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),""UP"",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),""DOWN"",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),""LEFT"",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),""RIGHT"",""NO""))))"

The recorder is about as smart as a box of rocks.
 
Upvote 1
Solution

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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