Index Match in VBA Formula property - vba

I have been trying to loop through a range of cells and apply an index match . So, far, the index match is working only for the first row of the range (so its not looping). I am providing the code.
Dim LastRow As Long
Sheets("REPORT").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2:C" & LastRow).Formula "=INDEX('2609'!C:C,MATCH('REPORT'!A2,'2609'!E:E,FALSE))"

Write
Range("C2:C" & LastRow).Formula "=INDEX('2609'!C:C,MATCH('REPORT'!A2,'2609'!E:E,FALSE))"
as
Range("C2:C" & LastRow).Formula ="=INDEX('2609'!C:C,MATCH('REPORT'!A2,'2609'!E:E,FALSE))"
you are missing = sign.
Your code can be written as
Sub Demo()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = ThisWorkbook.Sheets("REPORT")
With ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("C2:C" & LastRow).Formula = "=INDEX('2609'!C:C,MATCH('REPORT'!A2,'2609'!E:E,FALSE))"
End With
End Sub

Related

Move Worksheet to String Variable

I am having trouble making a string variable equal to the cell in a worksheet since I get a type mismatch. I would also like to make a single string variable (SheetString) equal to all worksheet content. A portion of my code is below:
Range("A1").Select
Set sht = ThisWorkbook.Worksheets("Sheet3")
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set rng = Range("A1:A" & LastRow).SpecialCells(xlCellTypeBlanks)
rng.EntireRow.Delete
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'MsgBox (Continue)
Set rng = Range("A1:A" & LastRow)
'For x = 1 To LastRow
'RowString = cell(x, 1).Value
'if instr(,RowString,("Sheet1").
SheetString = Range("a1:a" & LastRow).Value
MsgBox (Continue)
IE.Quit
Thanks
you can't assign a string like this, if you want to asign the whole Range in Column A, you nned a loop, like this:
For i = 1 To LastRow
SheetString = SheetString & ";" & Range("A" & i).Value
Next i
See this example
Sub Sample()
Dim SheetString As String
LastRow = 12 '<~~ Example
SheetString = range("a1:a" & LastRow).Value
End Sub
You can't do this. You can store the entire column in an array though. For that you have to declare SheetString as Variant as shown below else you will get the Type Mismatch error as you are currently getting
Sub Sample()
Dim SheetString As Variant
LastRow = 12 '<~~ Example
SheetString = range("a1:a" & LastRow).Value
End Sub
This will create an array which you can loop to access
For i = LBound(SheetString) To UBound(SheetString)
Debug.Print SheetString(i, 1)
Next i

Excel VBA- Dynamic formula returning zero value for range

I am trying to have vba create a formula for two ranges within the same data set, as input into variables lastRow and lastRow2. However, when I try to create and calculate the formula, instead of getting the value of the last cell I get a zero. Offending code below:
Dim lastRow As Long
Dim lastRow2 As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
lastRow2 = Range("M" & Rows.Count).End(xlUp).Row
...
'Calculate ATRT 2014 at cell B4
Range("B6").Formula = "=(SUM(J11:J" & lastRow & ")/ SUM(I11:I" & lastRow & "))"
Range("E6").Formula = "=(SUM(U11:U" & lastRow2 & ")/ SUM(T11:T" & lastRow2 & "))"
Range("H6").Formula = "=E6-B6"
Running this gets two formulas: =(SUM(J11:J0)/ sum(I11:I0)) and =(SUM(U11:U0)/ SUM(T11:T0)). Why is the end of the range a zero???
I'm pretty sure what is happening if you are not defining which worksheet the range() and row() objects are on so VBA is guessing and probably guessing incorrectly. Try adding a worksheet object and then defining all the ranges and rows to use be on that worksheet.
Dim lastRow As Long
Dim lastRow2 As Long
Dim ws As Worksheet
Set ws = ActiveSheet
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
lastRow2 = ws.Range("M" & ws.Rows.Count).End(xlUp).Row
'...
'Calculate ATRT 2014 at cell B4
ws.Range("B6").Formula = "=(SUM(J11:J" & lastRow & ")/ SUM(I11:I" & lastRow & "))"
ws.Range("E6").Formula = "=(SUM(U11:U" & lastRow2 & ")/ SUM(T11:T" & lastRow2 & "))"
ws.Range("H6").Formula = "=E6-B6"

Apply formula to a range of cells

