Setting the default value based on the adjacent cell in VBA - excel

Sub Print_New()
'
' Print_New Macro
'
'
ActiveSheet.Unprotect
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1, Criteria1:="<>"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
ActiveSheet.Range("$B$7:$G$24").AutoFilter Field:=1
ActiveSheet.Protect
Sheets("Bill (1)").Copy Before:=Sheets(5)
ActiveSheet.Unprotect
Range("C8:C17,D20,E20:F20").Select
Range("E20").Activate
Selection.ClearContents
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
Range("F8").Select
Range("F8").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F9").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F10").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F11").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F12").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F13").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F14").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F15").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F16").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("F17").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]>0,1,"""")"
Range("C8").Select
ActiveSheet.Protect
ActiveWorkbook.Save
End Sub
Need a proper code instead of any "IF" formula.
When I write something in any cell in the range C8:C17, the default value 1 should be equal to the same cell in the range F8:F17. Which can be changed. And when C8:C17 is empty then F8:F17 should also be empty.

Please don't do the constant Select and ActiveCell: you might replace:
Range("G20").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
by:
Range("G20").FormulaR1C1 = "=IF(RC[-2]="""","""",5%)"
And, instead of using RC, you might do the following:
Range("G20").Formula = "=IF(Offset(-2;0)="""","""",5%)"
In top of this, you can use the whole range of F8:F17:
Range("F8:F17").Formula = "IF(Offset(-3;0)>0,1,"""")"
This is already a big decrease of obsolete code.

Related

Calculate cp & cpk

