Error 1004

marcohc88

New Member
Joined
Jan 18, 2017
Messages
13
The following code works when I execute it from the WB workbook but returns error 1004 when I run it from WBi workbook I need to run it from WBi Woorkbook. Thx for the help in advance

Code:
Option Explicit

Sub MatchCopy()

Dim WB As Workbook            
Dim WBi As Workbook            
Dim FindString1 As Long    
Dim FindString2 As Long     
Dim Lcol1 As Long             
Dim Lcol2 As Long           
Dim Lrow1 As Long              
Dim Lrow2 As Long               

Application.ScreenUpdating = False

Set WBi = Workbooks("WBi.xlsm")
Set WB = Workbooks("WB.xlsx")


WBi.Sheets("Sheet1").Range("A1:B180").Clear

FindString1 = "104"
FindString2 = "301"

With WB.Sheets("Sheet1")

    Lcol1 = Application.Match(FindString1, .Rows(27), 0)

    Lcol2 = Application.Match(FindString2, .Rows(6), 0)

    Lrow1 = Cells(Rows.count, Lcol1).End(xlUp).Row
    Lrow2 = Cells(Rows.count, Lcol2).End(xlUp).Row

    .Range(Cells(27, Lcol1), Cells(Lrow1, Lcol1)).Copy WBi.Sheets("Sheet1").Range("A1")
    .Range(Cells(6, Lcol2), Cells(Lrow2, Lcol2)).Copy WBi.Sheets("Sheet1").Range("B1")

End With

Application.ScreenUpdating = True

End Sub
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,371
See if this works (UNTESTED)

Observe the .
Code:
Sub MatchCopy()

Dim WB As Workbook
Dim WBi As Workbook
Dim FindString1 As Long
Dim FindString2 As Long
Dim Lcol1 As Long
Dim Lcol2 As Long
Dim Lrow1 As Long
Dim Lrow2 As Long

Application.ScreenUpdating = False

Set WBi = Workbooks("WBi.xlsm")
Set WB = Workbooks("WB.xlsx")

WBi.Sheets("Sheet1").Range("A1:B180").Clear

FindString1 = "104"
FindString2 = "301"

With WB.Sheets("Sheet1")
    Lcol1 = Application.Match(FindString1, .Rows(27), 0)
    Lcol2 = Application.Match(FindString2, .Rows(6), 0)

    Lrow1 = [COLOR=#ff0000].[/COLOR]Cells([COLOR=#ff0000].[/COLOR]Rows.Count, Lcol1).End(xlUp).Row
    Lrow2 = [COLOR=#ff0000].[/COLOR]Cells([COLOR=#ff0000].[/COLOR]Rows.Count, Lcol2).End(xlUp).Row

    .Range([COLOR=#ff0000].[/COLOR]Cells(27, Lcol1), [COLOR=#ff0000].[/COLOR]Cells(Lrow1, Lcol1)).Copy WBi.Sheets("Sheet1").Range("A1")
    .Range([COLOR=#ff0000].[/COLOR]Cells(6, Lcol2), [COLOR=#ff0000].[/COLOR]Cells(Lrow2, Lcol2)).Copy WBi.Sheets("Sheet1").Range("B1")
End With
Application.ScreenUpdating = True
End Sub
Hope this helps

M.
 
Last edited:

marcohc88

New Member
Joined
Jan 18, 2017
Messages
13
Wow It works thank you ,but why it worked when was executed from the WB workbook and what the dots do in this case?
 

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,371
You are welcome. Glad to help.

You must qualify (specify) the ranges properly inside the With...End With exactly like you did to get LCol1 and LCol2 using .Rows(27) and .Rows(6) .
Without the dots VBA considers the ranges on the ActiveSheet, not on WB.Sheets("Sheet1").

see
Referring to Ranges in VBA | Excel Matters

M.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,839
Messages
5,446,802
Members
405,415
Latest member
Noodnutt

This Week's Hot Topics

Top