Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

mismatch when try using array for two sheets for two files

0

 Hi ,

I try  using  array  to  copy  data for  two  sheets MISSED & REPORT   from  Bridgestone Stock  Sales report(2) open file  to  COMPARE REPORTS closed file for  sheets OUTPUT & SH1 respectively   , but  it  gives  error mismatch  in  this  line 

OpenFilesFromFolder Filename, FolderPath, CopySheet, ExportSheet

why  show  this  error  guys?

Answer
Discuss

Answers

0
Selected Answer

Leopard

The problem is that your sub is told to accept four strings:

Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
                        ByVal CopyFromSheetName As String, ByVal ExportToSheetName As String)

but in your export_data sub you redefine 3 of the variables as Variants:

Sub export_data()
     Dim Filename As Variant, CopySheet As Variant, ExportSheet As Variant

(but you assign a string to Filename). VBA fails since you're trying to pass Variants (the arrays) when the OpenFilesFromFolder sub expects only Strings.

In the first attached file (newer file format), I've changed the Dim statement in export_data to read:

Sub export_data()
     Dim Filename As String, FolderPath As String, CopySheet As Variant, ExportSheet As Variant

and your sub to accept the same:

Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
                        ByVal CopyFromSheetName As Variant, ByVal ExportToSheetName As Variant)

so you pass it the data types it expects.

REVISION 25 March 2023:

In the second file attached, I've modified your second macro so it copies data from the source sheets to the destination sheets set in your arrays. I've used the method mentioned in the Discussion below (on your further coding error) to determine the sheets (e.g. CopyFromSheetName(n)) and allowing more than 2 sheets per array- should you need them:

Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
                        ByVal CopyFromSheetName As Variant, ByVal ExportToSheetName As Variant)

    Dim ExtBk           As Workbook, IntBk      As Workbook
    Dim FilePath        As String
    Dim lRow            As Long
'    Dim Rng(1 To 2)     As Range

    On Error GoTo myerror

    Set IntBk = ActiveWorkbook

             FilePath = Dir(FolderPath & ExportToFileName)

               If FilePath <> "" Then
                    Application.ScreenUpdating = False
                    Set ExtBk = Workbooks.Open(FolderPath & FilePath, 0, False)

                    ' loop through all items in the arrays
                     For n = LBound(CopyFromSheetName) To UBound(CopyFromSheetName)
                         ' choose a sheet from the array to copy
                         With IntBk.Worksheets(CopyFromSheetName(n))
                             lRow = .Cells(Rows.Count, 1).End(xlUp).Row

                             'clear any old values in a sheet:
                             With ExtBk.Worksheets(ExportToSheetName(n))
                                .Range("A2:F" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
                             End With

                             'Copy from source:
                             ExtBk.Worksheets(ExportToSheetName(n)).Range("A1:F" & lRow).Value = .Range("A1:F" & lRow).Value

                            End With
                     Next n
                   Else

                   'file not found
                   Err.Raise 53, , ExportToFileName & Chr(10) & "File Not found"

               End If

myerror:
    Application.DisplayAlerts = False
    If Not ExtBk Is Nothing Then ExtBk.Close Err = 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

I have to say that I find your code overly complicated, given you appear to be just copying sheets from one book to another. A single macro might achieve the same effect but I've stuck with your method andassume you will do more in your final macro.

Hope this fixes both problems now.

Discuss

Discussion

Hi John,
thanks, but  it  shows  another  error object doesn't support property  or  method.
if  I  disable the  handler error to  show  debug line it  will show  in this line 
 lRow = IntBk.Worksheets(CopyFromSheetName).Cells(Rows.Count, 1).End(xlUp).Row
leopard (rep: 88) Mar 23, '23 at 4:19 am
Leopard. That's a separate question really and I can't answer from my mobile phone (e.g. I can't see if IntBk is a String rather than a Workbook). I may have chance much later today to look at your new problem. 
John_Ru (rep: 6142) Mar 23, '23 at 4:47 am
Leopard. This new problem in line:
lRow = IntBk.Worksheets(CopyFromSheetName).Cells(Rows.Count, 1).End(xlUp).Row

arises since the bold text above is not a sheet name- it would be the CopySheet Variant received from the macro export_data, so the Array("MISSED", "REPORT"). That causes an error however. 

Not sure what you're trying to do but you'll need to step through the sheet names in the array so you have lines like:

lRow = IntBk.Worksheets(CopyFromSheetName(0)).Cells(Rows.Count, 1).End(xlUp).Row

and
lRow = IntBk.Worksheets(CopyFromSheetName(1)).Cells(Rows.Count, 1).End(xlUp).Row

Note that I will NOT add this explanation to my Answer (since it wasn't part of your question). I hope you will now mark my original Answer has Selected (since I answered what you first asked).
John_Ru (rep: 6142) Mar 23, '23 at 9:48 am
 I hope you will now mark my original Answer has Selected (since I answered what you first asked).
well, if  you  see you  answered me  for original  question  ok  I  wll  do  it, but  in  reality  doesn't  solve  my  question . I  gave  in my  OP  what  I  try to  do   it . export  two  sheets  from  file  to  two  sheets for  closed  file  with  using  array. your  suggestion just  fix  error  without  export  data  from file  to  closed  file  and  shows  another  error . even  your  last suggesion  doesn't work .
anyway thanks  for  your  time .
leopard (rep: 88) Mar 24, '23 at 2:32 pm
Leopard

Your question was "why  show  this  error  guys?" and I explained why.

Please remember this is a Q&A Forum- I'm not obliged to fix all your problems/ write your code (though I often do).

I don't know how you implemented my suggestion but if you don't want my help in future, that's fine by me.
John_Ru (rep: 6142) Mar 24, '23 at 2:42 pm
but if you don't want my help in future, that's fine by me.
you  misundersood  me, John  . just  I  want  clrifing    what  do  you  think  about  my  question . maybe    this  is  my  bad  to  post  what I  want and  seem  doesn't  clear  for  you . I  appreciate  for  your many assistance  in earlier  time  and  that's   help  me  so  much . the  matther  is  not  as  you  think .I hope  to  understand  this .
Next time I will try to post details without any confusion or misunderstanding .
leopard (rep: 88) Mar 24, '23 at 3:47 pm
Leopard. To be honest your question wording isn't quite clear to me. If I get chance over the weekend however, I will look again at your file. 
John_Ru (rep: 6142) Mar 24, '23 at 5:32 pm
John. for  more  detailes  the  orginal  code  should  copy data from Bridgestone Stock  Sales report(2) file  for MISSED & REPORT  sheets respectively to  COMPARE REPORTS file into OUTPUT & SH1 sheets respectively.
leopard (rep: 88) Mar 24, '23 at 10:26 pm
Leopard. Please see my revised answer and second file.
John_Ru (rep: 6142) Mar 25, '23 at 9:15 am
John,
it  works  as  I  wanted  .
again many  thanks  for  dedicating your time  to  help  me .
leopard (rep: 88) Mar 26, '23 at 4:37 am
Great, glad it worked for you, Leopard.
John_Ru (rep: 6142) Mar 26, '23 at 6:50 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login