LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default ThisWorkbook Problem

I have the following code in a file (call it File1) which SHOULD do the
following: Prompt a user to select a file (call it FileMMS), then open
FileMMS, copy certain data from that file into the original (active) file
(File1), and then close FileMMS. Problem is this:

I open File1 and run the macro, it works fine. I then open another copy of
File1 (under a different filename - call this File2) and run the macro, it
works fine. With both of these files open, I go back to File1 and run the
macro again; instead of putting the extracted data (from FileMMS) into File1
however, it puts it into File2. It needs to put the data into whatever file
is active at the time, so it seems my code has a problem in terms of
recognizing in which workbook to put the data (it should be the active
workbook). Would appreciate some assistance in correcting this - is probably
a one-liner thing but I can't figure it out. The code is:

Sub GetDataFromMMSForm()
Dim WB As Workbook
Dim strFileName As String
Dim P As Variant
Dim X As Variant
Dim rFound As Range

strFileName = Application.GetOpenFilename(FileFilter:="All
Files(*.*),*.xls,All Files (*.*),*.*")

On Error Resume Next
Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
On Error GoTo 0

If WB Is Nothing Then
Set WB = Workbooks.Open(strFileName, True, True)
On Error Resume Next
Worksheets("A").Select
Set rFound = Cells.Find(What:="Customer Name =", After:=Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False)
If rFound Is Nothing Then
MsgBox "Sorry; Excel was unable to find a customer name."
Else
With ThisWorkbook.Worksheets("Schedule")
.Range("B6").Value = Range(rFound.Address).Offset(0,
1).Value
End With
End If
On Error GoTo 0
WB.Close False
Set WB = Nothing

Else

Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
On Error Resume Next

WB.Worksheets("A").Activate
Set rFound = Cells.Find(What:="Customer Name =", After:=Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False)
If rFound Is Nothing Then
MsgBox "Sorry; Excel was unable to find a customer name."
Else
With ThisWorkbook.Worksheets("Schedule")
.Range("B6").Value = Range(rFound.Address).Offset(0,
1).Value
End With
End If

On Error GoTo 0
Set WB = Nothing
End If

ThisWorkbook.Activate
Range("A1").Select
End Sub



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with ThisWorkbook.RefreshAll Vick Excel Discussion (Misc queries) 3 September 7th 07 12:26 AM
ThisWorkBook problem Blogd_Node Excel Programming 3 April 26th 06 07:19 AM
Set Range on ThisWorkBook problem. Cesar Zapata Excel Programming 1 January 31st 06 10:35 PM
Empty ThisWorkbook module, but Macro warning problem Stuart[_5_] Excel Programming 3 June 28th 04 07:20 PM


All times are GMT +1. The time now is 06:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"