I have some code which I know works, I am applying a SUMIF formula to a range of cells. It works but it add a load of extra row at the bottom that shouldn't be there. I tried adding in a do until loop but it gets stuck in an infinite loop and crashes.
This is my first lot of code which works but adds the extra row in only on the columns which have been copied over.
Dim z As Workbook 'Budget Workbook
Dim y As Workbook 'Formatted - current workbook
Dim lastRow As Integer
Dim budgLastRow As Integer
Dim rng As Range
Set y = Workbooks("DLT.xlsm")
Set z = Workbooks.Open("C:\Reports\Budget.xlsx")
'Apply function to columns to pull costing data
With y.Worksheets("DLT")
lastRow = Cells(Rows.Count, 5).End(xlUp).Row
For Each rng In .Range("AI22:AI" & lastRow)
rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!H:H)"
rng.Value = rng.Value
Next rng
For Each rng In .Range("AJ22:AJ" & lastRow)
rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!I:I)"
rng.Value = rng.Value
Next rng
For Each rng In .Range("AN22:AN" & lastRow)
rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!E:E)"
rng.Value = rng.Value
Next rng
For Each rng In .Range("AO22:AO" & lastRow)
rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!G:G)"
rng.Value = rng.Value
Next rng
End With
I think the other additional rows have been copied because the budget workbook contains more data then the formatted work book. I have know thought to possibly delete the other unnecessary row which have been copied cross.
So I have added this small piece of code in
With y.Worksheets("Formatted")
lastRow = Cells(Rows.Count, 5).End(xlUp).Row - 1
budgLastRow = Cells(Rows.Count, 35).End(xlUp).Row
Rows("AI" & lastRow & ":AO" & budgLastRow).EntireRow.Delete
End With
I get an application-defined error Object defined error on the line
Rows("AI" & lastRow & ":AO" & budgLastRow).EntireRow.Delete
This is probably not the most efficient way to do this, but its the only way I could think of. I am fairly new to VBA only been coding a couple of months so mostly just try out different ways and see what works. Can anyone help me please.
You didn't properly qualify the ranges for the lastRow variable:
lastRow = .Cells(.Rows.Count, 5).End(xlUp).Row
Note the dots before Cells and Rows.
A couple of additional points:
Always use Long rather than Integer for row variables as there are more rows in a sheet than an Integer can hold.
You don't need to loop to put the same formula in a column of cells.
Dim z As Workbook 'Budget Workbook
Dim y As Workbook 'Formatted - current workbook
Dim lastRow As Long
Dim budgLastRow As Long
Set y = Workbooks("DLT.xlsm")
Set z = Workbooks.Open("C:\Reports\Budget.xlsx")
'Apply function to columns to pull costing data
With y.Worksheets("DLT")
lastRow = .Cells(.Rows.Count, 5).End(xlUp).Row
With .Range("AI22:AJ" & lastRow)
.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!H:H)"
.Value2 = .Value2
End With
With .Range("AN22:AN" & lastRow)
.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!E:E)"
.Value2 = .Value2
End With
With .Range("AO22:AO" & lastRow)
.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!G:G)"
.Value2 = .Value2
End With
End With

Copying values not formula in VBA

I have the below code, which works fine but it is copying the formulas and cell formatting not just the values. could anyone tell me how to obtain just the values?
Option Explicit
Sub MoveQuick()
Dim sh As Worksheet
Dim ws As Worksheet
Set sh = Sheets("Sheet1")
Set ws = Sheets("Sheet2")
ws.[a2:o2000].ClearContents
sh.Range("B8", sh.Range("S" & Rows.Count).End(xlUp)).AutoFilter 1, "MTU"
sh.Range("D9", sh.Range("I" & Rows.Count).End(xlUp)).Copy ws.Range("A" & Rows.Count).End(xlUp)(2)
sh.Range("T9", sh.Range("V" & Rows.Count).End(xlUp)).Copy ws.Range("H" & Rows.Count).End(xlUp)(2)
sh.[B8].AutoFilter
End Sub
This code will work for you.
sh.Range("D9", sh.Range("I" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy
ws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

How to sum values in variable range in VBA?

I have a table as shown below.
In column C I would like to Sum values from column A if they have the same index (column B). I would like to put sum result for all the rows if they have same index (as shown in column D).
Unfortunately the range of values with same index is variable and my macro can sum values just with 2 indexes. Can anyone help with it, please? Thanks!
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Dim n, i As Integer
Set ws = ActiveWorkbook.Sheets("Sheet2")
ws.Select
LastRow = Sheets("Sheet2").Range("A" & Sheets("Sheet2").Rows.Count).End(xlUp).Row
Range("C3:C" & LastRow).Select
Selection.ClearContents
For i = 3 To LastRow
If Range("B" & i + 1) - Range("B" & i) = 0 Then
Range("C" & i) = Range("A" & i + 1) + Range("A" & i)
Else
Range("C" & i) = Range("C" & i - 1)
End If
Next i
End Sub
Here's one way:
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = ActiveWorkbook.Sheets("Sheet2")
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws.Range("C3:C" & LastRow)
.ClearContents
.Value = ws.Evaluate("INDEX(SUMIF(B3:B" & LastRow & ",B3:B" & LastRow & ",A3:A" & LastRow & "),)")
End With
End Sub