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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Statistical Packages

From novice to tech pro — start learning today.