Reverse a formula by changing > to < - excel

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

Related

Grouping records in an Excel sheet which have the same values in one column but only one unique record in other columns

Dummy data of a tournament
Above is the example of the dummy data. My goal is to use VBA to group the data so that there is only one name displayed and the 3 Games populated with the Results so there would only be one line for the name as well as the 3 Games' results in the same line.
Example of the output data
Well, this is not as easy as first appears, however, this works:
So, the country is returned with classic index & match. The results are built by finding the result against each player and round. This expects blanks in the other cells for each player.
Try this:
Sub mSummarise()
'
' Macro1 Macro
'
'
Dim lData, lSummary, lFilter As String
Dim lRow1, lRow2, lRow3, lCol1, lCount As Long
lData = ActiveSheet.Name
Range("A1").Select
Selection.End(xlToRight).Select
lCol1 = ActiveCell.Column
Range("A1").Select
Selection.End(xlDown).Select
lRow1 = ActiveCell.Row
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Summary"
Sheets(lData).Activate
Range("A1:B" & lRow1).Select
Selection.Copy
Sheets("Summary").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$B$" & lRow1).RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo
Range("A1").Select
Selection.End(xlDown).Select
lRow2 = ActiveCell.Row
Sheets(lData).Select
Range(Cells(1, 3), Cells(1, lCol1)).Select
Selection.Copy
Sheets("Summary").Select
Range("C1").Select
ActiveSheet.Paste
Sheets(lData).Select
For lCount = 3 To lCol1
Range(Cells(1, 1), Cells(lRow1, lCol1)).Select
Selection.AutoFilter
ActiveSheet.Range(Cells(1, 1), Cells(lRow1, lCol1)).AutoFilter Field:=lCount, Criteria1:="<>", Operator:=xlAnd
Range(Cells(1, 1), Cells(lRow1, lCount)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
lFilter = ActiveSheet.Name
ActiveSheet.Paste
Range("A1").Select
Selection.End(xlDown).Select
lRow3 = ActiveCell.Row
Sheets("Summary").Select
Application.CutCopyMode = False
Cells(2, lCount).Select
ActiveCell.Formula = "=VLOOKUP(A2," & lFilter & "!$A$2:" & Cells(lRow3, lCount).Address & "," & lCount & ",0)"
Range(Cells(2, lCount), Cells(2, lCount)).Copy
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
ActiveSheet.Paste
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(lFilter).Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets(lData).Select
Next
Selection.AutoFilter
Range("A1").Select
Sheets("Summary").Select
Range("A1").Select
End Sub

VBA changes date format

I have a table with data in it and run a macro to neaten things up and then adds a hyperlink to column G but the issue is when the macro has run, the date changes from:
https://websitenamehere.com//agentView/agentname#company/2021-11-08
to
https://websitenamehere.com//agentView/agentname#company/44508
In my table, I created in column H the column for "today" and then in column G is where it puts it all together but messing up the date part.
Here is my code which i am using. Any help would be appreciated.
Sub CleanFollowUps()
Dim Lrow As Integer
Dim lCol As Integer
Dim C As Range
Lrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(Lrow, lCol)), , xlYes).Name = "FollowUps"
Range("FollowUps[#All]").Select
ActiveSheet.ListObjects("FollowUps").TableStyle = ""
Range("F1").Select
ActiveCell.FormulaR1C1 = "Helper"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=IF(SUM([#[Due today]]+[#Late])>0,""Yes"","""")"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Schedule"
Range("H1").Select
ActiveCell.FormulaR1C1 = "day"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Columns("H:H").Select
Selection.NumberFormat = "yyyy-mm-dd"
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""https://websitenamehere.com/agentView/""&[#Username]&""#company/""&[#day]"
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Sheets("FU")
For Each C In .Range("G2:G" & .Range("G" & .Rows.Count).End(xlUp).Row)
.Hyperlinks.Add Anchor:=C, Address:=C.Value, SubAddress:=C.Value
Next C
End With
Application.DisplayAlerts = True
End Sub

Why won't multiple "NextFree = Range" language work in my code?

I am writing VBA code in Excel to copy formulas from the last row of data to the row below it and then copying that last row (now second-to-last row) and paste as values in it's place. I would like to do this for multiple sheets. The problem is that after it works properly for the first sheet, it errors out on the next sheet (and presumably the rest of them).
The code works for the first worksheet but when it moves to the next sheet, Excel gives me a "Run-time error '1004': No cells were found" error message". When I debug the error, the 2nd line in the 3rd paragraph below is what gives me the problem. What do I have to do to allow this code to work for multiple worksheets in the same workbook?
Sheets("BrentSkew").Select
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("BrentSkew").Select
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False
Sheets("LLSSkew").Select
'the line below is the problem
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("LLSSkew").Select
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" &
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False

I get a Run time error when only 1 row of data is visible

I have a workbook that refreshes data connections, then filters dates from yesterday. When there is only one row that is refreshed, I will get a
Run time error 1004.
Some days the macro works, some days it doesn't. This is run daily.
Sub Get_VRIDs()
Dim i As Integer
Sheets("Cancels").Select
i = 1
With Range("E2")
If .Cells(1, 1).Value = "" Then
Else
Range(.Cells(1, 1), .End(xlDown)).Copy
Destination:=Sheets("Metric").Range("a6")
x = x + 1
End If
End With
Sheets("Adhoc").Select
i = 1
With Range("C2")
If .Cells(1, 1).Value = "" Then
Else
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Metric").Range("a94")
x = x + 1
End If
End With
Sheets("Direct Tender").Select
i = 1
With Range("B2")
If .Cells(1, 1).Value = "" Then
Else
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Metric").Range("a132")
x = x + 1
Sheets("Metric").Activate
End If
End With
Sheet1.Activate
Range("B6").Select
Selection.Copy
Range("A6:A90").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=48
Range("B60").Select
Selection.Copy
Range("A94:A128").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=24
Range("B90").Select
Selection.Copy
Range("A132:A200").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=-108
Range("A7").Select
Call Hide_Rows
End Sub
The error occurs at this line:
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Metric").Range("a94")

Paste Special code

Public Sub CopyPaste()
Dim j As Long
For j = 2 To 52
Range("AE" & j).Select
Selection.Copy
Range("AE" & j).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("AF" & j).Select
Selection.Copy
Range("AF" & j).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("AG" & j).Select
Selection.Copy
Range("AG" & j).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
Next j
End Sub
Is there any way to minimize this code. I have tried using Range("AE:AG" & j).Select, but it showing some error.
Yes, it is.
If you want to paste only values you can equals ranges values. But you need to use cells and exact sheet object. For example
Public Sub CopyPaste()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range(ws.Cells(2, 31), ws.Cells(52, 34)).Values = _
ws.Range(ws.Cells(2, 31), ws.Cells(52, 34)).Values
Set ws = Nothing
End Sub
It's always best to avoid using Select, Copy and Paste. You can almost always use .Value = ... in their place.
Assuming this is what you want to do (it isn't too clear from your question), if you ever want to replace a formulated cell with its value you can just set its value to itself:
Sub RemoveFormulas()
With ActiveSheet
.Range(.Cells(2, 31), .Cells(52, 34)).Value = _
.Range(.Cells(2, 31), .Cells(52, 34)).Value
End With
End Sub

Resources