Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
In excel I have a bunch of columns that are made up of a sequence of 1s and 0s. For each column I am looking to count the number of times that there are 4 or more 1s consecutive 1s in that column. I believe that a piece of vba code would be the easiest way to do this?
Example:
0
1
1
1
1
0
1
1
1
1
1
0
0
The count here would be 2.
Thanks for any help.
use this function:
Function Count1s(rng As Range) As Long
Dim rArr() As Variant
Dim i As Long
Dim t As Long
Dim n As Long
t = 0
rArr = rng.Value
For i = 1 To UBound(rArr, 1)
If rArr(i, 1) <> 1 Then
If t + 4 <= i Then
n = n + 1
End If
t = i
End If
Next i
Count1s = n
End Function
You would call it as a regular formula:
=Count1s(A1:A12)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to count the total number of cells that have values for the row that I'll be looking up using their name. I have a different sheet for looking up the value I tried COUNTA and VLOOKUP. Is there any way to combine these two so I'll end up with the correct result?
Please see screenshot.
Thank you!
Use INDEX(,MATCH()):
=COUNTA(INDEX('Sheet1'!C:X,MATCH("Jessel Rayes",'Sheet1'!A:A,0),0))
Here is a vba function that looks up the value you define (first input) in a specified range (second input) and then returns the number of empty cells in the same row right from this cell for a specified amount of columns (third input).
Function TLookupT(Value As Variant, arr As Range, column As Long)
x = 0
For Each Cell In arr
If Cell.Value = Value Then
For i = 1 To column - 1
If Cell.Offset(0, i) = "" Then
x = x + 1
End If
Next i
End If
Next Cell
TLookupT = x
End Function
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table below with details in column A and B. I want to search a string in C from A and paste value of Column B in Column D with help of VBA.
Example:-
A B C D
STRAT Strategy s_strat_nhnh Strategy
TRDMK Trademarks bng_trdm_ndnd Trademarks
TRDMK not in bng_trdm_ndnd.
But if I got it right, you want something like
Then Code is:
Sub Test()
CStartRow = 1
CEndRow = 5
AStartRow = 1
AEndRow = 3
For I = CStartRow To CEndRow
For J = AStartRow To AEndRow
If InStr(UCase(Range("C" + CStr(I))), UCase(Range("A" + CStr(J)))) Then
Range("D" + CStr(I)) = Range("B" + CStr(J))
Exit For
End If
Next J
Next I
End Sub
If you do not want to use VBA then you can use this formula.
Formula in d1 cell is:
=IF(ISNUMBER(SEARCH(A1,C1)),B1,"")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I cannot figure out how to do a loop within a loop.
There is a list of words on sheet 1 that need to be copied and pasted if they match up with any of the 20 desired key words on sheet two, column 1.
This then needs to be copy pasted onto sheet 3. Then I need to look at the same list from sheet 1 and copy paste those onto sheet 4 if they match up with any of the key words from sheet 2, column 2. I could use any help.
Single loop
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next i
Double Loop
Dim i As Integer,
Dim j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Good Luck
You don't need 2 loops (which are slow)
Loop through the values in sheet 1 testing if they exists using this:
If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 3
ElseIf WorksheetFunction.CountIf(Sheets("Sheet2").Range("B1:B20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 4
End If
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to subtract multiple values from the same number?
For example:
A = 1000
x = 200
y = 300
z = 400
In iteration 1: A - x = 800
In iteration 2: A - y = 500
In iteration 3: A - Z = 100
Can we assign a temporary variable to solve this problem? Also, I am looking to subtract all these values in a single cell that means the value should keep on changing in the single cell as follows A1: 800, A1: 500, A1: 100
This should get you started on your programming adventure:
Dim i As Long
Dim a As Double
Dim values As Variant
a = 1000
values = Array(200, 300, 400)
For i = LBound(values) To UBound(values)
a = a - values(i)
Range("A1") = a
MsgBox "Cell A1 now contains: " & Range("A1").Value
Next i
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am confused as how to create interest rate input as an argument
When I try to list Dim Rate as single an error box says duplicate argument
The purpose of this function is to calculate NPV for a set of cash flows (myarray)
Public Function myNPV(rate As Single, r As Range) As Single
Dim myArray(64) As Double
Dim i As Integer
Dim sum As Double
For i = 0 To n
Next i
For Each elem In r
myArray(i) = elem.Value
myArray(i) = myArray(i) / ((1 + rate / 100) ^ i)
i = i + 1
Next elem
End Function
n is zero. The For loop does nothing.
myNPV never gets assigned a value in the UDF