FormulaArray keeps changing to relative references - vba

I'm trying to enter a formula in a sheet using VBA (for later use in Excel Solver). It worked fine until this morning.
For u = 1 To Row2
Sheets("Testa").Cells(u + 1, 14).FormulaArray = "=SUM(IF(B2:B2000=" & CStr(u) & ",F2:H2000,0))"
Next
For v = 1 To Row
Sheets("Testa").Cells(v + 1, 18).FormulaArray = "=SUM(IF(A2:A2000=" & CStr(v) & ",F2:H2000,0))"
Next
The first loop gives this as a result in the cell: "=SUM(IF(RC[-12]:R[1998]C[-12]=1,RC[-8]:R[1998]C[-6],0))", changing the absolute references to relative references and not translating the functions to the french equivalent. The second loop works just fine. Column A and B contain a list of numbers (from 1 to Row or Row2) that look like this :
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
I don't know what I'm missing. Any help is welcome !

The range where I was writing the formula was set up as a text range, explaining why it didn't compute the formula. Changing it manually to a standard range solved the problem.

Related

Automatic sequential numbering with decimals based on arbitrary senority using VBA code on excel

I'd like Excel to automatically number each row I add with a sequential unique identifier that is dependent on the level of seniority that I've assigned to the row. (For example, if the row is a level 2, the task no. should be 2.1; if the next task is a level 1, the task no. should be 3). Lastly, I'd like it to be dynamic so if I were to add a row in the middle of the list, all the subsequent task no.s should become greater.
I founded pieces of VBA codes online but none that fits my purpose exactly. Was wondering if anyone has something for this purpose. Thanks for your help!
Level | Task No.
1 | 1
2 | 1.1
2 | 1.2
1 | 2
2 | 2.1
2 | 2.2
1 | 3
2 | 3.1
See illustration of table here:
No comments, but you should be able to follow...
Sub Label()
Const MAX_LEVELS As Long = 10
Dim levels(1 To MAX_LEVELS) As Long, i As Long, x As Long
Dim c As Range, s
Set c = ActiveSheet.Range("A1")
Do While Len(c.Value) > 0
s = ""
i = c.Value
levels(i) = levels(i) + 1
For x = i + 1 To MAX_LEVELS
levels(x) = 0
Next x
For x = 1 To i
s = s & IIf(s <> "", ".", "") & levels(x)
Next x
With c.Offset(0, 1)
.NumberFormat = "#"
.Value = s
End With
Set c = c.Offset(1, 0)
Loop
End Sub
Note it does not check you've been following the numbering scheme correctly (ie. if you go straight from level 1 to level 4 it won't warn you)

Use vba to write formula in cell

My basic formula is : =IF(COUNTIF($A:$A,$A2)=COUNTIFS($A:$A,$A2,$B:$B,$B2),"OK","NOT OK")
I use it to know if there are duplicate in column A, and i check the value in B.
ID Age My formula
1 15 NOT OK
2 50 OK
2 50 OK
3 35 OK
1 15 NOT OK
1 16 NOT OK
Now i'd like to write it with VBA, so I record a maccro, select the cell, press F2 then press Enter. I get:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(C3,RC[-10])=COUNTIFS(C3,RC[-10],C12,RC[-1]),""PRODUIT"",""ARTICLE"")"
Alright, it work. Now I'd like to use this formula with a new column:
Id Age Age formula Money Money formula Value3 Value3 formula ...
1 15 NOT OK 150 OK ... ...
2 50 OK 5 NOT OK ...
2 50 OK 800 NOT OK
3 35 OK 80 OK
1 15 NOT OK 150 OK
1 16 NOT OK 150 OK
I know how to use the formular "by hand" and I know how to use it for a single cell in vba, but I donĀ“t know hot to use it in a loop. (I have to use the formula on 25+ column that's why I need loop and variable)
Sorry for my bad english,
Thanks in advance
You just need to edit the 25 in For r = 2 To 25 to the last row you want to fill with the formula.
Code:
'Loops throug the rows
For r = 2 To 25
'Loops throug the columns
For c = 13 To 69 Step 2
'Converts the column-index into the column-letter
Dim col As String
col = Split(Cells(r, c - 1).Address(True, False), "$")(0)
'Writes the formula into the cell
Cells(r, c).Formula = "=IF(COUNTIF($C:$C,C" & r & ")=COUNTIFS($C:$C,C" & r & ",$" & col & ":$" & col & "," & col & r & "),""PRODUIT"",""ARTICLE"")"
Next
Next
Thank you tony for your time.
So:
my excel file has A to BQ column
from L to BQ, one out of two colum is data, and the next is the formula
My ID is always column C
basically M column will use column C to check the ID and column L to check the value.
Now the formulas:
cell M2 with Excel:
=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$L:$L;L2);"PRODUIT";"ARTICLE")
Cell M2 with VBA:
ActiveCell.FormulaR1C1 = _ "=IF(COUNTIF(C3,RC[-10])=COUNTIFS(C3,RC[-10],C12,RC[-1]),""PRODUIT"",""ARTICLE"")"
Now cell O2 (based on C and N columns) in Excel:
=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$N:$N;N2);"PRODUIT";"ARTICLE")
Finally cell O2 with VBA:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(C3,RC[-12])=COUNTIFS(C3,RC[-12],C14,RC[-1]),""PRODUIT"",""ARTICLE"")"
Now i'd like to use a loop (from L to BQ with step 2) to fill M2, O2, Q2, S2... with variable
I hope you get it this time,
sorry again for my low english level

