Variable from different Workbook

Partjob

Board Regular
Joined
Apr 17, 2008
Messages
139
It seems I have worked on this bit of code for months and it was working until the sheet that the data is coming from was changed. To allow for this and any further possible changes I did the following.
Code:
Workbooks.Open Filename:=Fnm 'place file path of workbook in cell in column A
    With ActiveWorkbook
    [COLOR=red]Rnum = Sheet43.Range("S" & Rows.Count).End(xlUp).Row
[/COLOR]    CopRan = Range(Range("S" & Rnum), Range("S" & Rnum).Offset(0, Prolen)).Address
    'find Range for copy
    Sheets("Outline Activity Schedule").Range(CopRan).Copy
       Workbooks(MyWbk).Sheets("Prediction").Cells(Target.Row, Pastecol).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
So just to clarify what is going on here. An external workbook is being opened with the variable "Fnm" Once this is open I need to declare the row where the data will be, and pass it to the next line to set the next variable "CopRan" It is always in Column "S".
This all works if I don't try and do this ie drop the red line and have an actual number instead of the variable.
I have Dim'd the variables at the beginning of the code.
If I put this line in the immeadiate window on the target sheet I do get the result I require.
I get the Error "Object Required" Run time Error 424
Thanks Partjob
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello,

my guess would be

Code:
Rnum = sheets("Sheet43").Range("S" & Rows.Count).End(xlUp).Row
 
Upvote 0
My guess would be to create references to the workbooks.
For example:
Code:
Set wbThis = ThisWorkboook ' create reference to workbook the code is in
Code:
Set wbAct  = ActiveWorkbook ' create reference to the active workbook
Code:
Set wbOpen = Workbooks.Open("C:\MyPath\MyBook.xls") ' open workbook and create a reference to it
Code:
Set wbNew = Workbooks.Add ' create new workbook and a reference to it
 
Upvote 0
Thanks onlyadrafter
I had tried all sorts there but not Sheets.
However I now have a different error "Subscript out of range"
This is I think because it is still looking at the original workbook, this has not got a Sheet43, only the target book has a Sheet43. I think it is probably worth posting the whole code
Code:
Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Dim Fnm As String
Dim Myrow As Integer
Dim MyWbk As String
Dim Pastecol As Integer
Dim CopRan As String
Dim Dat As Long
Dim Prolen As Integer
Dim Strdt As Long
Dim Rnum As Integer
 If AbortChangeEvent = True Then Exit Sub
'Reference point for dates
Dat = Sheet2.Cells(2, 9).Value
'Length of project to create copy range variable
Prolen = Sheet1.Cells(Target.Row, 19).Value - 1
'Date project starts
Strdt = Sheet1.Cells(Target.Row, 18).Value
'varable to work out where to paste the data
Pastecol = 9 + DateDiff("m", Dat, Strdt)
'set copy range
MyWbk = ThisWorkbook.Name
Fnm = Sheet2.Cells(Target.Row, 1).Text
If Target.Column <> 1 Then Exit Sub
If Dir(Fnm) = "" Then MsgBox "You have made an error try again", vbOKOnly, "David Masters"
If Target.Column = 1 And Fnm = "" Then
MsgBox "You have made an error try again", vbOKOnly, "David Masters"
Application.ScreenUpdating = True
     Exit Sub
    Else
    Workbooks.Open Filename:=Fnm 'place file path of workbook in cell in column A
    With ActiveWorkbook
    [COLOR=red]Sheets("Sheet43").Range("S" & Rows.Count).End(xlUp).Row[/COLOR]
    CopRan = Range(Range("S" & Rnum), Range("S" & Rnum).Offset(0, Prolen)).Address
    'find Range for copy
    Sheets("Outline Activity Schedule").Range(CopRan).Copy
       Workbooks(MyWbk).Sheets("Prediction").Cells(Target.Row, Pastecol).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End With
    End If
ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
I still have a problem with the red line with the error listed above.
thanks Partjob
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,544
Members
449,169
Latest member
mm424

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