franklin_m
New Member
This is giving me fits & I could really use some help from the VBA wizards here. Posted elsewhere and not solved yet.
Have one WB Open, name can vary - let's call it RawDataWB.xls, with raw data on "Sheet1". Have cross reference sheet (call it XrefSheet) I need in another WB, called CertDurations.xls. Because the location of that CertDurations.xls can vary depending upon the user, I ask them to navigate to it using the windows interface. The code I've posted below works perfectly when the code is all in a module that's part of RawDataWB.xls.
However, I need the code (and userforms) to reside in the user's Personal.xlsb. They open the file containing the raw data (RawDataWB.xls or whatever it may be named), run the code from Personal.xlsb, which would open an copy the XrefSheet into RawDataWB.xls (or whatever happens to be the name of the WB that contains the raw data). As written below, it's trying to put it into the personal.xlsb instead. I've tried to redefine the destination workbook (even using explicit names), but it's still choking.
Thanks much, Frank
[pre]
[/pre]
Have one WB Open, name can vary - let's call it RawDataWB.xls, with raw data on "Sheet1". Have cross reference sheet (call it XrefSheet) I need in another WB, called CertDurations.xls. Because the location of that CertDurations.xls can vary depending upon the user, I ask them to navigate to it using the windows interface. The code I've posted below works perfectly when the code is all in a module that's part of RawDataWB.xls.
However, I need the code (and userforms) to reside in the user's Personal.xlsb. They open the file containing the raw data (RawDataWB.xls or whatever it may be named), run the code from Personal.xlsb, which would open an copy the XrefSheet into RawDataWB.xls (or whatever happens to be the name of the WB that contains the raw data). As written below, it's trying to put it into the personal.xlsb instead. I've tried to redefine the destination workbook (even using explicit names), but it's still choking.
Thanks much, Frank
[pre]
Code:
'--------------------------------------------------------------------------
' Copies data from "CertDurations.xls" (user navigates to source)
'--------------------------------------------------------------------------
MsgBox ("You will now be asked to navigate to 'CertDurations.xls' as if you were going to open the file")
Dim DstWkb As Workbook
Dim FileFilter As String
Dim filename As Variant
Dim SrcWkb As Workbook
SrcWks = "Durations" 'Name of the Worksheet to be copied
DstWks = "Temp" 'Name the copied Worksheet will be given in this workbook
FileFilter = "Excel Files, *.xls;*.xla;*.csv, All Files, *.*"
filename = Application.GetOpenFilename(Title:="Get CertDurations Info")
If filename = False Then Exit Sub
Workbooks.Open filename:=filename
Set DstWkb = ThisWorkbook
Set SrcWkb = Workbooks.Open(filename:=filename, ReadOnly:=True)
On Error Resume Next
With SrcWkb.Worksheets(SrcWks)
If Err.Number = 9 Then
MsgBox SrcWks & " was not found in " & SrcWkb.Name
GoTo OrderlyExit
End If
On Error GoTo 0
.Copy After:=DstWkb.Worksheets(DstWkb.Worksheets.Count)
On Error Resume Next
ActiveSheet.Name = DstWks
If Err.Number = 1004 Then
MsgBox DstWks & " already exists in " & DstWkb.Name & vbCrLf _
& "Two worksheets can not have the same name."
GoTo OrderlyExit
End If
On Error GoTo 0
End With