Excel VBA Variable for R or C in an equation

below is a VBA formula for subtotalling filtered data and  which works fine if I define the column number as a given number  in in the section of code to the right
However I would like to define the column number as a variable ie Col Number 2 or Column Number 6 etc
Have seen some suggestions how this might work but doesnt seem to like the formula I am using
I know what I have put in is wrong but am trying to figure out the correct if possible version

Sub Analysis_Testdev()
 Dim ColNumberx As Integer
 ColNumberx = 2


   
    Sheets("WsheetA").Select
    Range("B10").Select
    Application.CutCopyMode = False
'
    ActiveCell.FormulaR1C1 = _
        "=IF('123WsheetA'!RC[-1]>-.1,SUMPRODUCT(SUBTOTAL(109,OFFSET('123WsheetA'!R48C5,ROW('WsheetA'!R49C5:R20000C5)-ROW('WsheetA'!R48C5),,1)),--('WsheetA'![R49C&ColNumberx]:[R20000C&ColNumberx]='WsheetA'!RC[-1])),"""")"
           
           
    ' my problem is getting this bit of the above equation to work in that I want the Column Number to be a variable ColNumberx and i cant seem to get it to work
       '   --('WsheetA'![R49C&ColNumberx]:[R20000C&ColNumberx]='WsheetA'!RC[-1])),"""")"
       ' the aim is to create a valid formula in B10 based on whatever column I choose and then copy it to cells B10:B29 but I cant get the column number based on a variable right
           
       
    Range("B10").Select
    Selection.Copy
    Range("B10:B29").Select
    ActiveSheet.Paste
   
   
 
End Sub
Paul CollinsAsked:
Who is Participating?
 
Paul CollinsAuthor Commented:
Thanks very much to everybody for your suggestions. Particularly liked Fabrice's elegant work  .
Strangely none of the solutions quite worked in this particular case but pointed me in the right direction
It turned out I had two problems
1 I was seeking to use a column number ( say column 10 , 11, 12 etc) as a variable
2 because I was starting in column B to get the formula to work I needed to deduct the column number (B being column 2) from the original variable ColNumberx to create a second variable ColNumberxb =Colnumberx-2
Similarly if I am using C I was i would have a variable ColNumberxc = ColNumber-3
As I was only using about 10 columns it was no problem to define and use the 10 variables which in turn made creation of new VBA's much easier and faster
For some strange reason my square bracket worked in a slightly different place than suggested

ActiveCell.FormulaR1C1 = _
        "=IF('WsheetA'!RC[-1]>-.1,SUMPRODUCT(SUBTOTAL(109,OFFSET('WsheetA'!R48C5,ROW('WsheetA'!R49C5:R20000C5)-ROW('WsheetA'!R48C5),,1)),--('WsheetA'!R49C[" & ColNumberxb & "]:R20000C[" & ColNumberxb & "]='WsheetA'!RC[-1])),"""")"


Best regards Paul Collins
0
 
Rgonzo1971Commented:
Hi,

pls try
    ActiveCell.FormulaR1C1 = _
        "=IF('123WsheetA'!RC[-1]>-.1,SUMPRODUCT(SUBTOTAL(109,OFFSET('123WsheetA'!R48C5,ROW('WsheetA'!R49C5:R20000C5)-ROW('WsheetA'!R48C5),,1)),--('WsheetA'![R49C&ColNumberx]:[R20000C" & ColNumberx & "]='WsheetA'!RC[-1])),"""")"

Open in new window

Regards
0
 
Paul CollinsAuthor Commented:
Short answer is No but your response seems to have two different treatments of the variable
f
-('WsheetA'![R49C&ColNumberx]:[R20000C" & ColNumberx & "] = !WsheetA'!RC[-1])

ie ![R49C&ColNumberx] and then  [R20000C" & ColNumberx & "]

was that intentional  

Thanks Paul
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Fabrice LambertFabrice LambertCommented:
Hi,

You need to concatenate the column number in your formula (notice the additional quotes):
ActiveCell.FormulaR1C1 = _
         "=IF('123WsheetA'!RC[-1]>-.1,SUMPRODUCT(SUBTOTAL(109,OFFSET('123WsheetA'!R48C5,ROW('WsheetA'!R49C5:R20000C5)-ROW('WsheetA'!R48C5),,1)),--('WsheetA'![R49C" & ColNumberx & "]:[R20000C" & ColNumberx & "]='WsheetA'!RC[-1])),"""")"

Open in new window


Additional notes:
- Never use objects such as ActiveWorkbook, ActiveSheet, ActiveShape, Selection, Range (alone) as their value depend on user action, and are by nature chaotic.
As a developper, you don't want to use chaotic objects.
Prefer referencing directly and precisely the objects you need.
- Avoid selecting ranges, it is slow as hell.

Your code can easily be replaced by:
Sub Analysis_Testdev()
  Dim ColNumberx As Integer
  Dim ws as Excel.Worksheet
  Dim rng as Excel.Range

  ColNumberx = 2
  Set ws = ThisWorkbook.WorkSheets("WsheetA")
  Set rng = Range("B10")
  Application.CutCopyMode = False

  rng.FormulaR1C1 =  '// your formula here

   Set rng = ws.Range("B10")
   rng.Copy ws.range(""B10:B29")

Open in new window

1
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
It should be like this...
ActiveCell.FormulaR1C1 = _
    "=IF('123WsheetA'!RC[-1]>-.1,SUMPRODUCT(SUBTOTAL(109,OFFSET('123WsheetA'!R48C5,ROW('WsheetA'!R49C5:R20000C5)-ROW('WsheetA'!R48C5),,1)),--('WsheetA'![R49C" & ColNumberx & "]:[R20000C" & ColNumberx & "]='WsheetA'!RC[-1])),"""")"

Open in new window

1
 
TracyVBA DeveloperCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Paul Collins (https:#a42371694)
-- Subodh Tiwari (Neeraj) (https:#a42370517)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

broomee9
Experts-Exchange Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.