I am getting run-time error for Vlook-up formula while using dynamically from activesheet to another sheet i.e Binarysheet, in Binary sheet, my lookup range is A to C column
I need to iterate the for loop and I have to use vlookup inside the for loop
Dim wfd As Worksheet
Set wfd = thisworkbook.Sheets("Binarysheet")
For i = 20 To 61
Cells(2, i).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],wfd!$A:$B,2,False)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells(2, i+1).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],wfd!$A:$C,3,0)"
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
With the above comments and advice from the two linked questions, we can simplify the whole thing and load the whole without the need to loop:
Dim wfd As Worksheet
Set wfd = ThisWorkbook.Sheets("Binarysheet")
With ActiveSheet
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim rng As Range
Set rng = .Range(.Cells(2, 20), .Cells(lastrow, 61))
rng.FormulaR1C1 = "=VLOOKUP(RC1,'" & wfd.Name & "'!C1:C65,COLUMN(RC),False)"
rng.Value = rng.Value
End With
I'm setting up a pricing model and am wondering how I am able to get the macro to run the pricing loan by loan and have the output pasted in a separate tab (this would also be loan by loan, so it cannot overwrite). I used the macro recorder and this is what I have so far, but I'm a novice and not sure how to loop this until it hits a blank cell (I did the first two loans....)
Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Copy
Sheets("Cashflows").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Input").Select
Range("A3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Cashflows").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
The tools you need:
To figure out the last row:
Dim LastRow As Integer
LastRow = Range("A" & Rows.Count).End(xlUp).Row
'This simulates selecting the last cell in "A" Column,
'hitting "End" and "Up Arrow", then returns that row number
'as in integer.
To cycle through each row:
Dim I As Integer
For I = 1 To 10 '(Or replace "10" with "LastRow")
'Do something like look at a range value:
Debug.Print Cells(I, 1).Value
Next I
Finally, this is going to be a lot easier if you use .value = .value instead of copying and pasting:
Dim RowNum As Integer
RowNum = 10
Range("A1").Value = Range("B1").Value 'Copies Value from B1 into A1
Cells(1, 1).Value = Range("B1").Value 'Does Exact same thing as above: Cells(row, column)
'Copy A10:C10 from sheet2 to sheet1:
Sheet1.Range("A" & RowNum & ":C" & RowNum).Value = Sheet2.Range("A" & RowNum & ":C" & RowNum)
See how far you get with that and come back if you have more specific questions.
There are lots of good resources out there if you're having trouble.
I have a macro that performs some cleanup on values. It works fine unless there is only one row of data in the spreadsheet; if there is only one row, it blanks out my value instead of fixing it.
Sub UpdateNumberFormat()
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Columns("AL:AM").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.NumberFormat = "General"
Range("AL2").Select
ActiveCell.FormulaR1C1 = "'"
Range("AM2").Select
ActiveCell.FormulaR1C1 = "=CONCAT(RC[-1],RC[1])"
Range("AL2:AM" & LastRow).Select
Selection.FillDown
Range("AM2:AM" & LastRow).Select
Selection.Copy
Range("AN2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("AL:AM").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.NumberFormat = "General"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
Range("C2:C" & LastRow).Select
Selection.FillDown
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
I'm trying to create a Match macro that compares two lists and gives me the cells that present in only one of the lists. The cells are then copied to another sheet where the cells are counted. However, the blank cells are also being copied and I don't know why.
The below is what I have:
Sub Macro_do_Match()
Dim CopyrangeB As String
Dim lRowB As Integer
Dim fRowB As Integer
Dim CopyrangeD As String
Dim lRowD As Integer
Dim fRowD As Integer
Dim rng As Range
' Defines range for column B
lRowB = Cells(Rows.Count, 1).End(xlUp).Row
fRowB = 2
Let CopyrangeB = "B" & fRowB & ":" & "B" & lRowB
' "macro"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[1],0)))=FALSE,C[-1], """")"
Range("B2").Select
Selection.AutoFill Destination:=Range(CopyrangeB)
' Defines range for column D
lRowD = Cells(Rows.Count, 3).End(xlUp).Row
fRowD = 2
Let CopyrangeD = "D" & fRowD & ":" & "D" & lRowD
' "macro"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=IF(NOT(ISERROR(MATCH(C[-1],C[-3],0)))=FALSE,C[-1], """")"
Range("D2").Select
Selection.AutoFill Destination:=Range(CopyrangeD)
'Copy and paste B
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and paste D
Sheets("Insert Lists").Select
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Final Results").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
My code for writing a formula works.
Range("A2").Select
a = Range(Selection, Selection.End(xlDown)).Count + 1
Range("N2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A""," & _
"""N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))"
Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(a, 14))
I need a reversed formula for every row that has specific text in column F.
These are: "AR17-Report", "CS02-Report", "HSCBD-Report", "KHG-Report".
If column F has any of those texts the formula in column N should be reversed.
Instead of >, it should be <.
This code is reversing the formula. However, the pivot in my other sheet is not updating or not getting data when I replace my code to this for reversing the formula.
MAXROW = 10000
For i = 1 To MAXROW
Range("N" & i).Select
If Not IsError(Application.Match(Range("F" & i), Array("AR17 - Past Due Receivables Outstanding", "AR18 - Past Due Receivables Outstanding greater than 60 days", "HRSC01NA - Call Abandonment Rate ", "ESC07 - Call Abandonment"), False)) Then
ActiveCell.FormulaR1C1 = "=IF(RC[-2]<=RC[-4],""Met"",""Not Met"")"
Else
ActiveCell.FormulaR1C1 = "=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A"",""N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))"
End If
Next
Now here is my whole code:
Sub Button5_Click()
Dim b As Integer
Sheets("database_2").Select
ActiveSheet.Range("$A$1:$P$7436").AutoFilter Field:=8, Criteria1:="=LIVE", _
Operator:=xlOr, Criteria2:="="
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
Range("A1").Select
Selection.AutoFilter
Range("A2").Select
b = Range(Selection, Selection.End(xlDown)).Count + 2
Range("A2").Select
Cells(b, 1).Select
Windows("NEW Consolidated Data Point System_Final.xlsm").Activate
Range("D1").AutoFilter Field:=4, Criteria1:=Array("HR", "OM", "PY"), Operator:=xlFilterValues
For Each rngCell In Range("D2:D" & Range("D2").End(xlDown).Row)
If Not rngCell.EntireRow.Hidden Then
rngCell.Value = "ES"
End If
Next rngCell
Selection.AutoFilter
Windows("Compliance.csv").Activate
Range("I1").AutoFilter Field:=9, Criteria1:=Array("KPI", "CPI", "GPI"), Operator:=xlFilterValues
For Each rngCell In Range("I2:I" & Range("I2").End(xlDown).Row)
If Not rngCell.EntireRow.Hidden Then
rngCell.Value = "NBS"
End If
Next rngCell
Selection.AutoFilter
Range("N2").Select
i = Range(Selection, Selection.End(xlDown)).Count + 1
Range("P2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-2],""mmmm"")"
Range("P2").Select
Selection.AutoFill Destination:=Range(Cells(2, 16), Cells(i, 16))
Range(Cells(2, 16), Cells(i, 16)).Select
Selection.Copy
Range("N2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Cells(2, 16), Cells(i, 16)).Delete
Range("C2:M2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("NEW Consolidated Data Point System_Final.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Compliance.csv").Activate
Range(Cells(2, 15), Cells(i, 15)).Copy
Windows("NEW Consolidated Data Point System_Final.xlsm").Activate
Cells(b, 12).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Compliance.csv").Activate
Range(Cells(2, 14), Cells(i, 14)).Copy
Windows("NEW Consolidated Data Point System_Final.xlsm").Activate
Cells(b, 13).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A2").Select
A = Range(Selection, Selection.End(xlDown)).Count + 1
Range("N2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-4]="""","""",IF(RC[-2]=""NA"",""N/A"",IF(RC[-2]=""N/A"",""N/A"",IF(RC[-2]>=RC[-4],""Met"",""Not Met""))))"
Selection.AutoFill Destination:=Range(Cells(2, 14), Cells(A, 14))
Range("O2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""","""",IF(RC[-1]=""NA"",""N/A"",IF(RC[-1]=""Met"",1,0)))"
Selection.AutoFill Destination:=Range(Cells(2, 15), Cells(A, 15))
Range("P2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-2]="""","""",IF(RC[-2]=""NA"",0,IF(RC[-2]=""N/A"",0,1)))"
Selection.AutoFill Destination:=Range(Cells(2, 16), Cells(A, 16))
DataArea = "database_2!R1C1:R" & A & "C16"
Sheets("Per Month").Select
Range("Q12").Select
ActiveSheet.PivotTables("PivotTable5").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _
, Version:=xlPivotTableVersion15)
ActiveWorkbook.RefreshAll
Sheets("Per Market").Select
Range("Q12").Select
ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _
, Version:=xlPivotTableVersion15)
ActiveWorkbook.RefreshAll
Sheets("Computation").Select
Range("Q12").Select
ActiveSheet.PivotTables("PivotTable7").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea _
, Version:=xlPivotTableVersion15)
ActiveWorkbook.RefreshAll
Sheets("database_2").Select
End Sub
A procedure to reverse formula might look like:
Sub ReverseFormula()
Dim c As Range
Dim sReversedFormula As String
For Each c In ThisWorkbook.Worksheets("TypeProperName").Range("F1:F100").Cells
Select Case c.Value
Case "AR17-Report", "CS02-Report", "HSCBD-Report", "KHG-Report"
'=IF(J2="","",IF(L2="NA","N/A",IF(L2="N/A","N/A",IF(L2<=J2,"Met","Not Met"))))
sReversedFormula = "=IF(J" & c.Row & "='',''IF(L" & c.Row & "='NA', 'N/A', IF(L" & c.Row & "<=J" & c.Row & ", 'Met', 'Not Met')))"
c.Offset(ColumnOffset:=8).Formula = sReversedFormula
Case Else
'do nothing
End Select
Next
End Sub
How to use it?
Open the file you want to change
Go to Code pane (ALT+F11)
Insert new module (Insert menu => Module)
Copy and paste above procedure
Change the code to your needs
Run (F5)
Note: Before you execute above code, do not forget to backup your file