0

I have one button macro for reading data from excel files after leaving some(irrelevent starting rows of detail) rows(A1-A10) and merging all those files in single file.

It runs correctly when i use product files(excel files which have details about particular product). But when i use excel files which has company details it reads one row from irrelevant row(A5) then goes to the relevant data part to read.

I am not able to understand why it is reading one row i.e. company name from company excel files. i want it to directly go to (A11)th row to read. Which it does with produt files.

Product files are the files which have particular product details. Whereas Company Files are the files which has details of all products of particular company.

With my code below, i want to know that why it is reading company name(row A5), which it should not read.

 Sub Button2_Click()
  Dim basebook As Workbook
  Dim mybook As Workbook
  Dim sourceRange As Range
  Dim destrange As Range
  Dim SourceRcount As Long
  Dim N As Long
  Dim rnum As Long
  Dim MyPath As String
  Dim SaveDriveDir As String
  Dim FName As Variant

  SaveDriveDir = CurDir
  MyPath = "C:\"
 ChDrive MyPath
 ChDir MyPath

 FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", _
 MultiSelect:=True)
 If IsArray(FName) Then
 Application.ScreenUpdating = False
 Set basebook = ThisWorkbook

  For N = LBound(FName) To UBound(FName)
  Set mybook = Workbooks.Open(FName(N))

  rnum = LastRow(basebook.Worksheets(1)) + 1

  Set sourceRange = mybook.Worksheets(1).UsedRange
  SourceRcount = sourceRange.Rows.Count
  Set destrange = basebook.Worksheets(1).Cells(rnum, "A")

   'basebook.Worksheets(1).Cells(rnum, "D").Value = mybook.Name
  ' This will add the workbook name in column D if you want

   sourceRange.Copy destrange
   ' Instead of this line you can use the code below to copy only the values

   ' With sourceRange
   ' Set destrange = basebook.Worksheets(1).Cells(rnum, "A"). _
   ' Resize(.Rows.Count, .Columns.Count)
   ' End With
   ' destrange.Value = sourceRange.Value

   mybook.Close False

  'Clear Rows
     rnum = LastRow(basebook.Worksheets(1)) + 1
       While Not rnum = 2
      If basebook.Worksheets(1).Cells(rnum, 1).Value = "" Or
        Left(basebook.Worksheets(1).Cells
              (rnum, 1).Value, 9) = "Copyright" Or Left                                                      
          (basebook.Worksheets(1).Cells(rnum, 1).Value, 4) = "Free" Or  Left
          (basebook.Worksheets(1).Cells(rnum, 1).Value, 7) = "Product" Or
        Left(basebook.Worksheets(1).Cells(rnum, 1).Value, 9) = "Intl Port" Or 
        Left(basebook.Worksheets(1).Cells(rnum, 1).Value, 5) = "House" Or 
       Left(basebook.Worksheets(1).Cells(rnum, 1).Value, 7) = "Arrival" Or
       Left(basebook.Worksheets(1).Cells(rnum, 1).Value, 5) = "Bill " Then
       basebook.Worksheets(1).Rows(rnum).Delete
         End If
       rnum = rnum - 1
           Wend

         Next
      End If
        ChDrive SaveDriveDir
      ChDir SaveDriveDir
     Application.ScreenUpdating = True
      End Sub

      Function LastRow(sh As Worksheet)
      On Error Resume Next
      LastRow = sh.Cells.Find(What:="*", _
     After:=sh.Range("A1"), _
        Lookat:=xlPart, _
       LookIn:=xlFormulas, _
      SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
       MatchCase:=False).Row
       On Error GoTo 0
  End Function
7
  • Both, your question and your code, are not clear at all. As far as you are expecting to get some help from us, you should put a bit of effort from side to make yourself clear. A first step might be, structuring the code properly such that it is, at least, properly tabbed (to know what is inside a condition, a loop, etc. after a quick look).
    – user2480047
    Nov 9, 2013 at 9:27
  • Sure! my problem is that i am suppose to fetch the excel files from this code. Nov 9, 2013 at 9:32
  • 1
    I see... As said: you should make the code clear enough (via tabbing, for example). And you should explain what you mean with "fetch excel files", by understanding that we don't have access to the input data, we don't know what it is now wrong or what you want to do exactly. Example of bad question: "this is my code; fix it for me". Example of good question: "with this code (properly tabbed and commented), I am analysing 1,2,3,4 and it outputs a,b,c,d; I want it to output d,c,b,a, what should I do?". Well... this is my suggestion if you want to get help from me or from any other person like me
    – user2480047
    Nov 9, 2013 at 9:35
  • but when my files are product based it is fetching the data correctly from all the files, but when i do the same with files which are company based not product bases, it is fetching name of the company from A5 row then display that then desplaying the data from A10, but when i do the same from product file, it is directly goes to the data row (A10) and correctly display the data. Nov 9, 2013 at 9:40
  • well said! Will keep that in mind from now on. Nov 9, 2013 at 9:42

1 Answer 1

1

Instead of this:

Set sourceRange = mybook.Worksheets(1).UsedRange
SourceRcount = sourceRange.Rows.Count

Try this:

With mybook.Worksheets(1)
    SourceRcount = .UsedRange.Rows.Count
    Set sourceRange = .UsedRange.Offset(10, 0).Resize(RowSize:=SourceRcount - 10)
End With

By directly copying only what you want you avoid the need to delete the rows later.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.