Not sure how to word this well but how do for loops work in range names? what's the correct convention? E.g. I understand how the following works:
For i = 1 to 3
Sheets("1").Range("A1:B" & i).......
Next i
But if i want it to work for the the A column as well - what's the correct convention? Can't seem to find one that works
e.g. something like below
For i = 1 to 3
Sheets("1").Range("A" & i ":B" & i).......
Next i
Clarification
Sorry - I should be clarify my question
I'm not really looking for a loop, I just want to include a defined integer i column A
e.g.
Sheets("1").Range("A" & i :B3")
if that makes sense?
Probably cleener to do a range resize and enter the value in a single shot:
i = 3
Sheets("1").Range("A1").Resize(i, 2).Value = "Test"
You are missing one & in the line Sheets("1").Range("A" & i ":B" & i) after the first i. Please use this: Sheets("1").Range("A" & i & ":B" & i).Value = "Test".
------------ Old answer ------------------
If you want to loop through all cells in Range("A1:B3") you could use this:
For each cell in Sheets("1").Range("A1:B3")
cell.Value = "Test"
Next cell
This loop will progress like this: A1,B1,A2,B2,A3,B3. Hope this helps.
Probably you should switch from Range("A1:B2") to Range(Cells(1,1),Cells(2,2))
Just to clarify in VBA one has strings which are delimited by double quote character, e.g.
"hello"
"world"
To join strings together one uses an ampersand, so
"hello" & " " & "world"
Joining strings is technically known as string concatenation.
In the light of your question it is possible to join the evaluation of variables so if i = 1 then
"hello" & i & "world"
outputs hello1world.
The Range method returns a range that is either a single cell Range("A1") or multiple cells Range("A1:B1").
Simply start and end strings with double quotes and use ampersands between components of the expression so the answer for you is
Sheets("1").Range("A" & i & ":B3")
Related
I want to concatenate a variable in side a formula, but I cannot get it to work:
For char_index = 1 To 48
Cells(char_index, 2).Formula = "=MID('mysheet'!$B$8;" & CStr(char_index) & ";1)"
Next char_index
The idea is that the formula is added to column B from row 1 to 48 (or B1:B48).
cells(char_index, 2).Formula = "=MID('mysheet'!$B$8," & CStr(char_index) & ",1)"
Even if for your localization the list/formula separator is ";", in VBA you have to use "," and it will write the formula in the appropriate way...
I am using variable ranges, and want to insert those ranges into a formula that can be saved in a document that does not use VBA.
Do I have to use a with statement?
My If Statement works until I try and insert a relative range. I searched and didn't see much on this, so this issue is probably so simply I am an idiot.
The program will be doing things all over the worksheet, so relative coding is needed. The number of items in the range changes with each iteration. I will be using an end range integer [myRange] for the number instead of 10, but I wanted to simplify what I am struggling with into the smallest steps.
Dim Test As String
Test = "=IF(COUNTIF(" & ActiveCell.Offset(1, 0).Address & ":" & ActiveCell.Offset(10, 0).Address & "" _
& ")," & """Not Complete""" & "," & "" & """"")"
ActiveCell.Formula = Test
Desired output in target cell:
"=IF(COUNTIF($J$3:$J$12),"Not Complete","")"
As mentioned in comments, your COUNTIF statement is currently invalid. What are you counting in the range J3:J12? This needs to be added (in place of "CRITERIA" in below solution) before your equation will be valid
Test = "=IF(COUNTIF(" _
& ActiveCell.Offset(1).Resize(10).Address(True, True) _
& ",""CRITERIA""), ""Not Complete"", """")"
If you want your output to actually have the quotes you can output
Chr(34) & Test & Chr(34) which seems cleaner than wrapping the whole string in quotes from a readability point of view IMO
I have to calculate the difference between the values of 2 columns(firstCol and lastCol) and set those differences in a different column(NextColumn). The row count keeps changing, so I have to calculate the rowCount before calculating the difference. I'm trying to write a loop using Range so that the difference can be calculated but it doesn't seem to work.
For i = 3 To lastRow
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).FormulaR1C1 = "=Range(Cells(i, firstCol),Cells(i,firstCol)).Value - Range(Cells(i, lastCol),Cells(i,lastCol)).Value"
Next i
Any help would be greatly appreciated!
Thank you
Nick
For any input of "i" into the formula, you need to use " & i & " when using using a formula="", such as:
"=A" & i & "+B" & i
When you change to just a formula that doesn't input a formula to the cell (math happens in VBA), you can ignore the Excel formatting and "" blocking, such as:
= Cells(i,"A").Value + Cells(i,"B").Value
Make sure to use your loop variable where appropriate, so that you would have an outcome in a loop like:
Dim i as Long
For i = 1 to 10
Cells(i,"C").Formula = Cells(i,"A").Value + Cells(i,"B").Value
Next i
Why looping ? Unless I misunderstood the question, something like this should do the trick:
Range("X3:X"& lastrow).Formula = "=C3-D3"
The formula will adjust.
vba needs to be outside the quotes and you do not need the loop:
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).Formula = "=" & Cells(3, firstCol).Address(0, 0) & "-" & Cells(3, lastCol).Address(0, 0)
No loop needed. Excel will change the relative references as needed.
I have two columns with data. The first one has some terms and the other one contains single words.
what I have
I'm looking for a way to identify which words from each cell from the first column appear in the second, so the result should look something like this (I don't need the commas):
what I need
My question is somehow similar to Excel find cells from range where search value is within the cell but not exactly, because I need to identify which words are appearing in the second column and there can be more than one word.
I also tried =INDEX($D$2:$D$7;MATCH(1=1;INDEX(ISNUMBER(SEARCH($D$2:$D$7;A2));0);))
but it also returns only one word.
If you are willing to use VBA, then you can define a user defined function:
Public Function SearchForWords(strTerm As String, rngWords As Range) As String
Dim cstrDelimiter As String: cstrDelimiter = Chr(1) ' A rarely used character
strTerm = cstrDelimiter & Replace(strTerm, " ", cstrDelimiter) & cstrDelimiter ' replace any other possible delimiter here
SearchForWords = vbNullString
Dim varWords As Variant: varWords = rngWords.Value
Dim i As Long: For i = LBound(varWords, 1) To UBound(varWords, 1)
Dim j As Long: For j = LBound(varWords, 2) To UBound(varWords, 2)
If InStr(1, strTerm, cstrDelimiter & varWords(i, j) & cstrDelimiter) <> 0 Then
SearchForWords = SearchForWords & varWords(i, j) & ", "
End If
Next j
Next i
Dim iLeft As Long: iLeft = Len(SearchForWords) - 2
If 0 < iLeft Then
SearchForWords = Left(SearchForWords, Len(SearchForWords) - 2)
End If
End Function
And you can use it from the Excel table like this:
=SearchForWords(A2;$D$2:$D$7)
I have a partial solution:
=IF(1-ISERROR(SEARCH(" "&D2:D7&" "," "&A2&" ")),D2:D7&", ","")
This formula returns an array of the words contained in the cell (ranges are according to your picture). This array is sparse: it contains empty strings for each missing word. And it assumes that words are always separated by one space (this may be improved if necessary).
However, native Excel functions are not capable of concatenating an array, so I think the rest is not possible with native formulas only.
You would need VBA but if you use VBA you should not bother with the first part at all, since you can do anything.
You can create a table with the words you want to find across the top and use a formula populate the cells below each word if it's found. See screenshot.
[edit] I've noticed that it's incorrectly picking up "board" in "blackboard" but that should be easily fixed.
=IFERROR(IF(FIND(C$1,$A2,1)>0,C$1 & ", "),"")
Simply concatinate the results
=CONCATENATE(C2,D2,E2,F2,G2,H2)
or
=LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2))-2)
to take off the last comma and space
I've edited this to fix the problem with "blackboard"
new formula for C2
=IF(OR(C$1=$A2,ISNUMBER(SEARCH(" "&C$1&" ",$A2,1)),C$1 & " "=LEFT($A2,LEN(C$1)+1)," " & C$1=RIGHT($A2,LEN(C$1)+1)),C$1 & ", ","")
New formula for B2 to catch the error if there are no words
=IFERROR(LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2,I2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2,I2))-2),"")
Hello dear StackOverflow users,
My issue is regarding inserting a Formular via Macro into specific range of cells. Again, again and again.
So the Basic function of the formula should be the following:
=WHEN('ZEK '!F3="X";"6";WHEN('ZEK '!G3="X";"8";"1"))
In my macro it has to Change the column for the next specific range.
This is Happening in a for Loop for i growing.
meaning --> =When('ZEK '!$F$"& i+2 &".......
or in any other Syntax that should work. My try fairly does not, thats why I need your help.
This is my Initial try where i exchanged literally everything with strings:
Sub 123()
Dim a,b,c As String
a = 1
b = 6
c = 8
....
'used and declared variables k, x, f, a, b, c
Range(Cells(k + 2, 5), Cells(k + 27, 5)).FormulaR1C1 = _
"=WHEN('ZEK'!$F$" & f & "=" & x & ";" & a & ";WHEN('ZEK'!$G$" & f & "=" & x & ";" & b & ";" & c & "))"
End Sub
With that i get Runtime Error 1004. (changed it to .Formula from .FormulaR1C1)
I hope i gave enough Information, so that you could help me.
This really does not have to be performant, i just Need to get the formula into about 100.000 cells with i changing for each Range of cells
I think it's a language problem... Try
"=IF('ZEK'!$F$" & f & "=" & x & "," & a & ",IF('ZEK'!$G$" & f & "=" & x & "," & b & "," & c & "))"
Notice the use of IF and , as a delimiter instead of ;. Is WHEN even a valid worksheet function? To be honest I don't know if the worksheet functions get translated or not when setting the formula via VBA. The delimiters are adapted though.
edit: there is also the .FormulaLocal property that should work with ; as delimiter! So change .Formula to .FormulaLocal. This will probably cause an error if you execute it on a machine that uses , as default delimiter so I'd try to stick with .Formula.