How to average every 3rd row in excel using VBA [closed] - vba

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
I have column I with "item score". I would like to average every third row and return the value in Column A, which I have labeled "Overall Survey Score".
So the average of I2:I4 would be displayed in A2. The average of I5:I7 would be displayes in A5 and so on until the last row of data available.
I would like stay away from a formula in a cell and do this using VBA. Any help would be greatly appreciated.

Sub getavgs()
Dim i As Integer
For i = 1 To 27 Step 3
ActiveWorkbook.Worksheets("Sheet1").Range("A" & i).Value = _
(ActiveWorkbook.Worksheets("Sheet1").Range("L" & i).Value + _
ActiveWorkbook.Worksheets("Sheet1").Range("L" & i + 1).Value + _
ActiveWorkbook.Worksheets("Sheet1").Range("L" & i + 2).Value) / 3
Next i
End Sub

You can use a formula, like so. In I2 onwards
=IF(OR(ROW()-2=0,MOD(ROW()-2,3)=0),AVERAGE(OFFSET(I2,0,0,3,1)),"")

Related

Cut n cells and insert to below row using excel vba [closed]

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
I am trying cut 4 adjacent cells and insert a new row below and paste it on a new row.
My input is similar to
I have 4 columns Addr,Phone,Count,Amount. Some Rows of my excel sheet contains multiple n numbers of entries. I want to cut multiple of 4 cells and insert a new row below and paste it on.
The output would be similar to
I tried with transform function but unable to produce the expected result.
How can I do this with vba code or any excel functions
Here is the code its exactly work with your requirement
Sub Narasappa()
For i = 2 To 1000
If ThisWorkbook.Worksheets(5).Cells(i, 2) = "" Then
Exit For
End If
For j = 6 To 1000 Step 4
If ThisWorkbook.Worksheets(5).Cells(i, j).Value = "" Then
Exit For
Else
ThisWorkbook.Worksheets(5).Cells(i, j).Resize(, 4).Cut
ThisWorkbook.Worksheets(5).Range("B" & i + 1).Insert xlShiftDown
End If
Next
Next
End Sub

Merge two cells vertically and create loop until last row [closed]

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 6 years ago.
Improve this question
I'm currently having a hard time running a loop to merge two cells vertically. In the picture you can see what I'm talking about: I need to merge C1 and C2, then move on and merge C3 and C4 and so on...until there is no more data.
Can someone help me with that? I just have no idea how to create a loop having all the empty rows in between..
Thanks
Andy
Use something like,
Dim lastR as Integer
'get the last used row in col C
lastR = Sheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row
For i = 1 to lastR step 2
Range(Cells(i, 3), Cells(i + 1, 3)).Merge
Next i
Where the 10 is how far down you want to go, i is the starting row, and 3 is 3rd column, C.

Search Value from A column and Paste Value of B in Column D [closed]

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,"")

How to find the days difference between two dates [closed]

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 6 years ago.
Improve this question
I have been working on finding the Days difference between the dates found in Column A and column B. The dates in both columns are not constant thus I would need a code that would allow to read both dates in column A and B and find the Days difference between those two dates until the last row is empty.
Is there any code that I could use to find the Days difference between column A and B with a range of more than 500 rows?
Instead of using vba just enter the formula for the first row on C1 :
=ROUND(a1,0)-ROUND(b1,0)
Then just the formula to the end of exisiting rows.
If you insist using vba code use the simple code below:
Dim LastRow As Long
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("c1").Select
ActiveCell.FormulaR1C1 = "=ROUND(RC[-1],0)-ROUND(RC[-2],0)"
Range("c1").AutoFill Destination:=Range("C1:C" & LastRow)
End Sub

Delete spaces in cell - VBA [closed]

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 have the following string in excel:
" 163,40 3,10 1,86 163,30 163,40 167,00 163,30 435862329"
And I have no problem to split up this column into 8 individual columns - one for each block of data. But I saw that the first column - here 163.40 is truncated so it becomes 163 - that is from a float to an integer. I realized later thats because the numbers is preceeded by four spaces - " 163.40".
So my question is how to delete these four spaces - and ONLY these four first spaces.
That would solve my problem.
Any ideas?
Use Mid function like used below for your problem.
Mid(text, 5, Len(text))
Assuming that your source string is in cell A1 and you need the 8 columns data in row 2 ; please refer to below code :
Function SplitMyData()
Dim var As Variant
var = Split(Trim(Range("A1").Value), " ", , vbTextCompare)
For i = 0 To UBound(var)
Cells(2, i + 1).Value = var(i) 'Pasting vals in row 2
Next
End Function
You can change the source and destination cell references as per your requirements. :)