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
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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:
Upvote 0
Wow It works thank you ,but why it worked when was executed from the WB workbook and what the dots do in this case?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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