Excel - Macro Changing sheets and Fill to End - Compile Error - excel
I am creating a Macro to run consecutively on about 25 sheets, where I am naming a cells in a specific column. I have the code for filling to end, and for changing sheets. But when I combine the codes, I am getting a warning:
Compile Error: Duplicate declaration in current scope
When I remove the DIM definition, it will run on the consecutive sheets, but only to the end of the first sheet. My goal is to have each sheet dynamically filled to end in column N with a set name for each.
Here is the code I was working on
Sub Mailbox_Name()
'
' Mailbox_Name Macro
' Adds Mailbox Name to Each Sheet
'
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("N1").Select
ActiveCell.FormulaR1C1 = "Mailbox"
Range("N2").Select
ActiveCell.FormulaR1C1 = "ACC"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N" & LR)
Range("N2:N" & LR).Select
Selection.Copy
Sheets("ACPR").Select
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("N1").Select
ActiveCell.FormulaR1C1 = "Mailbox"
Range("N2").Select
ActiveCell.FormulaR1C1 = "ACPR"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N" & LR)
Range("N2:N" & LR).Select
End Sub
Thanks for any help you can provide!
Just read what the error is telling you - you have a duplicate declaration. Sure enough, in your code you have Dim LR As Long twice.
Related
To add the excel formulas into VBA Macro with condition
I have recorded some formulas into Macros and they are functioning properly, however I am not able to update them so that they should select the range themselves where the data ends in the last end in Column C. These 3 formulas extracts Date, File Name and Status of Files from Column A. As you see for now the range is e.g. "F3 to F313" where next time if the Data in Column C is up to C500 Range than I have to manually copy and paste the formulas. Is there anyway these 3 formulas should automatically detect the last text cell from Column C and ends there. That would be much helpful. To Extract Date Sub Macro13() 'To Extract Date ActiveCell.FormulaR1C1 = "=extractDate(RC[-1])" Range("D2").Select Selection.Copy Range("D3:D313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub To Find Status of File Sub Macro15() 'To Find Status of File ActiveCell.FormulaR1C1 = _ "=IFERROR(LOOKUP(2^15,SEARCH({""Feed"",""Feed 1"",""Feed 2""},RC[-3]),{""Feed"",""Feed 1"",""Feed 2""}),""Combine"")" Range("F2").Select Selection.Copy Range("F3:F313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub To extract File Name Sub Macro17() 'To extract File Name ActiveCell.FormulaR1C1 = _ "=IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABCD - GAMA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ALPHA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9," & _ "0},RC[-2]&""1234567890""))-1))=""ABCD - BETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""DBETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""A"",LEFT(RC[-2]," & _ "MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))="""",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABETA"",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),LEF" & _ "T(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2] & ""1234567890""))-1))))))))" Range("E2").Select Selection.Copy Range("E3:E313").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub
Try this: Sub TestThis() Dim LastRow As Long, ws As Worksheet Set ws = ActiveSheet LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ws.Range("D2:D" & LastRow).FormulaR1C1 = "=extractDate(RC[-1])" ws.Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(LOOKUP(2^15,SEARCH({""Feed"",""Feed 1"",""Feed 2""},RC[-3]),{""Feed"",""Feed 1"",""Feed 2""}),""Combine"")" ws.Range("E2:E" & LastRow).FormulaR1C1 = _ "=IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABCD - GAMA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ALPHA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+2),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9," & _ "0},RC[-2]&""1234567890""))-1))=""ABCD - BETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""DBETA "",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""A"",LEFT(RC[-2]," & _ "MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))="""",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+8),IF((LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))-1))=""ABETA"",LEFT(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2]&""1234567890""))+6),LEF" & _ "T(RC[-2],MIN(FIND({1,2,3,4,5,6,7,8,9,0},RC[-2] & ""1234567890""))-1))))))))" End Sub
VBA copy paste codes does not pasting anything
Can someone please let me know why my code is not pasting anything from my source data to my destination file? The objectives of this code are to select rows that satisfy certain criteria, copy-pastes it into another workbook, The code is shown below: Sub Copy_Source_LRE() Dim LastRow As Integer, i As Integer, erow As Integer Workbooks.Open _ "C:\Users\sjiang\OneDrive - Canada Pension Plan Investment Board\Desktop\IOA Exposure\AAPAF_strategy_loadings_2019-04-01_2020-04-01 (2).csv" Worksheets("AAPAF_strategy_loadings_2019-04").Activate Set sht = ActiveSheet 'Workbooks("AAPAF_strategy_loadings_2019-04-01_2020-04-01 (2).csv").Sheets("AAPAF_strategy_loadings_2019-04").Activate LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row For i = 2 To LastRow For Each d In Array("4/1/2019", "5/1/2019", "6/3/2019", "7/1/2019", "8/1/2019", "9/2/2019", _ "10/1/2019", "11/1/2019", "12/2/2019", "1/2/2020", "2/3/2020", "3/2/2020") If Cells(i, 2) = d And Cells(i, 3) = "Real Estate" And Cells(i, 4) = "Listed Real Estate" And Cells(i, 5) = "AAPAF_SA" Then Range(Cells(i, 2), Cells(i, 12)).Select Selection.Copy Workbooks.Open _ "C:\Users\sjiang\OneDrive - Canada Pension Plan Investment Board\Desktop\IOA Exposure\pull data.xlsm" Worksheets("Sheet1").Select erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row ActiveSheet.Cells(erow, 1).Select ActiveSheet.Paste ActiveWorkbook.Save 'ActiveWorkbook.Close End If Next d Next i Application.CutCopyMode = False End Sub
This is a really easy and basic way that I use all the time to copy data into new workbooks. In this example I'm copying a named range called "MasterData" into a new blank workbook. Then I save that new book with a password and re-activate the current workbook. Dim newfilename As String newfilename = "/Users/" & userName & "/Desktop/savedWorkbook.xlsx" Dim NewBook As Workbook Set NewBook = Workbooks.Add ThisWorkbook.Activate Sheets("Datasheet").Select Range("MasterData").Copy NewBook.Activate NewBook.Sheets(1).Select Range("A1").Select Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False NewBook.SaveAs Filename:=newfilename, Password:="examplepassword", AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges NewBook.Close (True) ThisWorkbook.Activate
I've redone the code for you as the major problem was related to a loop that is not really necessary. The best/fast way to apply those criteria and extract the data is using a filter to apply them, so copy the visible cells without the hidden (unmatching) lines and then open the second file where you need to past info, find next blank line below selection and paste all lines at once. I'm pasting the code below (with comments) and also saved a zip file with 3 files (code, info, database) that might reflect your working files, link below. VBS code: Sub Copy_Source_LRE() Dim LastRow As Integer, i As Integer, erow As Integer Workbooks.Open ThisWorkbook.Path & "\" & "Wks1.xlsx" 'change the path and name here Worksheets(1).Activate Set sht = ActiveSheet LastRow = Range("a1").SpecialCells(xlCellTypeLastCell).Row datar = Range(Cells(LastRow, 12), Cells(1, 1)).Address 'data range Range(datar).Select Selection.AutoFilter 'create a filter,then use the criteria you need ActiveSheet.Range(datar).AutoFilter Field:=2, Criteria1:= _ Array("4/1/2019", "5/1/2019", "6/3/2019", "7/1/2019", "8/1/2019", "9/2/2019", _ "10/1/2019", "11/1/2019", "12/2/2019", "1/2/2020", "2/3/2020", "3/2/2020"), Operator:=xlFilterValues 'your dates array can be update here ActiveSheet.Range(datar).AutoFilter Field:=3, Criteria1:="Real Estate", Operator:=xlAnd ActiveSheet.Range(datar).AutoFilter Field:=4, Criteria1:="Listed Real Estate", Operator:=xlAnd ActiveSheet.Range(datar).AutoFilter Field:=5, Criteria1:="AAPAF_SA", Operator:=xlAnd Range(datar).Offset(1, 0).Resize(Range(datar).Rows.Count - 1, Range(datar).Columns.Count).Select 'resize selection to remove the header Selection.SpecialCells(xlCellTypeVisible).Select 'select visible cells only Selection.Copy Workbooks.Open ThisWorkbook.Path & "\" & "Wks2.xlsx" 'change the path and name here Worksheets("Sheet1").Select Range("A1").End(xlDown).Offset(1, 0).Select 'goes to the last row on column A the goes another one - 1st empty ActiveSheet.Paste ActiveWorkbook.Save ActiveWorkbook.Close SaveChanges:=True 'close and save your database Application.CutCopyMode = False ActiveWorkbook.Close SaveChanges:=False 'close without saving your csv file End Sub link to files/code: https://drive.google.com/file/d/1zL_TwclHR4lrNhKB1xODGAmliPHM1r3K/view?usp=sharing If the solution matches you need please consider as solution. Regards!
Format worksheets 5 and on, then copy&paste that info into "Sheet3" with source width and format
I am currently try to make a code that will format sheets 5 and on to module one's code and then have the program copy all the information in each of those newly formatted sheets and paste them into "sheet3" with original width and format. I have tried the "for each" and "integer" functions but can't seem to get 'the program to move past "sheet5". This sub is suppose to go through all of the sheets and and 'format them to my needs: Sub TEST2() Dim ws As Worksheet Dim wsDest As Worksheet Dim LastRow As Long Set wsDest = Sheets("sheet3") For Each ws In ActiveWorkbook.Sheets If ws.Name <> wsDest.Name And _ ws.Name <> "sheet1" And _ ws.Name <> "sheet2" And _ ws.Name <> "sheet4" Then 'code here Columns.Range("A:A,B:B,H:H,I:I").Delete Columns("A").ColumnWidth = 12 Columns("B").ColumnWidth = 17 Columns("C").ColumnWidth = 10 Columns("D").ColumnWidth = 85 Columns("E").ColumnWidth = 17 ActiveSheet.Range("D:D").WrapText = True ActiveSheet.Range("F:F").EntireColumn.Insert ActiveSheet.Range("F1").Formula = "Product ID" LastRow = Cells(Rows.Count, 1).End(xlUp).Row Range("F2:F" & LastRow).Formula = "=$G$2" ActiveSheet.Range("F2").Copy Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues End If Next ws End Sub This sub is meant to go to "sheet5" first and paste it into '"sheet3", than the second half of the sub should start at "sheet6" and go on 'until the end of the work sheets and then copy & paste onto "sheet3" with 'original width. Sub Test1() Dim sht As Worksheet Dim LastRow As Long Dim WS_Count As Integer Dim I As Integer Sheets("Sheet5").Select Application.CutCopyMode = False Application.Run "PERSONAL.XLSB!Module1.Del_move_insert_colmuns" Range("G2").Select ActiveCell.Offset(0, -1).Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToLeft)).Select Application.CutCopyMode = False Selection.Copy Sheets("Sheet3").Select Range("A1").Select ActiveSheet.Paste Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False ActiveSheet.Range("D:D").WrapText = True WS_Count = ActiveWorkbook.Worksheets.Count ' Begin the loop For I = 5 To WS_Count 'code here Sheets("Sheet6").Select Application.Run "PERSONAL.XLSB!Module1.Del_move_insert_colmuns" Application.CutCopyMode = False Range("G2").Select ActiveCell.Offset(0, -1).Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToLeft)).SelectApplication.CutCopyMode = False Selection.Copy Sheets("Sheet3").Select Range("A1").Select 'crtl shift + down Selection.End(xlDown).Select 'moves down one cell to paste ActiveCell.Offset(1, 0).Select ActiveSheet.Paste Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Next I End Sub What im getting right now is it does "sheet5" and "sheet6" fine,but after that doesn't format and on sheet there all i get is a bunch of columns with top labeled as product ID and a bunch of 0's.
A big part of your problem is that most of your code is "assuming" that you are working with a certain worksheet when you're really working with the ActiveSheet. As an example in your TEST2 routine, you're looping through all of the worksheets in the workbook, skipping certain sheets. This part works fine. But when you want to format the other sheets, you're really only working with whatever worksheet is currently active. To fix this, you should make a habit of making sure all of your Worksheet, Range, and Cells reference are always fully qualified. So then your code works like this: ws.Columns.Range("A:A,B:B,H:H,I:I").Delete ws.Columns("A").ColumnWidth = 12 ws.Columns("B").ColumnWidth = 17 ws.Columns("C").ColumnWidth = 10 ws.Columns("D").ColumnWidth = 85 ws.Columns("E").ColumnWidth = 17 ws.Range("D:D").WrapText = True ws.Range("F:F").EntireColumn.Insert ws.Range("F1").Formula = "Product ID" LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ws.Range("F2:F" & LastRow).Formula = "=$G$2" ws.Range("F2").Copy ws.Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues Notice how every single reference is locked to the same worksheet. You can take a shortcut though, by using the With statement. But you must make sure that each reference has the . in front of it to lock it back to the With object, like this: With ws .Columns.Range("A:A,B:B,H:H,I:I").Delete .Columns("A").ColumnWidth = 12 .Columns("B").ColumnWidth = 17 .Columns("C").ColumnWidth = 10 .Columns("D").ColumnWidth = 85 .Columns("E").ColumnWidth = 17 .Range("D:D").WrapText = True .Range("F:F").EntireColumn.Insert .Range("F1").Formula = "Product ID" LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row .Range("F2:F" & LastRow).Formula = "=$G$2" .Range("F2").Copy .Range("F2:F" & LastRow).PasteSpecial Paste:=xlPasteValues End With For the rest of your code, you can make improvements by avoiding the use of Select and Activate. Consider also the tips discussed in this article that will give you excellent guidance.
Macros to copy and paste in loop
For many of you this might be simple. I need to copy and paste data from 2 specific columns to a new spread sheet; but the macros need to stop when it reaches the end of data or when a cell is blank. So far the code looks like this: Workbooks.Add ActiveCell.FormulaR1C1 = _ "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C1" Range("A1").Select ActiveCell.FormulaR1C1 = _ "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC1" Range("B1").Select ActiveCell.FormulaR1C1 = _ "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C3" Range("B1").Select ActiveCell.FormulaR1C1 = _ "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC3" Range("A1:B1").Select Selection.Copy Range("A2:A101").Select ActiveSheet.Paste Range("A1").Select Windows("Muhanad_Reset_import-TEMPLATE.xlsx").Activate I know the code for copy and paste code does not work for a good result.
Do you mean code like this: Sub Test() Dim lr As Long Dim i As Long lr = ThisWorkbook.Worksheets("Reset_import-Raw Data").Cells(Rows.Count, 1).End(xlUp).Row Workbooks.Add With ActiveSheet.Range("A1:A" & lr) .Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$A1" .Offset(, 1).Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$C1" End With Windows("Muhanad_Reset_import-TEMPLATE.xlsm").Activate End Sub
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...