VBA between excel workbooks

kalynx

New Member
Joined
Dec 12, 2017
Messages
4
Hi!

I'm trying to copy the value of one workbook and pasting it in another workbook. Where it is pasted depends on the value in the first workbook.

Code:
Sub Part1()
Dim rng As Range
Dim rng1 As Range
Dim Lastrow As Integer
Dim lCol As Long, i As Long


Lastrow = ActiveSheet.UsedRange.Rows.Count
    
 Windows("Workbook1").Activate
    Sheets("Sheet1").Select
    
    lCol = Cells(9, Columns.Count).End(xlToLeft).Column
    For i = lCol To 3 Step -1
           If Cells(9, i) > 0 Then Cells(9, i).Activate
    ActiveCell.Offset(1).Resize(Lastrow, 2).Select
    Selection.Copy


'Part 2


Lastrow = ActiveSheet.UsedRange.Rows.Count
pnum = Workbooks("Workbook1").Sheets("Sheet1").Cells(9, i).Value
    
Windows("Workbook2").Activate
    Sheets("Sheet1").Select
Set rng = Cells.Find(What:=pnum, After:=Range("D6"), LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)
   Set rng1 = rng.Offset(2, -1)
    Cells.Find(What:="FY", After:=rng1, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    ActiveCell.Offset(1).Resize(Lastrow, 2).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Next i
End Sub


Both parts function correctly if run individually. However, when I run them together, my code in the first part is applied to Workbook2. Can anyone explain to me why this happens?

Any help would be much appreciated.
 
Last edited by a moderator:

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".
Hi kalynx,

There's no need to select and activate workbooks, worksheets and ranges in order to perform the copy operation you are wanting to do. The problems you are having are due to the active workbook not being the one you expect it to be when your code iterates through the columns to be copied.

I've revised the first part of your code to show replacing Select and Activate with direct references to the objects to be copied. You can continue that those revisions for the pasting process in part 2.

Code:
 Dim wkb1 As Workbook, wkb2 As Workbook
  
 Set wkb1 = Workbooks("Workbook1.xlsm")
 Set wkb2 = Workbooks("Workbook2.xlsm")

 With wkb1.Sheets("Sheet1")
   Lastrow = .UsedRange.Rows.Count
   
   lCol = .Cells(9, .Columns.Count).End(xlToLeft).Column
   For i = lCol To 3 Step -1
      If .Cells(9, i) > 0 Then .Cells(9, i).Offset(1).Resize(Lastrow, 2).Copy
 
Last edited:
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