vba code runs in f8 but not in f5 - vba

So I have looked at the same question and answer but it does not help with my problem.
here is the code
Private Sub Update_To_Search_Click()
Dim itmx As ListItem
Set itmx = ListView1.FindItem(Number_Selected.Text, lvwText) ', , lvwPartial)
If itmx Is Nothing Then
MsgBox "No Record", vbCritical
Else
ListView1.ListItems(itmx.Index).Selected = True
ListView1.SetFocus
End If
Dim myindex As Integer
Number_Selected.Text = Me.ListView1.SelectedItem
myindex = Me.ListView1.SelectedItem.Index
TextBox2.Text = Me.ListView1.ListItems.Item(myindex).SubItems(1)
TextBox3.Text = Me.ListView1.ListItems.Item(myindex).SubItems(2)
TextBox4.Text = Me.ListView1.ListItems.Item(myindex).SubItems(3)
TextBox5.Text = Me.ListView1.ListItems.Item(myindex).SubItems(4)
TextBox6.Text = Me.ListView1.ListItems.Item(myindex).SubItems(5)
TextBox7.Text = Me.ListView1.ListItems.Item(myindex).SubItems(6)
TextBox8.Text = Me.ListView1.ListItems.Item(myindex).SubItems(7)
TextBox9.Text = Me.ListView1.ListItems.Item(myindex).SubItems(8)
TextBox10.Text = Me.ListView1.ListItems.Item(myindex).SubItems(9)
'Go get the selected line
Dim Base As Worksheet, GoodData As Worksheet
Dim Rng As Range
Set GoodData = Sheets("GoodDBData")
Set Base = Sheets("Data")
Set wb = Workbooks("Staffing LogV1.7.xlsm")
Set listview = wb.Sheets("ListView")
Set fromsearch = wb.Sheets("FromDB")
Set Rng = Base.Range("A20:A28")
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
filename = "DB.xlsx"
Application.ScreenUpdating = False
Set DB = Workbooks.Open(FilePath & "\" & filename)
Application.ScreenUpdating = True
Rng.Copy
DB.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
With DB.Sheets("DB")
With .Rows(1)
Selection.AutoFilter
Selection.AutoFilter
End With
End With
Dim rCrit1 As Range, rCrit2 As Range, rCrit3 As Range, rCrit4 As Range, rCrit5 As Range, rCrit6 As Range, rCrit7 As Range, rCrit8 As Range
Dim rRng1 As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rCrit1 = Sheets("Search Criteria").Range("A2")
Set rCrit2 = Sheets("Search Criteria").Range("B2")
Set rCrit3 = Sheets("Search Criteria").Range("C2")
Set rCrit4 = Sheets("Search Criteria").Range("D2")
Set rCrit5 = Sheets("Search Criteria").Range("E2")
Set rCrit6 = Sheets("Search Criteria").Range("F2")
Set rCrit7 = Sheets("Search Criteria").Range("G2")
Set rCrit8 = Sheets("Search Criteria").Range("H2")
Set rRng1 = Sheets("DB").Range("A1").CurrentRegion
With rRng1
If rCrit1.Value <> "" Then
.AutoFilter field:=11, Criteria1:=rCrit1.Value, Operator:=xlOr
End If
If rCrit2.Value <> "" Then
.AutoFilter field:=7, Criteria1:=rCrit2.Value, Operator:=xlOr
End If
If rCrit3.Value <> "" Then
.AutoFilter field:=13, Criteria1:=rCrit3.Value, Operator:=xlOr
End If
If rCrit4.Value <> "" Then
.AutoFilter field:=14, Criteria1:=rCrit4.Value, Operator:=xlOr
End If
If rCrit5.Value <> "" Then
.AutoFilter field:=16, Criteria1:=rCrit5.Value, Operator:=xlOr
End If
If rCrit6.Value <> "" Then
.AutoFilter field:=30, Criteria1:=rCrit6.Value, Operator:=xlOr
End If
If rCrit7.Value <> "" Then
.AutoFilter field:=32, Criteria1:=rCrit7.Value, Operator:=xlOr
End If
If rCrit8.Value <> "" Then
.AutoFilter field:=37, Criteria1:=rCrit8.Value, Operator:=xlOr
End If
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End sub
The following still does not copy and paste the criteria to look for. For some reason it only copies blanks no data is entered in Searcriteria. rangeA2.
Rng.Copy
DB.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
I'm at a lost and I'm looking for any help I could get.
Thank you very much

Check this for me.
Replace your code
Base.Select
Base.Range("A7:A15").Select
Selection.Copy
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
FileName = "DB.xlsx"
Application.ScreenUpdating = False
Set Db = Workbooks.Open(FilePath & "\" & FileName)
Application.ScreenUpdating = True
Sheets("Search Criteria").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("DB").Select
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter
With
Dim Rng As Range
Set Rng = Base.Range("A7:A15")
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
FileName = "DB.xlsx"
Application.ScreenUpdating = False
Set Db = Workbooks.Open(FilePath & "\" & FileName)
Application.ScreenUpdating = True
Rng.Copy
Db.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
With Db.Sheets("Search Criteria")
With .Rows(1)
'~~> REST OF THE CODE
End With
End With
Now Try it?

Related

Ways to improve macros execution time

I have created a macro, which runs fine but, for no reason I can explain, takes so long to finish. I have tried running the macro line to line and cannot figure out which part of the process takes so long. I can only imagine that it is the part where I delete rows based on backround color. I have built several macros with similar lines of code and the performance was way better.
Sub Pharma_Stock_Report()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim lastrow1 As Long
Dim lastrow2 As Long
Dim lastrow3 As Long
Dim cell As Range
Dim DeleteRange As Range
spath1 = Application.ThisWorkbook.Path & "\Pharma replenishment.xlsm"
spath2 = Application.ThisWorkbook.Path & "\NOT OK.xlsx"
Workbooks.Open spath1
Workbooks.Open spath2
Set ws1 = Workbooks("Pharma Stock Report.xlsm").Worksheets("Pharma Stock Report")
Set ws2 = Workbooks("Pharma replenishment.xlsm").Worksheets("Replenishment")
Set ws3 = Workbooks("NOT OK.xlsx").Worksheets("Sheet1")
ws1.Cells.Clear
lastrow1 = ws2.Range("A" & Rows.Count).End(xlUp).Row
ws2.Range("A4:G" & lastrow1).Copy
With ws1.Range("A1")
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteValues, , False, False
.PasteSpecial xlPasteFormats, , False, False
End With
Application.CutCopyMode = False
Workbooks("Pharma replenishment.xlsm").Close
lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row
For Each cell In ws1.Range("D2:D" & lastrow2)
If Not cell.Interior.ColorIndex = 2 Or cell.Interior.ColorIndex = -4142 Then
If DeleteRange Is Nothing Then
Set DeleteRange = cell
Else
Set DeleteRange = Union(DeleteRange, cell)
End If
End If
Next cell
If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete
ws3.Range("H1:J1").Copy
With ws1.Range("H1")
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteValues, , False, False
.PasteSpecial xlPasteFormats, , False, False
End With
lastrow3 = ws1.Range("D" & Rows.Count).End(xlUp).Row
ws1.Range("H2:H" & lastrow3).Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:H,3,FALSE),"""")"
With Range("H2:H" & lastrow3)
.Value = .Value
.NumberFormat = "dd/mm/yyyy"
End With
ws1.Range("I2:I" & lastrow3).Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:I,4,FALSE),"""")"
With Range("I2:I" & lastrow3)
.Value = .Value
.NumberFormat = "dd/mm/yyyy"
End With
ws1.Range("J2:J" & lastrow3).Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:J,5,FALSE),"""")"
With Range("J2:J" & lastrow3)
.Value = .Value
.NumberFormat = "dd/mm/yyyy"
End With
Application.CutCopyMode = False
Workbooks("NOT OK.xlsx").Close
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub

autofilter out of range

Hi I have the following VBA code and it fails at one place time and again.
Sub theathersplitmacro()
Dim SDrv As String
Dim DDrv As String
Dim Sfname As String
Dim Dfname As String
Dim wkbSrc As Workbook
Dim wkbDst As Workbook
Dim shtname(1 To 16) As Variant
Dim i As Integer
Dim Lastrow As Variant
Dim destination_file As String
'Dim regions As String
Dim theater As String
Dim j As Integer
For j = 2 To 9
destination_file = Workbooks("VBA Master
Copy.xlsb").Sheets("Data").Range("A" & j).Value & ".xlsb"
'regions = Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("C" &
j).Value
theater = Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("D" &
j).Value
shtname(1) = "DataQTR"
shtname(2) = "DataSWDriver"
shtname(3) = "DataMTD"
shtname(4) = "DataWeekly"
shtname(5) = "DataSoftware"
shtname(6) = "DataCloud"
shtname(7) = "DataServices"
shtname(8) = "TopCustomer"
shtname(9) = "TopDeals"
shtname(10) = "TopPartners"
shtname(11) = "DataForecast"
shtname(12) = "DataFcstCloud"
shtname(13) = "DataFcstSoftware"
shtname(14) = "DataFcstServices"
shtname(15) = "DataServicesSW"
shtname(16) = "TopCustomerDebooking"
SDrv = "C:\Users\skumawat\Documents\Explore\"
Sfname = "Theater_Bookings - New Format with formulae.xlsb"
DDrv = "C:\Users\skumawat\Documents\Explore\"
Dfname = destination_file
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
End With
Set wkbSrc = Workbooks.Open(SDrv & Sfname)
Set wkbDst = Workbooks.Open(DDrv & Dfname)
For i = 1 To 15
wkbSrc.Worksheets(shtname(i)).Activate
Lastrow = wkbSrc.Worksheets(shtname(i)).Range("k" &
Rows.Count).End(xlUp).Row
wkbSrc.Worksheets(shtname(i)).Range("A1:BZ" & Lastrow).Select
If Worksheets(shtname(i)).AutoFilterMode = True Then
wkbSrc.Worksheets(shtname(i)).AutoFilter.Sort.SortFields.Clear
End If
wkbSrc.Worksheets(shtname(i)).Range("A1:BZ" & Lastrow).Select
Selection.AutoFilter
wkbSrc.Worksheets(shtname(i)).Range("$k$1:$k$" & Lastrow).AutoFilter
Field:=11, Criteria1:=theater
Range("$A$1:$BZ$" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
wkbDst.Worksheets(shtname(i)).Range("A1").PasteSpecial
xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
Next i
wkbDst.Worksheets("aMapping").Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Workbooks("VBA Master Copy.xlsb").Sheets("Data").Activate
Range("G" & j).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
wkbDst.Worksheets("aMapping").Activate
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=True
Application.CutCopyMode = False
With wkbDst
.Save
.Close
End With
Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("E" & j).Value =
"Completed"
Next j
With wkbSrc
.Close
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
End With
Workbooks("VBA Master Copy.xlsb").Activate
End Sub
The error I get is in the following line
wkbSrc.Worksheets(shtname(i)).Range("$k$1:$k$" & Lastrow).AutoFilter
Field:=11, Criteria1:=theater
You use a wrong range. To set the autofilter user your range "A1:BZ" & lastrow again. If you use only $K you have no column 11 for your criteria.

Pastespecial method of range class failed - Excel VBA

I need you help. I really do not understand what is the wrong with my code. I always get an error message at this point:
Sheets.Add(After:=Sheets(Sheets.Count)).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
The error message is that: Pastespecial method of range class failed
What I want is to filter the raw data and copy the result to a new sheet in the workbook.
Do you have any suggestion what is the problem with my code?
Thank you for your help in advance!
Sub copypaste()
Dim i, j, v As Long
Dim vSearchCols As Variant
Dim vCols As Variant
Dim FilterFor As String
FilterFor = "=AF*"
Set s1 = ThisWorkbook.Worksheets("RAW DATA")
Set s2 = ThisWorkbook.Worksheets("AF SITE TYPE")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
With s1
vSearchCols = Array("Prefix+short name", "Site type", "Probe Id", "Owner", "SLA Target", "Avg RTT (ms)", "Completion (ms)")
ReDim vCols(0 To UBound(vSearchCols))
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = .rows(2).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
End With
With s1
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells.Resize(.rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
.Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ActiveSheet.Name = "TEMP"
End If
End With
End With
End Sub
This should work:
Dim ws As Worksheet
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = "TEMP"
With s1
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
.Copy
ws.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
End With
End With
Or some completely revised code:
Sub copypaste()
Dim i, j, v As Long
Dim vSearchCols As Variant
Dim vCols As Variant
Dim FilterFor As String
Dim ws As Worksheet
Dim s1 As Worksheet
Dim s2 As Worksheet
FilterFor = "=AF*"
With ThisWorkbook
Set s1 = .Worksheets("RAW DATA")
Set s2 = .Worksheets("AF SITE TYPE")
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
ws.Name = "TEMP"
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
vSearchCols = Array("Prefix+short name", "Site type", "Probe Id", "Owner", "SLA Target", "Avg RTT (ms)", "Completion (ms)")
ReDim vCols(0 To UBound(vSearchCols))
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = s1.Rows(2).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
With s1
.AutoFilterMode = False
With .Range("A1").CurrentRegion
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
ws.Range("A1").PasteSpecial Paste:=xlPasteValues
End If
End With
End With
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

VBA Error 1004: PasteSpecial method of range class failed

I'm having a bit of trouble with any kind of paste method I use at the moment.
Data from one sheet must be cut and pasted to another, but I'm not sure what I'm missing.
The error occurs here, shortly after the commented "HERE" :
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Full code can be found below, thanks for any replies.
Option Explicit
Public Sub Workbook_Open()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wb As Variant
Dim wsName As Variant
Dim blastrow As Variant
Dim flastrow As Variant
Dim lastrow As Variant
ActiveWorkbook.Sheets("combined").Select
Range("A1:U9999").ClearContents
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\")
'file level loop
While (file <> "")
If InStr(file, ".xlsx") > 0 Then
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\" & file
wb = ActiveWorkbook.Name
'ws = ActiveSheet.Name
Dim ws As Worksheet
'worksheet/tab level loop
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
wsName = ws.Name
'andrew code (09/12/2015)
blastrow = Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & Rows.Count).End(xlUp).Row + 1
If blastrow = 2 Then blastrow = 1
Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & blastrow & ":XFD" & blastrow).Value = _
Workbooks(wb).Worksheets(wsName).Range("A1:XFD1").Value
lastrow = Range("A" & Rows.Count).End(xlUp).Row
'finding status column
Range("M1").Select
Do Until ActiveCell.Value = "Status" Or ActiveCell.Column > 100
If Range("A2") = "" Then
GoTo there
End If
ActiveCell.Offset(0, 1).Select
Loop
'looping through
Do Until ActiveCell.Row > lastrow
If ActiveCell.Value = "Solved" Then 'HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
wb = ActiveWorkbook.Name
wb = Replace(wb, ".xlsx", "")
ActiveCell.EntireRow.Cut
Workbooks("copy of merge.xlsb").Activate
'find matching company
Range("E1").Select
While ActiveCell.Value <> "CoName"
ActiveCell.Offset(0, 1).Select
Wend
Do Until ActiveCell.Value = wb
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
ActiveCell.EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Loop
'first cell in row select
ActiveSheet.Cells(ActiveCell.Row, 1).Select
'find matching ws
If ws = "Be Wiser" Then
Do Until ActiveCell.Value = "BW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Insure Wiser" Then
Do Until ActiveCell.Value = "IW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Call Wiser" Then
Do Until ActiveCell.Value = "CW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Quote Wiser" Then
Do Until ActiveCell.Value = "QW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Be Wiser Business" Then
Do Until ActiveCell.Value = "BWB"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Younger But Wiser" Then
Do Until ActiveCell.Value = "YBW"
ActiveCell.Offset(1, 0).Select
Loop
End If
'insert row and paste
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1
'Range("A" & lastrow).Select
'ActiveSheet.Paste
ws.Activate
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Cells.Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A2:A19" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:U" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("M1").Select
Do Until ActiveCell.Value = "Status" Or ActiveCell.Column > 100
ActiveCell.Offset(0, 1).Select
Loop
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
there:
'here
flastrow = Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
If blastrow = flastrow Then
Workbooks("Copy of merge.xlsb").Worksheets("Combined").Activate
Range("A" & blastrow).Select
ActiveCell.EntireRow.Delete
Workbooks(wb).Worksheets(wsName).Activate
End If
Next ws
Workbooks(wb).Close False
End If
file = Dir
Wend
Call storeFileNames
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
As has been noted, you really ought to rewrite this, but as a quick fix, add a range variable:
Dim rgCut as Excel.Range
then instead of this:
ActiveCell.EntireRow.Cut
use:
set rgCut = ActiveCell.EntireRow
and then replace this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
with this:
rgCut.Cut Destination:=Selection.Cells(1)

Copy cells between worksheets

I need the macro to open wkbk(B) goto row (??) based value entered in wkbk(A) copy certain colmns and paste back to col (j14) in wkbk (A).
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Application.ScreenUpdating = False
strPath = "\\"
Set wkbkSource = Workbooks.Open(strPath & Range("A13").Value & ".xls?")
Windows("Book1.xlsm").Activate
Set myRange = Range("i14:i25")
For Each c In myRange
i = c.Value
wkbkSource.Activate
Worksheets("Main Data").Select
Range("D" & i & ":O" & i).Select
Selection.Copy
Windows("Book1.xlsm").Activate
Range("J14").Select
Sheets("Data").Cells(Rows.Count, 9).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("J14").Select
Application.CutCopyMode = False
Next
wkbkSource.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
This will do it
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
strPath = "C:\temp\"
Set wkbkA = ThisWorkbook
Set wkbkB = Workbooks.Open(strPath & Range("A13").Value & ".xlsx")
Set myRange = wkbkA.Sheets("Sheet2").Range("i14:i25")
offs = 0
For Each c In myRange
i = c.Value
wkbkB.Worksheets("Main Data").Range("D" & i & ":O" & i).Copy
wkbkA.Sheets("Data").Range("J14").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
offs = offs + 1
Next
wkbkB.Close savechanges:=False
Application.ScreenUpdating = True
End Sub