building a table from another spreadsheet? (Excel 2007 VBA)
-
Wednesday, April 11, 2012 5:14 PM
I try to build a table using VBA. The source data is in 1 spreadsheet and the result in another sheet of the same file.
Example:
ColumnA(Sheet1) - ColumnA(Sheet2)
Title A Title B
12.1
43.8 Average(A2:A3)
8.7 Average(A3:A4)
6.5 Average(A4:A5)
7.4 Average(A5:A6)
I work in VBA because this is the first step of a larger table.
Thank you.
- Edited by zenobrugge Wednesday, April 11, 2012 6:49 PM
- Edited by zenobrugge Wednesday, April 11, 2012 8:16 PM
- Edited by zenobrugge Wednesday, April 11, 2012 8:16 PM
- Edited by zenobrugge Wednesday, April 11, 2012 8:20 PM
- Edited by zenobrugge Wednesday, April 11, 2012 8:23 PM
- Edited by zenobrugge Wednesday, April 11, 2012 9:18 PM
- Edited by zenobrugge Wednesday, April 11, 2012 9:18 PM
- Edited by zenobrugge Wednesday, April 11, 2012 9:20 PM
- Edited by zenobrugge Wednesday, April 11, 2012 9:22 PM
- Edited by zenobrugge Wednesday, April 11, 2012 9:24 PM
- Edited by zenobrugge Wednesday, April 11, 2012 10:09 PM
- Edited by zenobrugge Thursday, April 12, 2012 12:37 PM
- Edited by zenobrugge Thursday, April 12, 2012 12:50 PM
- Edited by zenobrugge Thursday, April 12, 2012 2:45 PM
- Edited by zenobrugge Thursday, April 12, 2012 2:46 PM
- Edited by zenobrugge Thursday, April 12, 2012 3:15 PM
- Edited by zenobrugge Thursday, April 12, 2012 4:22 PM
- Edited by zenobrugge Friday, April 13, 2012 1:16 AM
- Edited by zenobrugge Friday, April 13, 2012 1:18 AM
All Replies
-
Thursday, April 12, 2012 6:28 AMAnswerer
It is not clear what is the concept for building formulas.Apparently you want a moving average.But what is the use of Input 2.
Can you upload a file in skydrive/sendspace and share us the link.
Clearly show how Sheet3 will look after macro runs and how you are deriving the formulas.
We can definitely help you better then.
-
Thursday, April 12, 2012 6:32 AM
I am little confused by the fact that RowA, RowB, ..., seem to refer to columns. Am I right about this?
Can you also be more specific about why you want to use VBA in the first place? In other words, what do you want to do that cannot be done with standard formulas and core Excel functionalities?
- Edited by Laurent Couartou Thursday, April 12, 2012 6:33 AM
-
Thursday, April 12, 2012 6:10 PM
Thank you for your comments. I have simplified the example above.
I have run the macro recorder, and received the following below.
Can this be written in a better and more generic way, taking into account the example I provide above?
[code]
Option Explicit
Sub ATestrecorder()
'
' ATestrecorder Macro
' record macro
ActiveCell.FormulaR1C1 = "=RC[-8]"
Range("I1").Select
Selection.AutoFill Destination:=Range("I1:N1"), Type:=xlFillDefault
Range("I1:N1").Select
Range("K2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(Datatest!RC[-8]:R[1]C[-8])"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K11"), Type:=xlFillDefault
Range("K2:K11").Select
Range("L3").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(Datatest!R[-1]C[-9]:R[1]C[-9])"
Range("L3").Select
Selection.AutoFill Destination:=Range("L3:L11"), Type:=xlFillDefault
Range("L3:L11").Select
End Sub[/code]
- Edited by zenobrugge Friday, April 13, 2012 2:28 AM
-
Friday, April 13, 2012 2:09 PM
A first step would be to clean the code, so as to remove references to current selection.
In your example, this would look like this
Range("I1").FormulaR1C1 = "=RC[-8]"
Range("I1").AutoFill Destination:=Range("I1:N1"), Type:=xlFillDefault
Range("K2").FormulaR1C1 = "=AVERAGE(Datatest!RC[-8]:R[1]C[-8])"
Range("K2").AutoFill Destination:=Range("K2:K11"), Type:=xlFillDefault
Range("L3").FormulaR1C1 = "=AVERAGE(Datatest!R[-1]C[-9]:R[1]C[-9])"
Range("L3").AutoFill Destination:=Range("L3:L11"), Type:=xlFillDefaultRegarding a more generic way, this depends on what you want to achieve.
My personal preferences would lead me to keep formulas entry in the destination worksheet itself, and just use VBA for the fill down operation. Something like that:
Public Sub FillRange(dataRange As Range, formulaRange As Range)
Dim destinationRange As Range
Set destinationRange = Range(formulaRange.Cells(1, 1), formulaRange.End(xlToRight).Offset(dataRange.Rows.Count - 1))
formulaRange.Rows(1).AutoFill Destination:=destinationRange, Type:=xlFillDefault
End SubWith the function being called like that, for example:
FillRange [A2:A5], [C2:E2]- Marked As Answer by Yoyo JiangMicrosoft Contingent Staff, Moderator Thursday, May 03, 2012 2:47 AM
-
Friday, April 13, 2012 5:06 PM
Thank you. I have a few follow-up questions.
Is there a way that I can write in range(I1:N1) a series of titles, e.g. range of "title1", "title2", "title3", etc for those cells?
And can I calculate the average with regards to a specific column (e.g. column D) instead? So that for example in 'column I' the average of every 2 cells in column D is calculated, in 'columnJ' the average of column E?
- Edited by zenobrugge Friday, April 13, 2012 5:13 PM
-
Monday, April 16, 2012 10:59 AM
Yes, you can assign content to a cell with VBA, using the Value or Value2 properties of the range object.
Regarding, the rolling average: this is what your formulas do already, or did I miss something?

