Error on Code, Please Help

Excelnoobisme

Board Regular
Joined
Nov 19, 2010
Messages
128
Hi i have a worksheet name "Working on amount Mar 2011" i need to paste some data depending on the date to another worksheet. The below is my vba code which work.

Sub Testing()
ActiveSheet.Name = "Today"

Workbooks.Open Filename:= _
"C:\Program File\Shared Files\Export Amount Mar 2011.xlsx"

Dim ff As Range
Set Found = Workbooks("Export Amount Mar 2011.xlsx").Sheets("Loc1").Columns("A").Find(what:=Workbooks("Working on Amount Mar 2011.xlsm").Sheets("Today").Range("A3").Value, LookIn:=xlValues, lookat:=xlWhole)

If Found Is Nothing Then
MsgBox "Not found"
Else
Workbooks("Working on Amount Mar 2011.xlsm").Sheets("Today").Range("T12:T15").Copy
Workbooks("Export Amount Mar 2011.xlsx").Sheets("Loc1").Range(Found.Address).Offset(0, 1).PasteSpecial Paste:=xlPasteValues

End If
ActiveWorkbook.Save
ActiveWindow.Close

ActiveSheet.Name = Range("TodayDate").Value

End Sub




Now i need to change the worksheet name to be more flexible so as i dont need to change the code everymonth.The below code always give me a runtime error. Any1 can advise what is wrong?



Sub Testing()
ActiveSheet.Name = "Today"
ActiveWorkbook.Windows(1).Caption = "1"

Set objExcel = CreateObject("Excel.Application")
Workbooks.Open Filename:= _
"C:\Program File\Shared Files\Export Amount Mar 2011.xlsx"
objExcel.Visible = True
Set objExcel = Nothing
ActiveWorkbook.Activate
ActiveWorkbook.Windows(1).Caption = "2"

Dim ff As Range
Set Found = Workbooks("2.xlsx").Sheets("Loc1").Columns("A").Find(what:=Workbooks("1.xlsm").Sheets("Today").Range("A3").Value, LookIn:=xlValues, lookat:=xlWhole)

If Found Is Nothing Then
MsgBox "Not found"
Else
Workbooks("1.xlsm").Sheets("Today").Range("T12:T15").Copy
Workbooks("2.xlsx").Sheets("Loc1").Range(Found.Address).Offset(0, 1).PasteSpecial Paste:=xlPasteValues

End If
ActiveWorkbook.Save
ActiveWindow.Close

ActiveSheet.Name = Range("TodayDate").Value

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I don't think you can refer to workbooks based on labels (someone may prove me wrong)

Since, I assume, the xlsm is open before the xlsx, then they can be referred to by their workbook index as:

Set Found = Workbooks(2).Sheets("Loc1").Columns("A").Find(what:=Workbooks(1).Sheets("Today").Range("A3").Value, LookIn:=xlValues, lookat:=xlWhole)
 
Upvote 0
Set Found = Workbooks(2).Sheets("Loc1").Columns("A").Find(what:=Workbooks(1).Sheets("Today").Range("A3").Value, LookIn:=xlValues, lookat:=xlWhole)

Still getting run-time error "9"
 
Upvote 0
Yea, after I posted, I thought about it and hard coding indexes is not a good idea. other workbooks, including personal.xl* could cause undesireable results.
 
Upvote 0
Try this as a model.


Code:
Dim wb1, wb2 As Workbook
Dim ws1, ws2 As Worksheets
 
Set wb1 = ActiveWorkbook
 
FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
 
If FileToOpen <> False Then
    Set wb2 = Workbooks.Open(FileToOpen)
Else
    MsgBox "No File Selected", vbOKOnly, "Aborting"
    Exit Sub
End If
 
'<snip>
Set Found = wb2.Sheets("Loc1").Columns("A").Find(what:=wb1.Sheets("Today").Range("A3").Value, LookIn:=xlValues, lookat:=xlWhole)

you're going to have reference issues when you get down to
Code:
[COLOR=green]xxx[/COLOR].Sheets("Today").Range("T12:T15").Copy
as well.
 
Upvote 0
what's the problem exactly?
The more specific it is defined, the fewer cycles it takes to resolve.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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