I have recorded a macro to avoid repletion of same task ,
but the first average formula is getting disappeared after execution , please look into the program and kindly help where to correct.
**Actual steps what i want to execute:
Average of column c, min of column c, max of column c, average of min & max (all 4 adjacent cells p1,q1,r1,s1)
Standard deviation of column c ( cell : p2)
6 * standard deviation (cell : p3)
ABS(p1 - s1)/0.31 (cell : p4)
0.62/(p3) (cell : p5)
p5*(1-p4)**
Sub Macro5()
'
' Macro5 Macro
'
' Keyboard Shortcut: Ctrl+t
'
ActiveCell.FormulaR1C1 = "=AVERAGE(C[-13])"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "=MIN(C[-14])"
Range("R1").Select
ActiveCell.FormulaR1C1 = "=MAX(C[-15])"
Range("S1").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-2]:RC[-1])"
Range("P2").Select
ActiveCell.FormulaR1C1 = "=STDEV(C[-13])"
Range("P3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=6*R[-1]C"
Range("P4").Select
ActiveCell.FormulaR1C1 = "=ABS(R[-3]C-R[-3]C[3])/0.31"
Range("P5").Select
ActiveCell.FormulaR1C1 = "=0.62/R[-2]C"
Range("P6").Select
ActiveCell.FormulaR1C1 = "=R[-1]C*(1-R[-2]C)"
Range("P5").Select
End Sub
Following the logic of the generated code (i.e. without any optimisation), you need to first select cell P1. In your code the average of column C is entered in the selected cell just before you execute the code.
Try this
Sub Macro5()
'
' Macro5 Macro
'
' Keyboard Shortcut: Ctrl+t
'
Range("P1").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(C[-13])"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "=MIN(C[-14])"
Range("R1").Select
ActiveCell.FormulaR1C1 = "=MAX(C[-15])"
Range("S1").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-2]:RC[-1])"
Range("P2").Select
ActiveCell.FormulaR1C1 = "=STDEV(C[-13])"
Range("P3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=6*R[-1]C"
Range("P4").Select
ActiveCell.FormulaR1C1 = "=ABS(R[-3]C-R[-3]C[3])/0.31"
Range("P5").Select
ActiveCell.FormulaR1C1 = "=0.62/R[-2]C"
Range("P6").Select
ActiveCell.FormulaR1C1 = "=R[-1]C*(1-R[-2]C)"
Range("P5").Select
End Sub

Optimize slow VBA response

I need to optimize the vba code used for database updates in my spreadsheet. It would be great if I can get the maximum optimized code. This macro will put currency conversions directly on the report & having to calculate them (The "Exchanges" tab that this report creates uses historical exchange rates )
Sub ZKDP5M()
Application.ScreenUpdating = False
'Add New sheet for marketplace and currency conversions
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Name = "Exchange Rates"
Range("A1").Select
' Add Marketplaces and Exchange Rates under a new tab
' Update the exchange rates on this tab with the exchange rates under the "Payment" tab of your KDP Report each month to get accurate royalties.
Range("A1").Select
ActiveCell.FormulaR1C1 = "Marketplace"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Conversion Rate"
Range("B2").Select
Columns("A:A").EntireColumn.AutoFit
Range("A2").Select
ActiveCell.FormulaR1C1 = "US"
Range("B2").Select
ActiveCell.FormulaR1C1 = "1"
Range("A3").Select
ActiveCell.FormulaR1C1 = "UK"
Range("B3").Select
ActiveCell.FormulaR1C1 = "1.35"
Range("A4").Select
ActiveCell.FormulaR1C1 = "DE"
Range("B4").Select
ActiveCell.FormulaR1C1 = "1.12"
Range("A5").Select
ActiveCell.FormulaR1C1 = "JP"
Range("B5").Select
ActiveCell.FormulaR1C1 = "0.01"
Range("A6").Select
ActiveCell.FormulaR1C1 = "CA"
Range("B6").Select
ActiveCell.FormulaR1C1 = "0.76"
Range("A7").Select
ActiveCell.FormulaR1C1 = "IT"
Range("B7").Select
ActiveCell.FormulaR1C1 = "1.12"
Range("A8").Select
ActiveCell.FormulaR1C1 = "ES"
Range("B8").Select
ActiveCell.FormulaR1C1 = "1.11"
Range("A9").Select
ActiveCell.FormulaR1C1 = "FR"
Range("B9").Select
ActiveCell.FormulaR1C1 = "1.68"
Range("A10").Select
ActiveCell.FormulaR1C1 = "NL"
Range("B10").Select
ActiveCell.FormulaR1C1 = "1.12"
Range("A11").Select
ActiveCell.FormulaR1C1 = "IN"
Range("B11").Select
ActiveCell.FormulaR1C1 = "0.01"
Range("A12").Select
ActiveCell.FormulaR1C1 = "AU"
Range("B12").Select
ActiveCell.FormulaR1C1 = "0.72"
Range("A13").Select
ActiveCell.FormulaR1C1 = "BR"
Range("B13").Select
ActiveCell.FormulaR1C1 = "0.26"
Range("A14").Select
ActiveCell.FormulaR1C1 = "MX"
Range("B14").Select
ActiveCell.FormulaR1C1 = "0.05"
Range("B15").Select
Columns("B:B").EntireColumn.AutoFit
Range("A1:B1").Select
Selection.Font.Bold = True
'Preserve Data
'This preserves the original data in your report in case you need it.
Sheets("KENP Read").Select
Sheets.Add
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "KENPBeforeConversion"
Sheets("KENP Read").Select
Range("A1:G2").Select
Selection.Copy
Sheets("KENPBeforeConversion").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("KENP Read").Select
Range("E20").Select
Sheets("KENP Read").Select
Application.CutCopyMode = False
'Columns Work
'I included a "Retailer" column for my own purposes, but this may be useful to you if you want to aggregate this report onto a master spreadssheet. You can simply delete or hide if you do not need it.
Range("A1").Select
Selection.EntireRow.Delete
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Retailer"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Month"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Year"
Columns("B:C").Select
Columns("B:C").EntireColumn.AutoFit
Range("A2").Select
Columns("A:A").ColumnWidth = 20.05
Range("H1").Select
ActiveCell.FormulaR1C1 = "KENP"
Range("K1").Select
ActiveCell.FormulaR1C1 = "GBP Conversion"
Range("L1").Select
ActiveCell.FormulaR1C1 = "DE Conversion"
Range("M1").Select
ActiveCell.FormulaR1C1 = "JPY Conversion"
Range("N1").Select
ActiveCell.FormulaR1C1 = "CAD Conversion"
Range("O1").Select
ActiveCell.FormulaR1C1 = "EUR IT Conversion"
Range("P1").Select
ActiveCell.FormulaR1C1 = "EUR ES Conversion"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "EUR FR Conversion"
Range("R1").Select
ActiveCell.FormulaR1C1 = "EUR NL Conversion"
Range("S1").Select
ActiveCell.FormulaR1C1 = "INR Conversion"
Range("T1").Select
ActiveCell.FormulaR1C1 = "AUD Conversion"
Range("U1").Select
ActiveCell.FormulaR1C1 = "BRL Conversion"
Range("V1").Select
ActiveCell.FormulaR1C1 = "MXN Conversion"
Range("W1").Select
ActiveCell.FormulaR1C1 = "USD Conversion"
Range("X1").Select
ActiveCell.FormulaR1C1 = "AdjustedIncome"
Range("Y1").Select
ActiveCell.FormulaR1C1 = "AdjustedCurrency"
Columns("K:Y").Select
Range("Y1").Activate
Columns("K:Y").EntireColumn.AutoFit
Columns("D:D").ColumnWidth = 37.36
'Currency Conversions
'GBP Currency Conversion
Range("K2").Select
ActiveCell.Formula = "=IF(G2=""UK"",'Exchange Rates'!$B$3*I2,0)"
Range("K2").Select
Range("K2").AutoFill Destination:=Range("K2:K1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'DE Currency Conversion
Range("L2").Select
ActiveCell.Formula = "=IF(G2=""DE"",'Exchange Rates'!$B$4*I2,0)"
Range("L2").Select
Range("L2").AutoFill Destination:=Range("L2:L1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'JPY Currency Conversion
Range("M2").Select
ActiveCell.Formula = "=IF(G2=""JP"",'Exchange Rates'!$B$5*I2,0)"
Range("M2").Select
Range("M2").AutoFill Destination:=Range("M2:M1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'CAD Currency Conversion
Range("N2").Select
ActiveCell.Formula = "=IF(G2=""CA"",'Exchange Rates'!$B$6*I2,0)"
Range("N2").Select
Range("N2").AutoFill Destination:=Range("N2:N1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'EUR IT Currency Conversion
Range("O2").Select
ActiveCell.Formula = "=IF(G2=""IT"",'Exchange Rates'!$B$7*I2,0)"
Range("O2").Select
Range("O2").AutoFill Destination:=Range("O2:O1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'EUR ES Currency Converter
Range("P2").Select
ActiveCell.Formula = "=IF(G2=""ES"",'Exchange Rates'!$B$8*I2,0)"
Range("P2").Select
Range("P2").AutoFill Destination:=Range("P2:P1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'EUR FR Currency Conversion
Range("Q2").Select
ActiveCell.Formula = "=IF(G2=""FR"",'Exchange Rates'!$B$9*I2,0)"
Range("Q2").Select
Range("Q2").AutoFill Destination:=Range("Q2:Q1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'EUR NL Currency Conversion
Range("R2").Select
ActiveCell.Formula = "=IF(G2=""NL"",'Exchange Rates'!$B$10*I2,0)"
Range("R2").Select
Range("R2").AutoFill Destination:=Range("R2:R1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'INR Currency Conversion
Range("S2").Select
ActiveCell.Formula = "=IF(G2=""IN"",'Exchange Rates'!$B$11*I2,0)"
Range("S2").Select
Range("S2").AutoFill Destination:=Range("S2:S1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'AUD Currency Conversion
Range("T2").Select
ActiveCell.Formula = "=IF(G2=""AU"",'Exchange Rates'!$B$12*I2,0)"
Range("T2").Select
Range("T2").AutoFill Destination:=Range("T2:T1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'BRL Currency Conversion
Range("U2").Select
ActiveCell.Formula = "=IF(G2=""BR"",'Exchange Rates'!$B$13*I2,0)"
Range("U2").Select
Range("U2").AutoFill Destination:=Range("U2:U1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'MXN Currency Conversion
Range("V2").Select
ActiveCell.Formula = "=IF(G2=""MX"",'Exchange Rates'!$B$14*I2,0)"
Range("V2").Select
Range("V2").AutoFill Destination:=Range("V2:V1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'USD Currency Conversion
Range("W2").Select
ActiveCell.Formula = "=IF(G2=""US"",'Exchange Rates'!$B$2*I2,0)"
Range("W2").Select
Range("W2").AutoFill Destination:=Range("W2:W1000" & Cells(Rows.Count, "D").End(xlUp).Row)
'Find and Replace illegal characters
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
fndList = Array("N/A", "Amazon.com.au", "Amazon.com.br", "Amazon.com.mx", "Amazon.com", "Amazon.co.uk", "Amazon.co.jp", "Amazon.ca", "Amazon.it", "Amazon.fr", "Amazon.es", "Amazon.nl", "Amazon.in", "Amazon.de")
rplcList = Array("0", "AU", "BR", "MX", "US", "UK", "JP", "CA", "IT", "FR", "ES", "NL", "IN", "DE")
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
'Royalty Month autofill
'Get filename
Range("AH2") = "=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-1)"
Range("AH2").Select
Selection.Copy
Range("AI2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("AJ2").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-1],27,4)"
Range("AJ2").Select
Selection.Copy
Range("C2:C1000").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'RoyaltyYear Autofill
Range("AK2").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-2],32,2)"
Range("AK2").Select
Selection.Copy
Range("B2:B1000").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Retailer Column Autofill
Range("A2").Select
ActiveCell.FormulaR1C1 = "Kindle Unlimited"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A1000"), Type:=xlFillDefault
Range("A2").Select
'Delete unnecessary cells
Range("AH2:AK2").Select
Selection.ClearContents
'Final Cleanup work
Columns("K:K").Select
Selection.ColumnWidth = 22.57
Columns("L:W").Select
Selection.NumberFormat = "$#,##0.00"
Columns("K:K").Select
Selection.Style = "Currency"
Selection.NumberFormat = "$#,##0.00"
Range("W2").Select
Range("X2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-13]:RC[-1])"
Range("X2").Select
Selection.AutoFill Destination:=Range("X2:X48"), Type:=xlFillDefault
Range("X2:X48").Select
Range("Y2").Select
ActiveCell.FormulaR1C1 = "USD"
Range("Y2").Select
Selection.AutoFill Destination:=Range("Y2:Y48"), Type:=xlFillDefault
Columns("K:W").Select
Selection.EntireColumn.Hidden = True
'Delete empty rows in Retailer Column if Title Column is blank
On Error Resume Next
Columns("D").SpecialCells(xlBlanks).EntireRow.Delete
'Delete Adjusted Currency Column as no longer needed
Columns("AG:AG").Select
Selection.Delete Shift:=xlToLeft
'Format KENP column as General text
Columns("H").Select
Selection.NumberFormat = "General"
'Save as CSV to directory
'ActiveWorkbook.SaveAs FileName:= _
'"C:\Royalties\KDP\KDP.csv", FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = False
Dim fn As String
Dim l As Long
Dim wb As Workbook
Set wb = ActiveWorkbook
fn = wb.FullName
l = InStrRev(fn, ".")
fn = Left(fn, l)
fn = fn & "csv"
wb.SaveAs fileName:=fn, FileFormat:=xlCSV
Application.DisplayAlerts = False
Range("A2").Select
End Sub
Here are two advices:
1: The comment from barvobot is a good approach:
Add the following code at the beginn of your sub to deactivate calculation and screen updating:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
At the end of your sub activate the calculation and screen updating again:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
This will make your code faster.
2: You are working a lot with .select. I think this is not a good approach. Here is my suggestion:
Use a Worksheet-variable for your new sheet. So replace your code:
'Add New sheet for marketplace and currency conversions
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Name = "Exchange Rates"
Range("A1").Select
with:
Dim ws As Worksheet
Set ws = Sheets.Add(After:=ActiveSheet)
ws.Name = "Exchange Rates"
Use the Worksheet-variable to set values and replace the .select. Here is an example:
replace:
Range("A2").Select
ActiveCell.FormulaR1C1 = "US"
Range("B2").Select
ActiveCell.FormulaR1C1 = "1"
Range("A3").Select
ActiveCell.FormulaR1C1 = "UK"
with:
ws.Range("A2").value = "US"
ws.Range("B2").value = "1"
ws.Range("A3").value = "UK"
This will reduce your code enormously.

autofill cells when selecting item in dropdown list from another cell

I'm pretty new to VBA .Here is my problem:
I have a dropdown list in A4 with ListItem1 to ListItem7.I want to autofill A6 t0 A11 when selecting ListItem1,ListItem2 ect...I've recorded the Macro:
Range("A6").Select
ActiveCell.FormulaR1C1 = "67"
Range("A7").Select
ActiveCell.FormulaR1C1 = "87"
Range("A8").Select
ActiveCell.FormulaR1C1 = "rty-u"
Range("A9").Select
ActiveCell.FormulaR1C1 = "kjty"
Range("A10").Select
ActiveCell.FormulaR1C1 = "erty"
Range("A11").Select
ActiveCell.FormulaR1C1 = "u"
Range("A6:A11").Select
Selection.ClearContents
Range("A6").Select
ActiveCell.FormulaR1C1 = "9867"
Range("A7").Select
ActiveCell.FormulaR1C1 = "4567"
Range("A8").Select
ActiveCell.FormulaR1C1 = "ghty"
Range("A9").Select
ActiveCell.FormulaR1C1 = "3454"
Range("A10").Select
ActiveCell.FormulaR1C1 = "klii"
Range("A6:A10").Select
Selection.ClearContents
Range("A6").Select
ActiveCell.FormulaR1C1 = "23567"
Range("A7").Select
ActiveCell.FormulaR1C1 = "9867"
Range("A8").Select
ActiveCell.FormulaR1C1 = "hjytrer"
Range("A9").Select
ActiveCell.FormulaR1C1 = "bnjg"
Range("A10").Select
ActiveCell.FormulaR1C1 = "987"
Range("A6:A10").Select
Selection.ClearContents
Range("A6").Select
ActiveCell.FormulaR1C1 = "56456"
Range("A7").Select
ActiveCell.FormulaR1C1 = "9876"
Range("A8").Select
ActiveCell.FormulaR1C1 = "45678"
Range("A9").Select
ActiveCell.FormulaR1C1 = "mjtyu"
Range("A10").Select
ActiveCell.FormulaR1C1 = "nbvc"
Range("A11").Select
ActiveCell.FormulaR1C1 = "fgtre"
Range("A6:A11").Select
Selection.ClearContents
End Sub
Any helps?

VBA macro to delete a row

Hey i just created a macro added headers deleted info and got data formatted
but i noticed that when i ran it for another file
it just deleted the data in that exact cell i now need to
do the same
but delete the row that the phrase sits on
imagine i had a cell a1 in other versions of the document that phrase could be in a2
my macro would only delete whats in A1
the phrase is ZFD
and whatever cell its in i need the macro to delete the entire row that phrase sits on
HELPPPPP
Sub UMR()
'
' UMR Macro
'
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "Transaction_Type"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Meter_Point_Ref"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Actual_Read_Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Source"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Reason"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Meter_Serial_Number"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Meter_ROC_Count"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Meter_Read_Verified"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serialNumber"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serial_Number"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Corrector_Uncorrected_Reading"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Corrector_Corrected_Reading"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Corrector_ROC_Count"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Corrector_Usable_IND"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Corrector_Read_Verified"
Range("A17").Select
Selection.ClearContents
Range("B17").Select
Selection.ClearContents
Columns("C:C").ColumnWidth = 8.29
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
Range("Q1").Select
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Range("R1").Select
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
Columns("O:O").EntireColumn.AutoFit
ActiveWindow.SmallScroll Down:=6
ActiveWindow.SmallScroll ToRight:=-9
ActiveWindow.SmallScroll Down:=-88
End Sub
As I just did have the time I reorganized your code a little. Be aware that this is not commonly done here on stackoverflow. For next time: At least try to code something, if it's wrong that's not a problem, that's where we can help. And for your information: I am quite the newby as well (3,5 months of vba so far), so it's not that hard. Even if my code is not perfected yet, most of the time I can get it to work somehow...
Try this once (read the comments in the code first):
Sub UMR()
Dim WS As worksheet
Set WS = AcitveWorkbook.ActiveWorksheet 'be aware this will always be run on the activesheet
Dim Values AS Variant
Values = Array("Transaction_Type", "Meter_Point_Ref", "Actual_Read_Date", "Meter_Reading_Source", "Meter_Reading_Reason", "Meter_Serial_Number", "Meter_Reading", "Meter_ROC_Count", "Meter_Read_Verified", "Corrector_serialNumber", "Corrector_serial_Number", "Corrector_Uncorrected_Reading", "Corrector_Corrected_Reading", "Corrector_ROC_Count", "Corrector_Usable_IND", "Corrector_Read_Verified")
Dim FindString As String
FindString = "ZFD"
Dim ZFDVal As Variant
Dim IRow As Integer
Dim ICol As Integer
Set ZFDVal = Ws.Find(What:=FindString, _
After:=Ws.Cells(Ws.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _ 'If the value is only a part of a cell it would be xlPart instead of xlWhole
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False) 'If you want it to Match the string exactly (regarding capital letters) you'll have to set this to true
IRow = Range(ZFDVal.Adress).Row 'This is untested...
For ICol = 1 To (UBound(Values)-LBound(Values))
Ws.Cells(IRow, ICol) = Values(ICol-1)
Next ICol
Range("A17").Clear ' I believe this was unintendet and just recorded alongside so you can delete these two rows...
Range("B17").Clear
Columns("A:O").EntireColumn.AutoFit
End Sub
If you get a run-time error please press "debug" and comment which line gets marked yellow. This way we can help you correcting the code...

Populate All Rows with Values with Formula

I was hoping to get help with one last tweak to this code. It works just fine with two extra manual steps, but I would love to make it all automatic with the Macros. In the last paragraph, there is a formula that I would like to be included in as many rows as there are rows with values in them, not just until Row 244.
Sub GLMacro2()
' Shortcut Ctrl+Shift+H
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
Rows(ActiveCell.Row).Delete
Range("N1").Select
ActiveCell.FormulaR1C1 = "Balance"
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Range("A1").Select
Columns("B:B").Select
Selection.ColumnWidth = 12
Columns("C:C").Select
Selection.ColumnWidth = 12
Columns("H:H").Select
Selection.ColumnWidth = 42.57
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("G2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Selection.Subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(12, 13), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlBelow
.SummaryColumn = xlLeft
End With
Selection.ApplyOutlineStyles
Columns("L:N").Select
Selection.Style = "Comma"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 49
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 2
Selection.Font.Bold = True
Range("A1").Select
' Balance
ActiveCell.Offset(1, 13).Range("A1").Select
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(C[-3]),RC[-2]-RC[-1],"""")"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A244"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A2").Select
ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit
Selection.Style = "Comma"
End Sub
Use a code similar to this:
Dim RowCount as Long
RowCount = Cells(Rows.Count,2).End(xlUp).Row
'Will get the row of the last row. Replace 2 with what ever column you want it to be bassed off. Ex: B = 2
Then in your auto-fill, what you want to do it this:
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & RowCount), Type:= _
xlFillDefault

Resources