Copy content from 2 Excel files in one(problem with last row)

Adi21

Board Regular
Joined
Aug 11, 2014
Messages
62
Hy guys, I want to copy the content from 2 Excel files in another.

Until now I have this code(I know it`s not complete):
Code:
Private Sub CommandButton1_Click()
Dim ThisPath As String
    ThisPath = ActiveWorkbook.Path
    Workbooks.Open Filename:=ThisPath & "\" & "DatabaseLealde1"
    b1 = ActiveWorkbook.Name
    'Workbooks.Open Filename:=ThisPath & "\" & "DatabaseLealde2"
    'b2 = ActiveWorkbook.Name
    Workbooks(b1).Sheets("Faza1").Activate
    LR = Range("J" & Rows.Count).End(xlUp).Row
    Range("A2", "M" & LR).Copy
    Workbooks("Grafice").Activate
    ActiveWorkbook.Sheets("Faza1").Activate
    Range("A2:M2").PasteSpecial Paste:=xlPasteValues
    Workbooks(b1).Close SaveChanges:=False
End Sub

But I checked with debugger and LR recive 1.
I can`t understand why, because I have 26 lines there and I used that line to find the last row for few times and was working.
Anybody can help be?
Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Do you have cells in column J merged with the previous column?

try to use this code:
Code:
LR = Cells.Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
 
Upvote 0
What is it in column J is copied from another excel files.
But I don`t thing is merged with any previous column.
 
Upvote 0
With your code I get this error: " Object variable or block variable not set"
 
Upvote 0
It works fine for me... try to add the sheet reference to the line:

Code:
LR = Sheets("Faza1").Cells.Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
 
Upvote 0
Now is working.Thanks a lot.
But I don`t understand why you need to add sheet reference if I used this
Code:
 Workbooks(b1).Sheets("Faza1").Activate
before.
You can explain me why?
 
Upvote 0
it should work without sheet reference. I can say why it doesn't without debugging code by myself;)
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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