VBA Mismatch (Error 13) - Help needed

sbooth

Board Regular
Joined
Dec 22, 2004
Messages
92
When I run the following code it produces the correct result but keeps giving me a "mismatch (Error 13)" message. Any idea on what is going wrong and how I can modify my code?

Thanks,
Steve


Sub retrievebudget()
' Assign variables
TargetWB = ActiveWorkbook.Name
TargetWS = "copiedb"
cs = ActiveSheet.Name
LR = Sheets(cs).Range("A65536").End(xlUp).Row
Application.ScreenUpdating = False
Sheets(TargetWS).Columns("A:O").Clearcontents

For i = 8 To LR
'Start at row 8
fn = Sheets(cs).Range("C" & i).Value
sn = Sheets(cs).Range("D" & i).Value
rn = Sheets(cs).Range("E" & i).Value

'Check for Source workbook
If Not Dir(fn) <> "" Then
Sheets(cs).Range("F" & i).Value = Now()
Sheets(cs).Range("G" & i).Value = "No"
GoTo NextI
End If

'Open Source workbook
Workbooks.Open fn, Password:="", WriteResPassword:="", ReadOnly:=True
SourceWB = ActiveWorkbook.Name
'Assign Target Range
TLR = Workbooks(TargetWB).Sheets("copiedb").Range("A65536").End(xlUp).Row + 1
CheckRowNo = TLR
Set TargetRange = Workbooks(TargetWB).Sheets("copiedb").Range("A" & TLR)

'Copy and PasteSpecial
Sheets(sn).Range(rn).Copy
TargetRange.PasteSpecial Paste:=xlValues
Application.CutCopyMode = False

'Close Source workbook
Workbooks(SourceWB).Close SaveChanges:=False

'Close Workbook w/o Save
Sheets(cs).Range("F" & i).Value = Now()
TLR = Workbooks(TargetWB).Sheets("copiedb").Range("A65536").End(xlUp).Row + 1
If TLR > CheckRowNo Then
Sheets(cs).Range("G" & i).Value = "Yes"
Else
Sheets(cs).Range("G" & i).Value = "No"
End If
NextI:
Next i
Sheets(cs).Select
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
It does not give me the option to debug it. I'm trying to figure out how to debug it from within VBA editor.

Steve
 
Upvote 0
I fixed the range in line 6 of the code and now it works fine. Thank you


Sub retrievebudget()
' Assign variables
TargetWB = ActiveWorkbook.Name
TargetWS = "copiedb"
cs = ActiveSheet.Name
LR = Sheets(cs).Range("B65536").End(xlUp).Row 'This range was wrong
Application.ScreenUpdating = False
Sheets(TargetWS).Columns("A:O").Clearcontents

For i = 8 To LR
'Start at row 8
fn = Sheets(cs).Range("C" & i).Value
sn = Sheets(cs).Range("D" & i).Value
rn = Sheets(cs).Range("E" & i).Value

'Check for Source workbook
If Not Dir(fn) <> "" Then
Sheets(cs).Range("F" & i).Value = Now()
Sheets(cs).Range("G" & i).Value = "No"
GoTo NextI
End If

'Open Source workbook
Workbooks.Open fn, Password:="", WriteResPassword:="", ReadOnly:=True
SourceWB = ActiveWorkbook.Name
'Assign Target Range
TLR = Workbooks(TargetWB).Sheets("copiedb").Range("A65536").End(xlUp).Row + 1
CheckRowNo = TLR
Set TargetRange = Workbooks(TargetWB).Sheets("copiedb").Range("A" & TLR)

'Copy and PasteSpecial
Sheets(sn).Range(rn).Copy
TargetRange.PasteSpecial Paste:=xlValues
Application.CutCopyMode = False

'Close Source workbook
Workbooks(SourceWB).Close SaveChanges:=False

'Close Workbook w/o Save
Sheets(cs).Range("F" & i).Value = Now()
TLR = Workbooks(TargetWB).Sheets("copiedb").Range("A65536").End(xlUp).Row + 1
If TLR > CheckRowNo Then
Sheets(cs).Range("G" & i).Value = "Yes"
Else
Sheets(cs).Range("G" & i).Value = "No"
End If
NextI:
Next i
Sheets(cs).Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top