I'm trying to use the function OFFSET in a cells.formula and it returns me an erros 1004. I know that the formula work if I use it directly without macro
.Cells(5, c_write).Formula = "=MIN(OFFSET(data!" & Split(.Cells(1, c_read).Address, "$")(1) & "3;0;0;data_calc!" & .Cells(3, c_write + 1).Address & "))"
I tried with other formulas as MIN or COUNTA and it worked fine, but with OFFSET, it put an error 1004
Related
my lastrow is (315) - in the below code i need help in the last line (i am trying to do (sum of col H) - (sum of col J) by using activecell-offset in col k
''Totals''
Range("K" & LastRow).Offset(5, 0).Formula = "=activecell.offset(0,-3)-activecell.offset(0-1)"
I don't know how your last row becomes 315 and you have data on the 320 but you are looking for this:
Range("K" & LastRow).Offset(5, 0).Value = ActiveCell.offset(0,-3) - ActiveCell.offset(0-1)
Range("K" & LastRow).Offset(5, 0).Formula = "=" & ActiveCell.Offset(0, -3).Address & "-" & ActiveCell.Offset(0 - 1).Address
This would work, but is not pretty. An alternative would be a static VBA solution such as Damian's answer.
Furthermore, I would advise you to refer to the correct workbook and -sheet. If you were to omit this, the VBA code would always refer to the active workbook/-sheet, something you often don't want.
E.g.
With Workbooks(REF).Sheets(REF)
.Range("K" & LastRow).Offset(5, 0).Formula = "=" & ActiveCell.Offset(0, -3).Address & "-" & ActiveCell.Offset(0 - 1).Address
End With
Next to that, referring to the active cell is also asking for trouble. Perhaps you are better off with referring to better defined ranges, such as
With Workbooks(REF).Sheets(REF)
.Range("K" & LastRow).Offset(5, 0).Formula = "=" & .Cells(LastRow + 4, "K").Address & "-" & .Cells(LastRow, "J").Offset(0 - 1).Address 'Ranges randomly chosen
End With
EDIT
Formulas are dynamic. In other words, their results change dynamically with values/ranges they refer to. Unless you code a Worksheet_Change event, the calculation below will not update if you change the values for which the sum is calculated. However, you don't always need calculations to be dynamic, so pick what you need. I am assuming your columns have headers.
With Workbooks(REF).Sheets(REF)
LastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
LROWJ = .Cells(.Rows.Count, "J").End(xlUp).Row
LROWH = .Cells(.Rows.Count, "H").End(xlUp).Row
.Range("K" & LastRow).Value = Application.Sum(.Range("H2:H" & LROWH)) - Application.Sum(.Range("J2:J" & LROWJ))
End With
Would like to know how to code properly a SUMIFS formula and paste the results as value only.
Is it also possible to just have the formula loop only on blank cells? I tried running the code and it seems that is pastes the formula for all cells.
I've attached a sample code which I only got from other forums for reference. Would really appreciate your help guys!
Option Explicit
Sub SumGroups()
Dim lastCode As Long, lastFiltCode As Long
'Determine Last Row in Column O (Unfiltered Codes)
With Worksheets("Database")
lastCode = .Range("O" & .Rows.Count).End(xlUp).Row
End With
With Worksheets("Sheet3")
'Determine last Row in Column A (Filtered Codes)
lastFiltCode = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:K" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",$A2,Database!$I$2:$I$" & lastCode & ",B$1)"
End With
End Sub
You just have to have the work occur in VBA:
cells(1,1).value = Application.SumIfs(Range(Cells(1,1),Cells(10,1)),Range(Cells(1,2),Cells(10,2)), "<0")
Or, you can do this AFTER you have shown the formulas, so the value of the formula is pasted in place of the formula:
cells(1,1).value = cells(1,1).value
Edit1:
To point out why your code is entering the formula, by using .Formula = "", you are telling the system to display within the defined range that exact information. These each enter whatever is within the quotations to the cell:
cells(1,1).value = "Cat"
cells(1,1).formula = "=A1+B1"
cells(1,1).formula = "=A" & i & "+B" & i 'where i is a variable
'The number rows in the database changes each month hence the variable lastCode.
'The code runs without problems when I manually put last row in i.e. $M$22510 and $O$22510 however when I put the variable & lastCode in the SUMIFS I receive the error
Run-time '1004'application-defined or object defined.
Please tell me what is wrong with my SUMIFS in the code below.
Sub SumGroups()
Worksheets("Database").Activate
Dim lastCode, lastFiltCode As Integer
Dim Formula As String
'Determine Last Row in Column O (Unfiltered Codes)
lastCode = Range("O" & Rows.Count).End(xlUp).Row
'Filter Unique Codes into Column A Sheet2
Range("O1:O" & lastCode).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Sheet2.Range("A1"), Unique:=True
'Determine last Row in Column A (Filtered Codes)
Worksheets("Sheet2").Activate
lastFiltCode = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
'Place SUMIF Formula in Column B Sheet2
Worksheets("Sheet2").Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$ & lastCode,Database!$O$2:$O$ & lastCode,A2)"
End Sub
What you are looking for is:
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
The reason for this is when you put something into "quotation marks" VBA will read it as just text, you can use Debug.Print "=SUMIFS(Database!$M$2:$M$ & lastCode,Database!$O$2:$O$ & lastCode,A2)" and either press Ctrl + G or in VBA select "View" > "Immidiate Window" and it will show you what exactly is going into Excel
I'm trying to insert a chart into my spreadsheet, as outlined in msdn here:
https://msdn.microsoft.com/en-us/library/bb238877(v=office.12).aspx
Unfortunately i get the error in the
Method 'SetSource Data' of Object' _chart' failed
on the line starting ActiveChart. Why is it doing this? i've tried both string and range variables here to no avail.
In addition to the fact that i can't get this method to work, i don't like the fact you need to select the select the graph, surely there is a better way?
Function TimeSeries(rngToPrint As Range)
Dim strRange As String
Dim rngChart As Range
lngstartrow = 8
lngendrow = Range("a10000").End(xlUp).Row
Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)).Select
Sheets(rngToPrint.Worksheet.Name).Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlLine
End Function
The PlotBy argument specifies whether to plot the data by rows or columns. So the argument should be set to either xlRows or xlColumns.
The error appears at the following line because there is no PlotBy:=xlLine
Modify it to:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlRows
Or either:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlColumns
I have A column and B column, They compare their previous values And Iam comparing it with the following formulae
=IF((OR(AND(A2=A1,B2=B1),K2=0),0,1) it puts the 0 or 1 on the coresponding Q column
so when it goes to the 5th cell then it becames
=IF((OR(AND(A5=A4,B5=B4),K5=0),0,1)
But im trying to apply it in my VBA code like these
For numm = 2 To lastRow
Sheet1.Cells(numm, "Q").Value = ="IF(OR(AND(sheet1.cells(numm,""A"").value=sheet1.cells(numm-1,""A"")simlar way becolumn),sheet1.cells(numm,""k"").value=0),1,0)"
Next numm
But Im unable to peform the action it says 1004 error and object error
How do i use cells(numm,"A") in my VBA formulae or atleast any other way to put my formula and make it work
The reference to the looping numm within your formula needs to be out of the string.
Maybe you can set in VBA the cell formula itself...
For numm = 2 To lastRow
Sheet1.Cells("Q" & numm).Formula = _
"=IF((OR(AND(A" & numm & "=A" & numm - 1 & ",B" & numm & _
"=B" & numm - 1 & "),K" & numm & "=0),0,1)"
Next numm
Personally, I'd do the whole statement within VBA (ifs, ors, ands) and just drop the value back to Excel. Using Excel formulas makes the code harder to read.
Are you looking for something like:
Public Sub test()
Dim Sheet1 As Excel.Worksheet
Set Sheet1 = ActiveSheet
Dim numm As Integer, lastrow As Integer
lastrow = 5
For numm = 2 To lastrow
Sheet1.Cells(numm, "Q").Value = "=IF(OR(AND(" & Sheet1.Cells(numm, "A").Address & "=" & Sheet1.Cells(numm - 1, "A").Address & "," & Sheet1.Cells(numm, "B").Address & "=" & Sheet1.Cells(numm - 1, "B").Address & ")," & Sheet1.Cells(numm, "k").Address & "=0),1,0)"
Next numm
End Sub
Sheet1 can't be referred to inside the value of the cell, it only exists in the vba function you're creating, so instead you can append the strings together, and just get the cell addresses out to match the sort of function you were creating earlier.