Summing cells from another sheet using offset - vba

I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!

You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"

Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!

Related

VBA: Using VLookUp to Set Dynamic Values in loop?

New VBA user here...
I have set up a Macro that runs for each file in a folder, as a loop.
In one part of the macro, there is an equation that contains a value that differs for each file.
Here is the equation (the value that changes for each file is 0.2483, everything else stays the same):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-83]C)/R[-85]C)*1000*0.2483"
I have set up a table that lists each file name and its corresponding value in another workbook. I have attempted to use vLookUp within the equation to find the value based on the file name - the file of which is active.
Here's what I have so far, for which I get a "Run-Time Error: 1004" on:
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(=VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!A2:B10,2,FALSE))"
Suggestions on how to make this work?
Try getting rid of the = in the formula and also use consistent cell notation (i.e. don't mix A1 notation with R1C1 notation):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!R2C1:R10C2,2,FALSE))"

How do I link to another sheet in Excel with VBA?

I'm trying to generate a list of hyperlinks in excel and link them dynamically to another cell in a different sheet. Could someone explain how I need to format the reference? Currently, it looks like this:
'p2r is one sheet
'sh is another
'Subaddress is currently linking to the correct location, wrong sheet.
'(It's linking to [p2r]'s cells, not [sh]'s cells)
p2r.Hyperlinks.Add_
Anchor:=p2r.Cells(p2rIndex, 1), _
Address:="", _
SubAddress:=sh.Cells(round2, 2).Address, _
TextToDisplay:=PrevRow
Thanks for the help! I've seen others use a format like: Sheet!A1, but when I tried something like:
SubAddress:=sh & "!" & Cells(round2, 2).Address
I got no results of value.
There are two modifications you could use to get your code working:
You could use
SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!" & Cells(round2, 2).Address
in order to generate something of the form SubAddress:="'Sheet name'!$A$1".
(The bits in the formula dealing with 's are to allow it to still work if there are spaces, etc, in the sheet name. If you know that won't occur, e.g. because you defined the sheet names and no-one else will be changing them, you could simplify it to SubAddress:=sh.Name & "!" & Cells(round2, 2).Address)
You could use
SubAddress:=sh.Cells(round2, 2).Address(External:=True)
which would generate something of the form SubAddress:="'[Book1.xlsx]Sheet name'!$A$1"

Using .Formula with variable in cells object

I've tried a variety of concats and uses of "" but I cant get .formula to work in the following code. The macro runs without error but it does not populate my sheet. I feel like this: Excel VBA formula with variables is what I am going for but main_row does not appear to be getting assigned to the variable when it is inside the .formula.
main_row = main.Range("b6").Row
calc_col = main.Range("k6").Column
main.Cells(main_row, calc_col).Formula = " = j & main_row / i & main_row "
Is it possible to use .formula with cells object?
Would rather not use application.worksheetfunction because i want end user to see the formula
Anyone have a good source to explain the proper way to qualify variables within .formula?
Try the below, I believe you were nearly there, but you inserting an unnecessary space before the equals sign + the quotes were out.
main.Cells(main_row, calc_col).Formula = "= J" & main_row & " / I" & main_row

Referring Cell range contained in variable in SUM formula

I have two cell addresses stored in my two variables
saddr and eaddr (starting and ending cell addresses respectively)
I have to find the some of all cells in between the range and have the value assigned to another random cell. My code below throws an error. I am pretty sure i am not using the variable containing the cell addresses in the right format. Please help
Code:
saddr = Cells(x, 3).Address
eaddr = Cells(x, (3 + 6)).Address
Worksheets("Sheet2").Range("C2").Select
ActiveCell.Formula = "=Sum(Sheet1!:"&saddr&":"&eaddr&")"
The last line throws up this error. Can you tell me how to use the variable cell references correctly in the formula?
You probably want
ActiveCell.Formula = "=Sum(Sheet1!" & saddr & ":" & eaddr & ")"
Even if this gets you going, you better check/fix a few things.
I strongly recommend you check this.

VBA Copy Cells + Formatting without using the clipboard

I'm trying to copy cells from one sheet to another without copying stuff to the clipboard but it must copy the formatting across.
Here are the ways I've tried at the moment, any ideas how to accomplish my needs?
LastRow = ThisWorkbook.Sheets("Nas").Range("A65536").End(xlUp).Row
'Option 1, works but it's using the clipboard :/
ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Copy
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).PasteSpecial
'Option 2, works but doesn't take formatting (ie text, general, time, date... etc)
Set Src = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow)
Set Dst = ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).Resize(Src.Rows.Count, Src.Columns.Count)
Dst.Value = Src.Value
'Option 3, works but doesn't take formatting (ie text, general, time, date... etc)
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0) = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Values
Your example code defines the formatting to be carried across as "text, general, time, date... etc". While the number formatting (a subset of the Properties of a cell) can easily be accommodated, delving further into enumerating the vast number of properties and subproperties of a Cells object is counter-productive when all relevant (non-default) properties can be carried across easily with a copy/paste operation using the clipboard.
With ThisWorkbook.Sheets("Nas")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0) = _
.Range("A6:F" & LastRow).Value
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0).NumberFormat = _
.Range("A6:F" & LastRow).NumberFormat
End With
Note that transferring the value of the cell across is performed with .Value or .Value2 not .Values.
I have seen this question popup occasionally and IMHO, the source of the question is likely a) a teacher that thinks this is a cutesy way to get students to appreciate just how many properties, subproperties and internal sub-subproperties a Range.Cells object (Range Members (Excel)) contains or b) some idiot with a fresh MBA that wants to prove they are smarter than the IT department. Enumerating through every possible formatting property a cell could contain is just a fool's errand (again IMHO) when the clipboard is available.
If you do attempt this yourself, don't forget Conditional Formatting and Comments, both of which can easily be attributed as part of a cell's formatting.