ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why Won't This Code Work???? (https://www.excelbanter.com/excel-programming/340121-why-wont-code-work.html)

TK[_3_]

Why Won't This Code Work????
 
Why can't I get this to work? NewFN always equals the file I selected but
wbExt always equals "nothing" and the routine crashes. I can't figure out
what I'm doing wrong.
Thanks in advance,
Todd

Private Sub CommandButton1_Click()
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),
*.xls", Title:="Select a file")
If NewFN = False Then
MsgBox "No File Was Selected"
Else
Workbooks.Open FileName:=NewFN

Dim wbExt As Workbook
Set wbExt = NewFN
wbExt.Worksheets("Sheet1").Range("C4").Copy

Application.CutCopyMode = False
wbExt.Close savechanges:=False
Set wbExt = Nothing

End If
End Sub



Rowan[_8_]

Why Won't This Code Work????
 
It crashes because NewFN is a string and wbExt is a workbook so you
can't set one equal to the other.

You could try:

Set wbExt = Workbooks(Right(NewFN, InStrRev(NewFN, "\")))

Hope this helps
Rowan

TK wrote:
Why can't I get this to work? NewFN always equals the file I selected but
wbExt always equals "nothing" and the routine crashes. I can't figure out
what I'm doing wrong.
Thanks in advance,
Todd

Private Sub CommandButton1_Click()
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),
*.xls", Title:="Select a file")
If NewFN = False Then
MsgBox "No File Was Selected"
Else
Workbooks.Open FileName:=NewFN

Dim wbExt As Workbook
Set wbExt = NewFN
wbExt.Worksheets("Sheet1").Range("C4").Copy

Application.CutCopyMode = False
wbExt.Close savechanges:=False
Set wbExt = Nothing

End If
End Sub



Patrick Molloy[_2_]

Why Won't This Code Work????
 
just a simple change is all, you pretty much had it....

Private Sub CommandButton1_Click()
Dim wbExt As Workbook

NewFN = _
Application.GetOpenFilename("Excel Files (*.xls), _
*.xls","Select a file")
If NewFN = False Then
MsgBox "No File Was Selected"
Else
Set wbExt = Workbooks.Open(NewFN)
wbExt.Worksheets("Sheet1").Range("C4").Copy

Application.CutCopyMode = False
wbExt.Close savechanges:=False
Set wbExt = Nothing
End If
End Sub





"TK" wrote:

Why can't I get this to work? NewFN always equals the file I selected but
wbExt always equals "nothing" and the routine crashes. I can't figure out
what I'm doing wrong.
Thanks in advance,
Todd

Private Sub CommandButton1_Click()
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),
*.xls", Title:="Select a file")
If NewFN = False Then
MsgBox "No File Was Selected"
Else
Workbooks.Open FileName:=NewFN

Dim wbExt As Workbook
Set wbExt = NewFN
wbExt.Worksheets("Sheet1").Range("C4").Copy

Application.CutCopyMode = False
wbExt.Close savechanges:=False
Set wbExt = Nothing

End If
End Sub





All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com