I'm trying to write VBA code to sum up cells, in the process i calculate the latest updated column using the statement:
clncnt1 = wb.Worksheets("Sheet1").Range("XFD16").End(-4159).Column
Suppose I capture the clncnt1 as 20 which is column P. Now how do I use:
wb.Worksheets("sheet1").Range("A1").Offset(16, clncnt).Formula = "=sum(*P12:P13*)"
using the column number clncnt1 that I just captured?
replace your sum with below
"=sum(" & Cells(12, clncnt1 ).Address & ":" & Cells(13, clncnt1 ).Address & ")"
you are actually getting the addresses of the range and then concatenating it all together.
Full code:
clncnt1 = Worksheets("sheet1").Range("XFD16").End(-4159).Column
Worksheets("sheet1").Range("A1").Offset(16, clncnt1).Formula = "=sum(" & Cells(12, clncnt1).Address & ":" & Cells(13, clncnt1).Address & ")"
this worked wb.Worksheets("sheet1").Range("A1").Offset(11, clncnt + 3).Formula = "=sum(" & _ wb.Worksheets("sheet1").Range("A1").Offset(8, clncnt + 3).Address & ":" & _ wb.Worksheets("sheet1").Range("A1").Offset(9, clncnt + 3).Address & ")"
Related
Failing againg with my project
I have formulas with variable Brand that is changed dynamically (AF Column). Basically all I want is to extract Brands into a column next (AE) to the formula column for visial convenience
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Next i
Range("AF:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
ActiveSheet.Range("AG2:AG8").Formula = ActiveSheet.Range("AF2:AF8").Formula
ActiveSheet.Range("AH2:AH8").Formula = ActiveSheet.Range("AF2:AF8").Formula
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim j As Variant
j = Application.Match(""" & Brand(i) & """, ws.Range("AF2:AF8"))
ActiveSheet.Range("AE2").Value = Application.Index(ws.Range("AF2:AF8"), j, 0)
And I get #N/A Already lost two days for that. Would be enourmously grateful to anyone who could help.
It's not exactly clear from your question as to your desired output but here's a guess:
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Range("AE" & i + 2).Value = Brand(i)
Next i
Range("AE:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
I've added a line to write the brand to AE, and altered the Sort to accommodate this.
I want to apply a formula that is automatically calculated by a column input from an Excel table to a specific column. At this time, since the column to which the formula is applied should appear at the same interval, the following code value is used and an error occurs. What am I doing wrong?
1004 runtime error
For b = 68 To 299 Step 21
Cells(2, b).Select
ActiveCell.Formula = "=IFERROR" & "([#[" & Cells(1, b - 2).Value & "]] " & "*" & "([#[" & Cells(1, b - 1).Value & "]]" & "/" & "1000000" & ")," & Chr(34) & Chr(34) & ")"
Cells(2, b).Select
Selection.AutoFill Destination:=Range("table1" & "[" & Cells(1, b) & "]")
Next b
cells(1,b) => table Header
I am doing an average of data (VBA Excel) as below:
If n < 8 Then
Sheet2.Cells(i, 20).Value = "=SUM(E" & i & ":S" & i & ")/" & n
Else
Sheet2.Cells(i, 20).Value = "=AVERAGE(LARGE(E" & i & ":S" & i & ", {1,2,3,4,5,6,7,8}))"
End If
n = 0
This code is working, because I want the best 8 values of 15 values.
My question is how to do the same with x values (x will be introduced via a userform, 0<x<16).
Of course, I could use a Select Case with 15 lines depending of x, but it does not seem to me a good coding.
Any ideas?
If AVERAGEIF is available:
Sheet2.Cells(i, 20).Value = "=AVERAGEIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))"
For i = 2 and x = 8, the formula is:=AVERAGEIF(E2:S2,">="&LARGE(E2:S2,8))
If not:
Sheet2.Cells(i, 20).Value = "=SUMIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))/" & x
For i = 2 and x = 8, the formula is:=SUMIF(E2:S2,">="&LARGE(E2:S2,8))/8
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
I am trying to average a column but only if the value is greater than zero. I then want it to put the information in the next blank cell in that row.
The below code was working as a simple Average but I want it to exclude any values of zero from the above cells.
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGE(" & .Address & ":" & .End(xlDown).Address & ")"
End With
I Tried with the following code to have it as if the cell address is greater than zero. But it keeps giving me an error to debug?
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & "," & Cell.Address & " > 0," & .Address & ":" & .End(xlDown).Address & ")"
End With
Any help would be great.
Thanks
Al
Your syntax for the formula is wrong.
You need to create a formula like
=AVERAGEIF(D2:Dxx, ">0")
So use this
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & ","">0"")"
End With