Multiply column A values to Column B values and result in column C using excel VBA

I have two columns A and B. Range of A is A1:A6 and B is B1:B6. Now, I want to multiply these two columns and display result in column C using excel VBA. For example
A B C
2 3 6 (It is A1*B1)
3 8 24
9 2 18
7 3 21
2 4 8
5 4 20
I have tried this code but did not find it helpful:
Range("C1:C6").Value = Range("A1:A6") * Range("B1:B6")
Please guide me here as I am new to VBA. You help is really appreciated.
Alternatively,
for i = 1 to 6
cells(i,3) = cells(i,1) * cells(i,2)
next i
Try this. I have commented the code so that you will not have a problem understanding it. But if you still do then simply ask :)
'~~> This will enter the formula in the entire range in one go
Range("C1:C6").Formula = "=A1*B1"
'~~> This will convert the formula to values
Range("C1:C6").Value = Range("C1:C6").Value

Excel macro for loop?

I am writing a macro in excel and I have a 10 by 10 grid that I need to input a vlookup equation into. So for the first column of data I wrote a for loop
For i = 2 to 11
Cells (i, 30) = "=vlookup (E2" & i & ", B:C, 2, False)"
Next i
And it works the way I want it. Now I would like to write an outer for loop that will allow me to go across the columns.
I started with
For j = 30 to 39
For i = 2 to 11
Cells (i, 30) = "=vlookup (E" & i & ", B:C, 2, False)"
Next i
Next j
But I need E to change to F and I can't figure out how to do that.
I also tried using the Range (Cells(__,__),Cells(__,__)).FormulaR1C1 =...
But I couldn't get that to work either, since it is moving the table as well as fixing E2 across the columns.
If anyone can help I would greatly appreciate it. If not I will just write 10 for loops one for each column.
I think all you need to do is create the first formula and add "$" to appropriately anchor the reference rows/columns at which point you can simply copy the formula across the desired rows/columns.
here is a quick synopsis of anchoring: http://excelonthegrid.wordpress.com/2012/09/26/anchor-with-dollars/
i can give you the final answers if you post the desired formula for
a) column 1 row 1
b) column 1 row 2
c) column 2 row 1
d) column 2 row 2

What does the To and Step mean in VBA?

Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
Does anyone know how this loop works? I'm confused on the meaning of rows To 1 Step (-1).
from high number To 1 adding (-1) each iteration
Note: It's adding because + AND - in mathematical logic evaluate to a -
If rows = 10 then
for i = 10 to 1 step -2 would mean loop back from 10 to 1 subtracting 2 from the i in each loop cycle.
adding a Debug.Print i inside the loop may give you a better clue.
Note: turn ON the Immediate Window hitting CTRL+G or View => Immediate Window from the VBE menu bar
An example loop increasing by 3 on each cycle.
for i = 1 to 10 step 3
debug.print i
next i
Usage
The step-back technique is mostly used when deleting rows from a spreadsheet.
To see the logic in practice see the following
How to select and delete every 3rd column
Delete entire excel column if all cells are zeroed
Excel VBA - Scan range and delete empty rows
When deleting rows, it is often common practise to start at the end and step backwards, this is so no rows are skipped.
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
'delete row if "delete" is in column 1
If rng3.cells(i,1).Value = "delete" Then
rng3.Rows(i).EntireRow.Delete
End If
next i
Dim i as Integer
For i = 1 To 14 Step 3
Debug.Print i
Next i
In above code loop will iterate from 1 to 14 increment with 3 so output will be like
1 4 7 10 13
It means it can not cross 14 that is limit.
So whatever value is provided in step it will add into the variable use for looping purpose. Here
i = i +3
But in For loop in VBA, Step value can not be changed dynamically. For example:
Dim i As Integer
For i = 1 To 10 Step i
Debug.Print i
Next i
Here, before starting iteration Step is equal to the value of i that is the default value i.e. 0. So i will increment like below:
i = i+ i => i = i+0
So i will not increment here and loop will iterate for ever.
Now for below code:
Dim i as Integer
For i = 1 To 14 Step i+1
Debug.Print i
Next i
i will increment like :
i=i+(i+1) => i= i+(0+1) =>i = i+1
so it will increment by 1 and output will be 1 2 3 .... 14
Now for below code :
Dim i As Integer
i = 3
For i = 1 To 10 Step i
Debug.Print i
Next i
here, i is equal to 3 before loop execution, so Step value will be 3, but loop will start with i = 1 and will increment with 3 through out the loop.
here,
i = i+3
so output will be 1 4 7 10.
Now for some other variable:
Dim i As Integer
Dim j As Integer
j = 2
For i = 1 To 10 Step j
Debug.Print i
j = i
Next i
in above code Step value will be 2, so i will increment by 2 for every iteration whether j is modifying inside loop or not, it will not impact Step value, so output will be
1 3 5 7 9
Please correct me if I miss anything or something is wrong in this. Also suggest if there is any way for dynamic looping using For loop in VBA.