vba byref argument type mismatch

Apple1

Board Regular
Joined
Jan 18, 2015
Messages
121
Using Excel 2013.

When I try to call my sub CopyPasteData, I get the error ""vba byref argument type mismatch". The problem most likely is due to the passthrough value for tofile.

Can someone help, please? Thank you!


Sub Main ()

Dim ThisPeriod as string
Dim WBS As Workbook

ThisPeriod = Range("B3").Text
Set WB3 = Workbooks("Tables_" & ThisPeriod & ".xls")

Call CopyPasteData("B5:S23", WB3, "id", "B11:S29")

End Sub

Sub CopyPasteData (fromsht As String, fromrange As String, tofile As workbook, tosht As String, torange As String)

ThisWorkbook.Sheets(fromsht).Range(fromrange).Copy
tofile.Sheets(tosht).Range(torange).PasteSpecial Paste:=xlPasteValues

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You seem to be passing 4 variables, but the sub "CopyPasteData" want to receive 5 !!!!
 
Upvote 0
You seem to be passing 4 variables, but the sub "CopyPasteData" want to receive 5 !!!!

Hi

Thank you for the reply. I still get the error when I use 5 values. Thank you

Call CopyPasteData("id1","B5:S23", WB3, "id", "B11:S29")
 
Upvote 0
Although the worksheet name is different, The code is basically the same and Works for me.!!!!!

Code:
Private Sub CommandButton1_Click()
Dim ThisPeriod As String
Dim WB3 As Workbook
 ThisPeriod = Range("B3").Text
  Set WB3 = Workbooks("Thur21_9_17_" & ThisPeriod & ".xlsm")
    Call CopyPasteData("id1", "B5:S23", WB3, "id", "B11:S29")
End Sub
Sub CopyPasteData(fromsht As String, fromrange As String, tofile As Workbook, tosht As String, torange As String)
    ThisWorkbook.Sheets(fromsht).Range(fromrange).Copy
    tofile.Sheets(tosht).Range(torange).PasteSpecial Paste:=xlPasteValues
End Sub
 
Upvote 0
Although the worksheet name is different, The code is basically the same and Works for me.!!!!!

Code:
Private Sub CommandButton1_Click()
Dim ThisPeriod As String
Dim WB3 As Workbook
 ThisPeriod = Range("B3").Text
  Set WB3 = Workbooks("Thur21_9_17_" & ThisPeriod & ".xlsm")
    Call CopyPasteData("id1", "B5:S23", WB3, "id", "B11:S29")
End Sub
Sub CopyPasteData(fromsht As String, fromrange As String, tofile As Workbook, tosht As String, torange As String)
    ThisWorkbook.Sheets(fromsht).Range(fromrange).Copy
    tofile.Sheets(tosht).Range(torange).PasteSpecial Paste:=xlPasteValues
End Sub

Hi

This code is actually part of a longer code. Some other things could have interfered with
Call CopyPasteData.

What could it be? Thank yuu.
 
Upvote 0
Have you got both workbooks open.
Where does the code now break ???
You need to break each bit down to its basic constituents and make sure each is working before moving on.
i.e. can you initially "Set WS3" as an object using your address.
 
Upvote 0
Could this be your problem?
Dim ThisPeriod as string
Dim WBS As Workbook

ThisPeriod = Range("B3").Text
Set WB3 = Workbooks("Tables_" & ThisPeriod & ".xls")

Call CopyPasteData("B5:S23", WB3, "id", "B11:S29")
 
Upvote 0

Forum statistics

Threads
1,216,089
Messages
6,128,760
Members
449,466
Latest member
Peter Juhnke

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