• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need help w/ VBA copy worksheets between workbooks

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]
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
[/pre]
 
Hi, franklin_m!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about questions in general...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


And about this question in particular...


Didn't checked and a little lazy to update my personal workbook, but as a blind shot give a try changing the DstWkb assignment from ThisWorkbook to ActiveWorkbook.


Regards!
 
Back
Top