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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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