How to refer to a range with two variables - vba

I'm putting together a report and can't figure out the syntax for referring to a range with two variable rows. This is what I currently have.
.Cells(mrow, 4).Value = Application.Sum(Range("D3:D" & brow - 1))
brow is a variable row that I've defined earlier in the code.
Instead of referring to D3:d(brow), how can I refer to D(arow):D(brow) in this range? I can't figure out where to place the quotes.
Thanks!

.Cells(mrow, 4).Value = Application.Sum(.Range("D" & arow & ":D" & brow - 1))
Or
.Cells(mrow, 4).Value = Application.Sum(,Range(.Cells(arow,4),.Cells(brow-1,4)))

Related

substract using activecell.offset

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

How to put a VBA formula using dynamic variables capturing Address?

I am running a Loop to capture the start of Range and end of Range for a particular scenario. After I find my start range and end range, I want to put it as a formula in a cell. For example:
Cells(1,1).Formula "= Min( startrange:endrange)"
The above code has assumed the variables as text and instead of putting the cell address these variables are handling, it is pasting the formula as text like this : '= Min( startrange:endrange)'
I have no idea and have tried different approaches I could get from internet like below
' cells(4,4).formula = "=Min("&startrage& :"" & endrange & ")""
' Cells(4, 4).Formula = "=Min(startrange.value :endrange)"
'Cells(4, 4).Formula = Application.WorksheetFunction.Min(startrange:endrange)
' Cells(4, 4).Formula = "=Min("&startrange&"&":"& " &endrange&")"
where
startrange = ActiveCell.Offset(1, 3).Address(0, 0)
endrange = ActiveCell.Offset(0, 3).Address(0, 0)
Nothing works.
How can I achieve this? Specially I am also facing error in handling ":".
cells(4,4).formula = "=Min(" & startrage & ":" & endrange & ")"

VBA Rows.Count and UsedRange returning "1"

I have a small macro which takes data from multiple xml files and pastes in xlsm. Each xml file has a tag name, and the column the data pastes into has the tag name in row 4. I'm trying to paste into one below the last used row, but the three methods I have below keep returning "1" for the last used row.
Asterisks is the code I'm having trouble with.
For Z = 1 To 16
If Cells(4, Z).Value Like i Then
Dim Lengthend As Double
Dim An As Variant
An = (Split(Cells(4, Z).Address(True, False), "$")(0))
Lengthend = *****
Cells(Lengthend + 1, Z).Select
Cells(Lengthend + 1, Z).PasteSpecial Paste:=xlPasteValues
End If
Next
Here are the three methods I used to find the end of the column:
Dim sht As Worksheet
Set sht = Sheets("PI Data")
sht.Cells(sht.Rows.Count, "An").End(xlUp).Row
And
Lengthend = Range("An" & Rows.Count).End(xlUp).Row
And
(ActiveSheet.UsedRange.Columns(An).Count)
Each of these methods returns the answer "1." Any advice on what I'm doing wrong?
Lengthend = Range("An" & Rows.Count).End(xlUp).Row
The above has a typo and works if you change it to - what you propably tried to do is pass the value of variable An and not value "An"
Lengthend = Range(An & Rows.Count).End(xlUp).Row
You should also define the worksheet you are referring to to avoid any further problems -
Lengthend = Sheets("PI Data").Range(An & Rows.Count).End(xlUp).Row

How to write a average formula in excel vba

I am trying to calculate average, but i am getting an run-time error. Here is my code..
lastrowcell = Range("B1", Range("B1").End(xlDown)).Rows.Count
Range("F1").Value = "High+Low/2"
For n = 2 To lastrowcell
Range(Cells(n, 6)).Formula = "=average(" & Range(Cells(n, 2), Cells(n, 3)).Address(False, False) & ")"
Next
Can anyone show what I did wrong.
Thanks in advance...
You don't need to loop, Excel is smart enough to fill the entire thing in one go:
lastrowcell = Range("B1", Range("B1").End(xlDown)).Rows.Count
Range("F1").Value = "High+Low/2"
Range("F6:F" & LastRow).Formula = "=AVERAGE(B6:C6)"
The 6 will be incremented in each row
If you want the last row though, its always better to come from the bottom up unless you are specifically looking for the first blank:
lastrowcell = Range("B" & Rows.Count).end(xlup).row
Range(Cells(n, 6))
is not correct syntax for Range property. When there is only one parameter, it should be string. Instead you can use:
Cells(n, 6)
or
Range("F" & n).

Splitting cell with multiple data into multiple rows in more than 1 column

I have a sheet with multiple data in 1 cell this happen in a couple of columns. What I need to do is split the cell into individual rows while still keep the details from the other columns
Screen 1 shows the data i got
http://imageshack.com/a/img845/1783/wxc8.png (Screen 1)
Screen 2 is what i wish the macro to output.
http://imageshack.com/a/img842/7356/7yra.png (screen 2)
The macro i found and edited in only allows me to split 1 column and i can't get the editing of the range right. the columns that needs to be split is "J" "K" "N" and "O". The columns "A"- "I" and "L""M" just needs to copy their content to the new row.
Thank you in advance for the help.
Here the Macro im using
Sub Splt1()
Dim LR As Long, i As Long
Dim X As Variant
Application.ScreenUpdating = False
LR = Range("J" & Rows.Count).End(xlUp).Row
Columns("J").Insert
For i = LR To 1 Step -1
With Range("K" & i)
If InStr(.Value, Chr(10)) = 0 Then
.Offset(, -1).Value = .Value
Else
X = Split(.Value, Chr(10))
.Offset(1).Resize(UBound(X)).EntireRow.Insert
.Offset(, -1).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
End If
End With
Next i
Columns("K").Delete
LR = Range("J" & Rows.Count).End(xlUp).Row
With Range("L1:M" & LR)
On Error Resume Next
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
On Error GoTo 0
.Value = .Value
End With
Application.ScreenUpdating = True
End Sub
The problem appears to be the with operator. It constrains your selection. Try reformulating your macro without the with and refer to the the ranges direct. For example, replace your first for loop with something like this:
For i = LR To 1 Step -1
If InStr(Range("K" & i).Value, Chr(10)) = 0 Then
Range("K" & i).Offset(, -1).Value = Range("K" & i).Value
'Range("J" ...
'Range("N" ...
'Range("O" ...
Else
K_collection = Split(Range("K" & i).Value, Chr(10))
Range("K" & i).Offset(1).Resize(UBound(K_collection)).EntireRow.Insert
Range("K" & i).Offset(, -1).Resize(UBound(K_collection) - LBound(K_collection) + 1).Value = Application.Transpose(K_collection)
'J_collection = Split(Range("J"...
'N_collection = Split(Range("N"...
'O_collection = Split(Range("O"...
End If
Next i
In general I avoid with because it tends to obscure the visual pattern of code.
You might also consider eliminating the .INSERT and .DELETE columns, and overwrite directly to the cells. When working with more than one at a time, it becomes hard to keep track which column is temporary and which one is the source. But that all depends on your preference.
Copying values for the other columns should be easy compared to this.