Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / April 2008

Tip: Looking for answers? Try searching our database.

Type Mismatch Error - Copying Data between Workbooks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NumberCruncher13 - 09 Apr 2008 16:54 GMT
Can someone let me know why I get a type mismatch error when the
following code is ran?

Sub CopyFromLSS()

   Dim MyDir As Range, lsFile As Range, csFile As Range
   Dim wsTo As Worksheet, wsFrom As Worksheet

   Set MyDir = Sheets("WB").Range("B2")  'C:\06\
   Set csFile = Sheets("WB").Range("B3") 'CS.xls
   Set lsFile = Sheets("WB").Range("B4") 'LS.xls

   Set wsFrom = Workbooks(lsFile).Worksheets("Sheet1") 'Why is there
a type mismatch error?
   Set wsTo = Workbooks("Copy and Paste.xls").Worksheets("Sheet2")

   With wsFrom
       .Range("A1:Z1057").Copy
       wsTo.Range("A1").PasteSpecial Paste:=xlPasteValues
        'etc
   End With
    Application.CutCopyMode = False

    Workbooks(lsFile).Close SaveChanges:=False

End Sub
FSt1 - 09 Apr 2008 17:45 GMT
hi
because you defined isfile as a range object. you then Set lsFile =
Sheets("WB").Range("B4")  which is a range, not a value. you are looking for
a value.

defined isfile as a string i.e.
dim isfile as string
and don't set it or you'll bet an object error.
isfile = range("B4")      'should do it.
and do the above will all the other range object you declared.
remember you are not looking for a range address. you are looking for the
value in the cell which is a string.

Regards
FSt1

> Can someone let me know why I get a type mismatch error when the
> following code is ran?
[quoted text clipped - 22 lines]
>
> End Sub
NumberCruncher13 - 09 Apr 2008 17:56 GMT
> hi
> because you defined isfile as a range object. you then Set lsFile =
[quoted text clipped - 40 lines]
>
> - Show quoted text -

Thanks so much!  That worked.  I was always confused when to use the
set statement.
George Nicholson - 09 Apr 2008 23:16 GMT
> I was always confused when to use the set statement.

Set is for objects only. Variables of other datatypes (numbers, strings,
etc.) require it.  Once upon a time you had to use Let statements for
non-objects and Set for objects, but those days are behind us and Let is now
implied and no longer required (but allowed).

Signature

HTH,
George

On Apr 9, 12:45 pm, FSt1 <F...@discussions.microsoft.com> wrote:
> hi
> because you defined isfile as a range object. you then Set lsFile =
[quoted text clipped - 42 lines]
>
> - Show quoted text -

Thanks so much!  That worked.  I was always confused when to use the
set statement.
Dave Peterson - 09 Apr 2008 23:55 GMT
I think you left out something important <bg>.

Variables of other datatypes (numbers, strings, etc.) _do not_ require it.

> > I was always confused when to use the set statement.
>
[quoted text clipped - 57 lines]
> Thanks so much!  That worked.  I was always confused when to use the
> set statement.

Signature

Dave Peterson

George Nicholson - 11 Apr 2008 16:47 GMT
oops

Well, it was one missing word. and a short one at that. <sheepish grin>

Signature

HTH,
George

>I think you left out something important <bg>.
>
[quoted text clipped - 63 lines]
>> Thanks so much!  That worked.  I was always confused when to use the
>> set statement.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.