Copying data from one file to others

Shanghai3

New Member
Joined
Dec 5, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am a beginner in Excel VBA and I would like to run the havimeteo() code, but I was not succeeded.
The Run-time error '13' appeared:Type mismatch in the following row:
Set omszrange = Workbooks(Filename1).Worksheets(Tabname1).Range("D14:J14")

Thank you for your help in advance.

Regards,

Shanghai3

VBA Code:
Option Explicit
Option Base 1
    
Sub havimeteo()


    Dim Pathname, Filename1, Tabname1, Tabname2 As String
    Dim omszallomany As Workbook
    Dim rownum As Double
    Dim omszrange As Range
    Dim Filename2 As Range
    Dim finishrange As Range
    Dim Tabname3, Tabname4 As Range
    
        
        Set Pathname = Range("A3")
        Set Filename1 = Range("B3")
        Set Tabname1 = Range("C3")
        Set Filename2 = Range("B6:B16")
        Set finishrange = Range("E3")
        Set Tabname3 = Range("C6:C16")
        Set Tabname4 = Range("D6:D16")       
        
        Workbooks.Open (Filename1)
        
        Set omszrange = Workbooks(Filename1).Worksheets(Tabname1).Range("D14:J14")
        
        
        For rownum = 1 To 11
        
            omszrange.Rows(rownum).Copy Workbooks(Filename2).Rows(rownum).Worksheets(Tabname3).finishrange
            omszrange.Rows(rownum).Copy Workbooks(Filename2).Rows(rownum).Worksheets(Tabname4).finishrange
        
        Next rownum
        
End Sub()
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to Mr Excel. The specific error you mention is due to using the wrong data type for the expression.

Instead of this:
VBA Code:
        Set omszrange = Workbooks(Filename1).Worksheets(Tabname1).Range("D14:J14")

Try this:
VBA Code:
        Set omszrange = ActiveWorkbook.Worksheets(Tabname1.Value).Range("D14:J14")

Another thing to be aware of is that when you declare a variable in this way:

VBA Code:
    Dim Pathname, Filename1, Tabname1, Tabname2 As String

Only Tabname2 is type string. The first three are type variant. If that was not your intention, then to declare all of them as string you would use this:

VBA Code:
    Dim Pathname As String, Filename1 As String, Tabname1 As String, Tabname2 As String

Similarly, only Tabname 4 is type range.
VBA Code:
   Dim Tabname3, Tabname4 As Range
 
Upvote 0
Solution
Dear rlv01,

Thank you for your valuable help.

Regards,

Shanghai3
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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