Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Calculate average of both the columns and rows using VBA
I have a dataset that looks like the following:
Column A always contains ascending numbers (x-as in graphs for the time[s]).
The following columns contain data. Both the amount of rows and columns differs everytime dynamically.
I want to do two things in Excel using vba:
Calculate the average of each row and past it in the lastCol + 1 (GREEN)
Calculate the average of each column and past in in the row áfter the lastRow of column A.
Calculate the average of each row and past it in the lastCol + 1 (GREEN)
Calculate the average of each column and past in in the row áfter the lastRow of column A.
I am a bit struggling with the code (source). To make it work, but also to find the most efficient way.
(source)
'Option Explicit
Public Sub ExtractInformation()
'Calculate averages of columns
Dim lastCol As Long, lastRow As Long, m As Long, n As Long
Dim rng As Range
'Find the last column.
'Assumes the relevant column is the last one with data in row 5.
'With Sheets("Graphs")
lastCol = Sheets("Graphs").Cells(1, Columns.Count).End(xlToLeft).Column
lastRow = Sheets("Graphs").Cells(Rows.Count, "A").End(xlUp).Row
'End With
'Iterate the columns from 1 (ie "A") to the last.
For m = 1 To lastCol
With Sheets("Graphs")
'Define the data range for this column.
'Assumes last cell from bottom of sheet is the end of data.
Set rng = .Range(.Cells(1, m), .Cells(.Rows.Count, m).End(xlUp))
'Print the averages on row 125
.Cells(126, m) = WorksheetFunction.Average(rng) 'Print the averages on row 125
End With
Next
' For n = 1 To lastRow
' With Sheets("Graphs")
' 'Define the data range for this column.
' 'Assumes last cell from bottom of sheet is the end of data.
'' Set rng = .Range(.Cells(n, 1), .Cells(n, .Columns.Count).End(xlLeft))
' 'Print the averages on row 125
' .Cells(128, n) = WorksheetFunction.Average(rng) 'Print the averages on row 125
' End With
' Next
End Sub
'Option Explicit
Public Sub ExtractInformation()
'Calculate averages of columns
Dim lastCol As Long, lastRow As Long, m As Long, n As Long
Dim rng As Range
'Find the last column.
'Assumes the relevant column is the last one with data in row 5.
'With Sheets("Graphs")
lastCol = Sheets("Graphs").Cells(1, Columns.Count).End(xlToLeft).Column
lastRow = Sheets("Graphs").Cells(Rows.Count, "A").End(xlUp).Row
'End With
'Iterate the columns from 1 (ie "A") to the last.
For m = 1 To lastCol
With Sheets("Graphs")
'Define the data range for this column.
'Assumes last cell from bottom of sheet is the end of data.
Set rng = .Range(.Cells(1, m), .Cells(.Rows.Count, m).End(xlUp))
'Print the averages on row 125
.Cells(126, m) = WorksheetFunction.Average(rng) 'Print the averages on row 125
End With
Next
' For n = 1 To lastRow
' With Sheets("Graphs")
' 'Define the data range for this column.
' 'Assumes last cell from bottom of sheet is the end of data.
'' Set rng = .Range(.Cells(n, 1), .Cells(n, .Columns.Count).End(xlLeft))
Source of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке