473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Copy User Selection from One Workbook to Another

5
I've got this program that I want to have the user select a range in one workbook, close that workbook, and then paste that selection in the second workbook. This is VBA code for an excel document. Here is the selection code...

Function SelectARange(sPrompt As String, sCaption As String, oReturnedRange As Range) As Boolean
Dim frmSelectCells As ufSelectCells
Set frmSelectCells = New ufSelectCells
With frmSelectCells
.PromptText = sPrompt
.CaptionText = sCaption
If TypeName(Selection) = "Range" Then
.StartAddress = Selection.Address(external:=True)
End If
.Initialise
.Show
If .OK Then
Set oReturnedRange = .ReturnedRange
If oReturnedRange Is Nothing Then
SelectARange = False
Else
SelectARange = True
End If
Else
SelectARange = False
End If
End With
Unload frmSelectCells
Set frmSelectCells = Nothing
End Function

And here is what I thought would work...

goBack = 1
Do While (goBack <> 0)
Dim percentCopy As Range
If SelectARange("Select the month's cost / budget range of both '% year' AND '% actual'.", "Range Select", oRangeSelected) = True Then
addingRows = (oRangeSelected.count / 2)
Set precentCopy = oRangeSelected
goBack = 0
Else
If (MsgBox("You have pressed cancel, go back?", vbYesNo, "Automation Tip") = vbNo) Then
goBack = 0
Else
goBack = 1
End If
End If
Loop
otherBook.Close
Dim destRange As Range
Set destRange = Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(ActiveCell.Row + addingRows - 1, ActiveCell.Column + 1))
destRange.Select
percentCopy.Copy destRange

Does anyone have anything that can help?
Oct 16 '08 #1
5 2673
What version of Excel is in use?
What happens when you run your code?
Is there a particular code line that fails?
Do you get an error message? If so what is the message?
Are you running the code from Excel or are you automating Excel from VB?
Also, the data type, scope and values of all variables should be shown.
Oct 16 '08 #2
dehboy
5
What version of Excel is in use?
What happens when you run your code?
Is there a particular code line that fails?
Do you get an error message? If so what is the message?
Are you running the code from Excel or are you automating Excel from VB?
Also, the data type, scope and values of all variables should be shown.
I'm using Excel 2003 Professional. When I run my code, the destination range is set correctly but nothing is copied or pasted to the destination range. I get no error messages. I'm running the code from excel...its a button click action. The scope of all variables shown but not declared can be assumed as global variables, and their types should be nothing radical (ie all are integers, strings, etc.). There are no arrays in this bit of code.
Oct 17 '08 #3
It may be as simple as adding "Option Explicit" as the first line of code in the module (without the quote marks).
That line of code forces variable declaration/identifies misspelled variable names...

You are setting "precentCopy" to the selection but are
using "percentCopy" when copying.



I'm using Excel 2003 Professional. When I run my code, the destination range is set correctly but nothing is copied or pasted to the destination range. I get no error messages. I'm running the code from excel...its a button click action. The scope of all variables shown but not declared can be assumed as global variables, and their types should be nothing radical (ie all are integers, strings, etc.). There are no arrays in this bit of code.
Oct 17 '08 #4
dehboy
5
It may be as simple as adding "Option Explicit" as the first line of code in the module (without the quote marks).
That line of code forces variable declaration/identifies misspelled variable names...

You are setting "precentCopy" to the selection but are
using "percentCopy" when copying.
Good call on the percentCopy thing, I hate that there is no good way of figuring those. Unfortunately, though, I tried running the program with the change and it still didn't copy the range the user selects from one workbook to another. I do have the option explicit thing at the top of the module too.

As some more information, I pulled this user selection thing off someone's code online. They use a form they created to do all the selection thing, and there is a lot of code behind the form. I didn't copy paste that code cause I didn't think it was relevant and it was too big to copy/paste.
Oct 17 '08 #5
Following is a simple example. You should be able to use it to modify your existing code.
"Range" or "Cells" without a workbook/sheet qualifier refers to the active sheet.
Close the workbook(s) After copy/paste.

'--
'Both workbooks must be open.
Sub CopyStuffBetweenWorkbooks()

Dim wbFirst As Workbook
Dim wb2nd As Workbook
Dim copyFrom As Range
Dim copyTo As Range

Set wbFirst = ActiveWorkbook
Set wb2nd = Workbooks("OtherName")

'Range and cells are qualifed with the workbook name and sheet!
'Notice the dots.
With wbFirst.Sheets(1)
Set copyFrom = .Range(.Cells(1, 1), .Cells(10, 3))
End With

With wb2nd.Sheets(2)
Set copyTo = .Range(.Cells(1, 3), .Cells(10, 5))
End With

copyFrom.Copy Destination:=copyTo
End Sub
'--


Good call on the percentCopy thing, I hate that there is no good way of figuring those. Unfortunately, though, I tried running the program with the change and it still didn't copy the range the user selects from one workbook to another. I do have the option explicit thing at the top of the module too.

As some more information, I pulled this user selection thing off someone's code online. They use a form they created to do all the selection thing, and there is a lot of code behind the form. I didn't copy paste that code cause I didn't think it was relevant and it was too big to copy/paste.
Oct 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: eyal | last post by:
Hello, I want to open 2 workbook, , lets say workbook1 and workbook2 now I want to copy data from a sheet name "TEST" in workbook2 and paste the data to a sheet named "TEST" in workbook. now...
0
by: shan_chennai | last post by:
I have the followign requirement.. I have a worksheet Named 'Sales' in a excel Workbook named "WorkBook1". There is another workbook by Name "Workbook2" which has many worksheets. There is a...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
0
by: Kay | last post by:
Hi all, I want to copy an entire row from a worksheet to another worksheet, when I set the excel app = visible and step thru the code I can actally see a row is appended to another worksheet,...
0
by: Probleminfinity | last post by:
Hi, I have been trying to Copy a worksheet into another workbook and rename it using macro. But while running it Gives me an error: Run-time error '40036': Application-defined or object-defined...
3
by: samj | last post by:
Looking at code examples, it would seem this is simple, but I can't figure it out. From an Access 2000 form command button, I want to copy all worksheets in an existing workbook to an existing...
4
by: hiitzsdg | last post by:
Hi All, I am completely new to VBA and I am trying to develop a macro in a workbook (Backup.xls). The main functionality of the macro is: 1. It would open another workbook (Source.xls). 2....
2
by: excelee | last post by:
want to run a script to first identify the user, and after identifying the user opens a workbook (e.g. workbbok1, sheet1). user_ID = Environ("USERNAME") the following is the process 2-...
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.