I have been trying to put a SUMIF formula into a cell but it returns a weird result.
BottomRow winds up being the last row in a table. When I run the macro and look at the value of txtformula in the immediate window I get =Sumif(B6:B241,F6:F19,D6:D241)
when it goes in the cell it enters =SUMIF(B6:B241,#F6:F19,D6:D241)
Where is the darn # sign coming from?
Range("B6").End(xlDown).Select
Classbottom = ActiveCell.Address
BottomRow = Range(Classbottom).Row
'Create totals data
Range("G6").Activate
txtFormula = "=Sumif(B6:B" & BottomRow & ",F6:F19,D6:D" & BottomRow & ")"
ActiveCell.Formula = txtFormula
I guessed at trying a different formula. I did
activecell.formula2 = txtFormula
It did the trick
I have the following situation:
I would like have EACH initial Ref. Cell cell and its associated blank cells, for example A2:A7, updated so that its results in the following data structure:
I have tried using formulas containing a combination of COUNT, COUNTBLANK, ROW() but have failed miserably in achieving the desired outcome.
Can you help?
In A2, formula copied down :
="1:"&COUNTA(B$2:B2)&":"&ROW(A1)-MATCH("zz",B$2:B2)+1
If with vba, maybe something like this ?
Sub Macro1()
For Each cell In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants)
If cell.End(xlDown).Row = Rows.Count Then Exit Sub
Set oEnd = cell.End(xlDown).Offset(-1, 0)
Set rngNum = cell
num = "'" & cell.Value & ":"
n = Range(cell, oEnd).Rows.Count
For i = 1 To n
rngNum.Value = num & i
Set rngNum = rngNum.Offset(1, 0)
Next i
Next cell
End Sub
But the weakness, the code will stop at the last row value in column A.
For example based on your first image, the code will stop at 3:1 while you are expecting this 3:1 row will change to 3:1:1 and the next row value is 3:1:2
I am trying to assign If formula to certain cell every 16th row. Formula needs to compare values of cells in J, K and L columns and gives Pass or Fail Result.
I am having issues making this If formula work in a loop. Please Help! My Code is below.
First I am getting compile error saying " Expected end of statement"
Second, is there any other way to make these work? I need same formula applied to Cell M5 to M789 at every 16th cell.
For a = 5 To 789 Step 16
Range("M" & a).FormulaR1C1 = "=IF(Range("J" & a)>= Range("K" & a),IF(Range("J" & a))<= Range("L" & a),""PASS"", ""FAIL""),""FAIL"")"
You need .Formula instead of .FormulaR1C1 - you are not using R1C1 references.
Remove the Range calls from the formula part. That's mixing VBA with regular worksheet formula syntax.
You can simplify the formula using AND. And in this case you don't need to concatenate a in at all. Excel is smart enough to update relative references.
Sub Test()
Dim myFormula As String
myFormula = "=IF(AND(J5>=K5,J5<=L5),""PASS"",""FAIL"")"
Dim a As Long
For a = 5 To 789 Step 16
Range("M" & a).Formula = myFormula
Next
End Sub
I'm looking for a formula to find and replace particular piece of text in a cell.
It sound a bit confusing but you can see what I mean by viewing the following image.
What I'm trying to achieve is when I fill for example cell B1 I would like to replace "SYS-NAME" in cell A25 and other cells where "SYS-NSME" is present.
You need to replace the ** before and after your values. You could change it to "" instead if you want to highlight those inserted values. ** will cause the entire cell to be replaced, instead of its match.
So the code splits column A into 2 sub columns. The first column runs until the first blank row, this contains all your variables and their new value. The second column runs until the end of the last used row. This contains your configuration with the variables that need to be changed.
Sub FindReplace()
Dim NewText, OldText As String
Dim LastRowText, i As Integer
LastRowText = Range("A1").End(xlDown).Offset(1, 0).Row
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
i = 1
Do While i < LastRowText
OldText = Range("A" & i).Value
NewText = Range("B" & i).Value
With ActiveSheet.Range("A" & LastRowText & ":A" & LastRow)
.Replace OldText, NewText, xlPart
End With
i = i + 1
Loop
End Sub
Option without a macro
In your configuration, edit each line with a variable to the following format. This will instantly update the configuration file too.
Cell A25: ="sysname " & B1
Cell A34: ="irf domain " & B2
...
The short answer would be
=SUBSTITUTE(A1,"**SYS-NAME**","MYSYSNAME")
starting in B1.
The formula =LEFT(A2,FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))-1) to remove all characters to the right of the last . (there might be more then one) works if I add it to a cell and copy down.
I can not figure out how add the formula to vba
I have the vba if there is only one .
(the cells to be evaluated are in column A and the output is to column E )
When I add the code ta a module c.Formula = "=LEFT(A2,FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))-1)" it highlights in red and error Invalid Character with the | highligthed
Thanks
Sub NotWorking()
Dim c As range
Dim Lastrow As Long
With Sheets("sheet1")
Lastrow = .range("C" & .Rows.Count).End(xlUp).Row
For Each c In .range("E2:E" & Lastrow)
c.Formula = "=LEFT(A2,FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))-1)"
Next
.range("E1").Value = "Source"
End With
End Sub
First tip:
when your are usign quotes in formula in VBA, you should use double qoutes. E.g.
instead
Range("E1").Formula="=IF(A1="test",1,2)"
you should use
Range("E1").Formula="=IF(A1=""test"",1,2)"
Second tip:
There is no need to use loop when applying formula. There is a more efficient way to do it.
Next your code
For Each c In .range("E2:E" & Lastrow)
c.Formula = "=LEFT(A2,FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))-1)"
Next
gives you formula for E2 : =LEFT(A2,...), and formula for E3 again =LEFT(A2,...) (note in both formulas A2). I think, it's not what you expected. Use this one instead:
.Range("E2:E" & Lastrow).Formula="=LEFT(A2,FIND(""|"",SUBSTITUTE(A2,""."",""|"",LEN(A2)-LEN(SUBSTITUTE(A2,""."",""""))))-1)"
Above code applies formula to entire range. Formula would be adjusted for each row, e.g. formula for E2 would be =LEFT(A2,...), while formula for E3 would be =LEFT(A3,...) and so on.
But, if you need to have the same formula for entire column E (for E2 : =LEFT(A2,...), and for E3 again =LEFT(A2,...)), use absolute/mixed references (with $ sign):
.Range("E2:E" & Lastrow).Formula="=LEFT(A$2,FIND(""|"",SUBSTITUTE(A$2,""."",""|"",LEN(A$2)-LEN(SUBSTITUTE(A$2,""."",""""))))-1)"
In addition to the points #Simoco makes, you need to adjust the cell refernces in order to get the same effect copying a range gives you, as Excel will automatically adjust these for you (unless you use absolute refernces $), whereas VBA won't.
The easiest way to do this is to use the R1C1 version of cell references.
Try this
Sub Working()
Dim rng As Range
With Sheets("sheet1")
Set rng = Range(.Cells(2, 5), .Cells(.Rows.Count, 3).End(xlUp).Offset(, 2))
End With
rng.FormulaR1C1 = "=LEFT(RC1,FIND(""|"",SUBSTITUTE(RC1,""."",""|"",LEN(RC1)-LEN(SUBSTITUTE(RC1,""."",""""))))-1)"
End Sub