Using INDIRECT Function in VBA

eoinymc

Board Regular
Joined
Jan 29, 2009
Messages
203
Hi,

I have the following function in my workbook:
Code:
=INDIRECT("'Sheet 1'!J"&$N$1)

Cell N1 has the number of the row that the data is on that I want to display.

My issue is that the Column "J" is dynamic on a monthly basis.

So, I was trying to do something like this in VBA ( I have a lot of code that splits data and does a lot of calculations and data manipulation, so this would just be an extension of the code):

Code:
CurrMth = 10

Ind.Range("H3").FormulaR1C1 = "=INDIRECT("'Sheet 1'!" & [B]CurrMth [/B]& "&$N$1)"

But it obviously won't work..

Any ideas on how I can make this indirect function dynamic?

Cheers,

Eoin
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This function from Microsoft converts numbers into column names e.g 1 to A, 8 to H e.t.c

I obtained the code from How to convert Excel column numbers into alphabetical characters

Code:
Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   iAlpha = Int(iCol / 27)
   iRemainder = iCol - (iAlpha * 26)
   If iAlpha > 0 Then
      ConvertToLetter = Chr(iAlpha + 64)
   End If
   If iRemainder > 0 Then
      ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
   End If
End Function

You can incorporate it to get the column alphabet which can then be passed into the Indirect function e.g
Code:
 ConvertToLetter(currmonth)
will give J when currmonth is 10
 
Upvote 0
Hi Momentman,

thanks for that, but even if I have a letter in there, it doesn't work..

Code:
[FONT=monospace]CurrMth = "J"

Ind.Range("H3").FormulaR1C1 = "=INDIRECT("'Sheet 1'!" & [/FONT][B]CurrMth [/B][FONT=monospace]& "&$N$1)"[/FONT]


This doesn't work either...

Any other ideas?

Eoin
 
Upvote 0
Nope,not like that

Set currmth = 10

then in your indirect formula, use ConvertToLetter(currmth)

Ind.Range("H3").FormulaR1C1 = "=INDIRECT("'Sheet 1'!" & ConvertToLetter(CurrMth) & "&$N$1)"
 
Last edited:
Upvote 0
Ok, so when I do this in VBA:

Code:
Ind.Range("H28").FormulaR1C1 = "=INDIRECT(""'Sheet 1'!"" & ConvertToLetter(CurrMthCell) &R1C14)"

I get this output in my work sheet:

Code:
=INDIRECT("'C-S3'!" & ConvertToLetter(CurrMthCell) &$N$1)

Any idea why it's not converting the ConvertToLetter(CurrMthCell) part to J?

Thanks,

Eoin
 
Upvote 0
Hi

Try :-
Code:
Ind.Range("H28").FormulaR1C1 = "=INDIRECT(" & Chr(34) & "'Sheet1'!" & Mid(Cells(1, CurrMth).Address, 2, 1) & Chr(34) & Chr(38) & "$N$1)"

hth
 
Upvote 0
Thanks Mike..

that works a treat...after I changed $N$1 to R1C14

Why does using the mid and .address functions make a difference?

Cheers,

Eoin
 
Upvote 0
This function from Microsoft converts numbers into column names e.g 1 to A, 8 to H e.t.c

Code:
Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   iAlpha = Int(iCol / 27)
   iRemainder = iCol - (iAlpha * 26)
   If iAlpha > 0 Then
      ConvertToLetter = Chr(iAlpha + 64)
   End If
   If iRemainder > 0 Then
      ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
   End If
End Function
Here is a much more compact function that you can use in the future...
Code:
Function ConvertToLetter(iCol As Integer) As String
   ConvertToLetter = Split(Cells(1, iCol).Address, "$")(1)
End Function
 
Upvote 0
Hi Eoin

Thanks for the feedback.

Pleased to have helped solve your problem.

The purpose of Cells(1, CurrMth).Address is to return "$J$1" when CurrMth is 10. Then, Mid(Cells(1, CurrMth).Address, 2, 1) returns "J" being the Column letter.

Good luck with your project.
 
Upvote 0
Let's say Sheet1!A1 has the text value "Sheet1!B1" and Sheet1!B1 has the value "1234". The following code will use the range address stored as text in A1 as an input and copy the range B1 to A2:

Code:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">Sub Tester()  

    Sheet1.Range(Range("A1")).Copy  

    Sheet1.Range("A2").PasteSpecial xlPasteAll

End Sub</code>
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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