Can't Remember what the code does

stephicohu

New Member
Joined
Jan 27, 2023
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
Hi guys!

I have a simple question to ask. I got this code from this message board last year. The question is I don't remember what this code does?

The code is the following:


StrFormula = "= IFERROR('" & SourceMonth & "'!R" & FirstRow1 & "C" & SourceColumn & ",0)"
Sheets(TargetMonth).Cells(TargetRow, TargetColumn).Formula = StrFormula

Sheets(TargetMonth).Activate


I am guessing that StrFormula lays out the operand of IFERROR and is taking the sourcemonth data and moving it to the targetmonth. What I don't know is what R does or C. Can someone help me to understand this code?

I would appreciate this

Stephanie Cohu
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If your workbook is set to use R1C1 reference styles (Options->Formulas->R1C1 reference style) or it was set by the VBA code, then you can refer to cells in a Row/Column format instead of using letters and numbers. So R5C2 refers to cell B5.

Your code is defining a formula using that format and then putting it in a cell.
 
Upvote 0
The code posted won't do much by itself. It relies on a number of variables that presumably have been populated previously.

Purely for illustration purposes, let's assume they have the following values:

VBA Code:
'Assumes you have worksheets called "Jul 2024" and "Aug 2024"
SourceMonth = "Jul 2024"
TargetMonth = "Aug 2024"

'Arbitrary values, just to ilustrate
FirstRow1 = 5
SourceColumn = 7
TargetRow = 11
TargetColumn = 13

Then this line:
Code:
StrFormula = "=IFERROR('" & SourceMonth & "'!R" & FirstRow1 & "C" & SourceColumn & ",0)"
will put the string value: "= IFERROR('Jul 2024'!R5C7,0)" into StrFormula, so that this line

Code:
Sheets(TargetMonth).Cells(TargetRow, TargetColumn).Formula = StrFormula
puts that formula into 'Aug 2024'!M11 (as M11 is row 11, column 13)

There are two ways of referencing cells. Generally we use "A1" referencing, but occasionally it's helpful to use "R1C1" referencing, where R1C1 means row 1, column 1, i.e. cell A1.

R5C7 refers to row 5, column 7, i.e. G5, so the end result is that
'Aug 2024'!M11 now contains the formula: = IFERROR('Jul 2024'!$G$5,0)
 
Upvote 0
The code posted won't do much by itself. It relies on a number of variables that presumably have been populated previously.

Purely for illustration purposes, let's assume they have the following values:

VBA Code:
'Assumes you have worksheets called "Jul 2024" and "Aug 2024"
SourceMonth = "Jul 2024"
TargetMonth = "Aug 2024"

'Arbitrary values, just to ilustrate
FirstRow1 = 5
SourceColumn = 7
TargetRow = 11
TargetColumn = 13

Then this line:
Code:
StrFormula = "=IFERROR('" & SourceMonth & "'!R" & FirstRow1 & "C" & SourceColumn & ",0)"
will put the string value: "= IFERROR('Jul 2024'!R5C7,0)" into StrFormula, so that this line

Code:
Sheets(TargetMonth).Cells(TargetRow, TargetColumn).Formula = StrFormula
puts that formula into 'Aug 2024'!M11 (as M11 is row 11, column 13)

There are two ways of referencing cells. Generally we use "A1" referencing, but occasionally it's helpful to use "R1C1" referencing, where R1C1 means row 1, column 1, i.e. cell A1.

R5C7 refers to row 5, column 7, i.e. G5, so the end result is that
'Aug 2024'!M11 now contains the formula: = IFERROR('Jul 2024'!$G$5,0)
Yes, you are right the arbitrary values are given earlier in the code. I just copy the code I had questions on. BTW, thank you for your clarifying answer and your quick response. This is a way you don't have to use the Copy, Paste Link formula.

Stephanie Cohu
 
Upvote 0
Hi Stephen!

I tried the code and I think I need another set of eyes to view it.

I am using the same code that you help me with earlier. The code is shown below:

VBA Code:
         strformula = "= IFERROR(' " & ssourcemonth & " ' !R" & isourcerow1 & "C" & isourcecol2 & ",0)"
         Sheets(stargetmonth).Cells(itargetrow1, itargetcol).Formula = strformula

Where:
ssourcemonth = "Sheet 1"
stargetmonth = "Sheet 3"
isourcerow1 = 9
isourcecol2 = 34
itargetrow1 = 9
itargetcol = 2

on the 2nd line of this code I get the following error:

Run-Time error '1004'
Application-defined or object-defined error.

I ran the code and everything works out fine except for this point in the code. Did I miss something?
 
Upvote 0
You have a number of stray space characters in the strformula line this time. Try this instead.
VBA Code:
strformula = "=IFERROR('" & ssourcemonth & "'!R" & isourcerow1 & "C" & isourcecol2 & ",0)"
 
Upvote 0
Solution
Good morning all:

I have another question this time the compiler is stating cannot find "Sheet1." Copy form: The error is on top of the window showing the open directory looking for Sheet1. I have Sheet1 as a sheet in the present workbook. I have interchange the names thinking this was the problem. It proved not to be the answer. The vba code is:

VBA Code:
         Sheets(ssourcemonth).Select
         itargetrow1 = 9
         isourcerow1 = 9

         strformula = "=IFERROR(' " & ssourcemonth & " '!R" & isourcerow1 & "C" & isourcecol2 & ",0)"
         Sheets(stargetmonth).Cells(itargetrow1, itargetcol).Formula = strformula

it happens on the next line. I have change the sheet names, remove sheets (ssourcemonth), added sheets(targetmonth). acitvate and it still gives this problem.

Sheets(stargetmonth).Cells(itargetrow1, itargetcol).Formula = strformula [/CODE]

VBA Code:
Where:
ssourcemonth = "Sheet 1"
stargetmonth = "Sheet 3"
isourcerow1 = 9
isourcecol2 = 34
itargetrow1 = 9
itargetcol = 2

What am i doing wrong?[ATTACH type="full"]114669[/ATTACH]
 

Attachments

  • Cannot find Sheet1.jpeg
    Cannot find Sheet1.jpeg
    147.9 KB · Views: 6
Upvote 0
Are the spaces between Sheet and the number supposed to be there?
 
Upvote 0
Yes, it is correct. VBA complier doesn't allow a space between sheets and the parenthesis. sheets () doesn't work.
 
Upvote 0
Okay, I looked over the code below
VBA Code:
strformula = "=IFERROR(' " & ssourcemonth & " '!R" & isourcerow1 & "C" & isourcecol2 & ",0)"
took out a couple more spaces and the software works...... Sorry about that guys. I do appreciate the work you guys do!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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