Forum Discussion
Need to copy some data from workbook A to Workbook B
Thanks again for the prompt response. I just realized the response I sent to you did not go because this system did not like .txt files. I've attached a sample of the first part of my code. What I would like to be able to open the data workbook (dataName which is "JustSoldData.xlsb) in the GetDataFile sub. Thanks again for your help and your patience. (It's embarrassing how much I've forgotten in the 10 years since I did any VB or VBA.) Code follows:
Option Explicit
Public iCurrentRow As Integer
Public entryName As String
Public dataName As String
Public bEntryOpen As Boolean
Public bDataOpen As Boolean
Public wsData As Worksheet
Public rgEntry As Range
Dim cel As Range
Private Sub workbook_open()
Call GetEntryFile
Call GetDataFile
' MsgBox "we did it"
End Sub
Function IsWorkbookOpen(ByVal WorkbookName As String) As Boolean
' Returns True if the specified workbook is found open in the current instance of excel
On Error Resume Next
IsWorkbookOpen = CBool(Len(Workbooks(WorkbookName).Name) <> 0)
On Error GoTo 0
End Function
Public Sub GetEntryFile()
Dim srcFilePath As Variant
Dim controlname As String
srcFilePath = ThisWorkbook.FullName
entryName = ThisWorkbook.Name
bEntryOpen = IsWorkbookOpen(entryName)
If Not bEntryOpen Then MsgBox "Entry workbook failed to open", vbCritical, "Entry Workbook Open Failed"
ThisWorkbook.Worksheets("Entry").Range("B22").Value = srcFilePath
End Sub
Public Sub GetDataFile()
Dim dataFilePath As Variant
Dim wbx As Workbook
MsgBox ("Please select the file containing the data for Neighbors")
dataFilePath = Application.GetOpenFilename _
("Excel files (*.xls, *xlsx, *xlsm, *xlsb")
If dataFilePath = False Then Exit Sub
dataName = Right(dataFilePath, Len(dataFilePath) - InStrRev(dataFilePath, _
Application.PathSeparator))
bDataOpen = IsWorkbookOpen(dataName)
If Not bDataOpen Then MsgBox "Data workbook (file) failed to open", vbCritical, "Data Workbook Open Failed"
ThisWorkbook.Worksheets("Entry").Range("E22").Value = dataFilePath
end sub
FWIW, sub GetEntryFile can never fail to find the entry workbook--because it is the one containing the code that is running. Not a problem--just that you have a useless test of whether that file is open.
The data file workbook, however, may or may not be open. I revised sub GetDataFile so it would open the workbook if need be:
Public Sub GetDataFile()
Dim dataFilePath As Variant
Dim wbx As Workbook
'MsgBox ("Please select the file containing the data for Neighbors")
dataFilePath = Application.GetOpenFilename _
("Excel files (*.xls, *xlsx, *xlsm, *xlsb", Title:="Please select the file containing data for Neighbors")
If dataFilePath = False Then Exit Sub
dataName = Right(dataFilePath, Len(dataFilePath) - InStrRev(dataFilePath, _
Application.PathSeparator))
bDataOpen = IsWorkbookOpen(dataName)
If Not bDataOpen Then
Workbooks.Open dataFilePath
'MsgBox "Data workbook (file) failed to open", vbCritical, "Data Workbook Open Failed"
End If
ThisWorkbook.Worksheets("Entry").Range("E22").Value = dataFilePath
End Sub- Bob HiltonJul 17, 2019Copper Contributor
Man, you must never sleep! Once again thanks for the quick response. I had tried the workbooks.open statement several days ago but must have fat fingered something. Thanks for putting up with me, your patience and your expertise.
Bob