why clear headers despite of start copying from row2?

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hi guys,
I'm not sure why this problem occurs for me .
the code should clear from row2 for closed file "Bridgestone Stock Sales report(12)" before import data from open file , but will also clear headers for closed file !!
VBA Code:
Sub OpenFilesFromFolder1()
  Dim ExtBk As Workbook
  Dim IntBk As Workbook
  Dim FolderPath As String
  Dim FilePath As String
  Dim lRow As Long
  Dim Rng1 As Range, Rng2 As Range
 
  Set IntBk = ActiveWorkbook

  lRow = IntBk.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

   FolderPath = "C:\Users\MY-NAME\DESKTOP\BRIDGESTONE REPORT\"
 
  FilePath = Dir(FolderPath & "Bridgestone Stock  Sales report(12).xls")
  If FilePath <> "" Then
    Set ExtBk = Workbooks.Open(FolderPath & FilePath)
  Else   
     MsgBox "File Bridgestone Stock  Sales report(2).xlsm not found"
     Exit Sub
  End If
  Application.ScreenUpdating = False

  With ExtBk.Worksheets("REPORT")
      .Range("A2:D" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
  End With
  ExtBk.Worksheets("REPORT").Range("A2:A" & lRow).Value = IntBk.Worksheets("Sheet1").Range("A2:A" & lRow).Value
  ExtBk.Worksheets("REPORT").Range("D2:D" & lRow).Value = IntBk.Worksheets("Sheet1").Range("D2:D" & lRow).Value
  Set Rng1 = IntBk.Worksheets("Sheet1").Range("B2:C" & lRow)
  Set Rng2 = ExtBk.Worksheets("REPORT").Range("B2:C" & lRow)
  Rng2.Value = Rng1.Value
  Application.ScreenUpdating = True
  Application.DisplayAlerts = False
  ExtBk.Save
  ExtBk.Close
  Application.DisplayAlerts = True
End Sub
any help to fix this problem,please?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Only in the sense of the row number being produced by ExtBk.Worksheets("REPORT").Cells(Rows.Count, 1).End(xlUp).Row is returning a number lower than 2
 
Upvote 0
so I have two choices as you and Alex suggests or using loop by specify where start row from to avoid this problem?
 
Upvote 0
As Alex suggested or the code below which will prevent it accepting a 1 (will produce a 2 instead of a 1)
VBA Code:
.Range("A2:D" & Application.Max(.Cells(.Rows.Count, 1).End(xlUp).Row, 2)).ClearContents
or base it on a column which always produces the required answer which obviously
VBA Code:
ExtBk.Worksheets("REPORT").Cells(Rows.Count, 1).End(xlUp).Row
doesn't
 
Last edited:
Upvote 0
what about loop like this ?

VBA Code:
for i=2 to lrow
      .Range("A2:D" & lrow).ClearContents
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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