Forum Discussion
Need to copy some data from workbook A to Workbook B
Thanks for the quick response Brad! I was making this too hard. But I still have an issue that I am confused on. what is the best way to have the user start Excel that is foolproof so they don't forget the data workbook? How do I open the workbook in code?
I am now getting an error when attempting the set rgEntry=thisWorkbook... statement. Error is run-time error '1004' select method of range class has failed. It all worked ok when I was in debug mode but when I saved everything and closed Excel, then attempted to start from double-clicking on the .xlsm file I get the error. I have a workbook_open sub which calls some initial routines that display info on the Entry worksheet. Obviously I missing something basic (no pun intended). Thanks again for your help.
The 1004 error results when VBA can't find a workbook or worksheet by the name shown in the code.
Since you didn't post sample workbooks, I may have made some incorrect guesses on the names and locations of your data. I commented the statements I thought you might need to review and possibly change.
I can certainly open the other workbook programmatically, and the ideal place to do so is in a Workbook_Open event sub. You may only have one of those, so I'll need to tweak your existing code.
Please post sample workbooks so I can adjust the code, confirm data location, workbook name, worksheet names, then test everything.
Brad
- Bob HiltonJul 17, 2019Copper Contributor
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 SubFunction 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.FullNameentryName = 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 SubPublic 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- Brad_YundtJul 17, 2019MVP
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