473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Making a Command Button in A Userform Copy & Paste Column from One Workbook Into Anot

3 New Member
Hi All:

Here are the requirements:

1) Target workbook is "lsat_v26.xlsm" and is the main program.

2) Source workbook is "fake_master_db5.xls".

3) Target worksheet is "LookupLists".

4) Source worksheet is "PROJECT LIST".

5) Another target workbook is "test8_trial_2 (works).xlsm" and contains the isolated copy/paste macro which is tested and is working.

6) A Userform has a command button which invokes a browse-for-file window for the user to select source workbook directory. Once selected, macro takes over and copies column A1:A from source worksheet of source workbook. Copied column is pasted into target worksheet of target workbook. Then, another macro sorts that column in a descending order. Then, a combobox pulls all cells in this sorted column and turns them into drop-down options a user can select in that combobox.

By itself, the browse/copy/paste macro works perfectly. But when attempting to merge it with the Userform code I get a bug. See copy/paste macro below. See my other post from yesterday. This is the same macro as the one from that post.

About the bug....the source excel file opens up and stays open in forefront of the target excel file. Userform also stays open and allows me to enter data into its many textbox, combobox, etc. Other command buttons work as long as they influence only the userform. One command button that adds data from userform into the target workbook produces an error on clicking....implying that the target worksheet/workbook gets locked by the copy/paste macro (see code below).

Questions/hypothesis: Should I be injecting this macro into a particular section of the userform code? Is the incorrect sequence of code breaking my program? Is the copy/paste macro locking the target workbook/worksheet and thus preventing the userform from operating on it?

Expand|Select|Wrap|Line Numbers
  1. 'MACRO TO READ-IN EXTERNAL EXCEL FILE FROM WHICH JOB NO.'S ARE EXTRACTED INTO USERFORM        
  2.         Sub ReadDataFromCloseFile()
  3.  
  4.             'IN CASE OF ERROR SEND TO ERROR FUNCTION
  5.                 On Error GoTo ErrHandler
  6.  
  7.             'PREVENT OPENED EXCEL SOURCE FILE FROM SHOWING TO USER
  8.                 Application.ScreenUpdating = False
  9.  
  10.             'OPEN SOURCE EXCEL WORKBOOK IN "READ ONLY MODE"
  11.                 Dim SrcName As String
  12.                 Dim src As Workbook
  13.                 SrcName = Application.GetOpenFilename()
  14.                 Set src = Workbooks.Open(SrcName, True, True)
  15.  
  16.             'GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK
  17.                 Dim iTotalRows As Integer
  18.                 iTotalRows = src.Worksheets("PROJECT LIST").Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Rows.Count
  19.  
  20.             'COPY DATA FROM SOURCE WORKBOOK  -> DESTINATION WORKBOOK
  21.                 Dim iCnt As Integer     '(COUNTER)
  22.                 For iCnt = 1 To iTotalRows
  23.                     Worksheets("Test_File_8").Range("B" & (iCnt + 1)).Formula = src.Worksheets("PROJECT LIST").Range("A" & (iCnt + 1)).Formula
  24.                 Next iCnt
  25.  
  26.             'CLOSE THE SOURCE WORKBOOK FILE
  27.                 src.Close False             'FALSE = DONT SAVE THE SOURCE FILE
  28.                 Set src = Nothing           'FLUSH DATA
  29.  
  30.             'ERROR FUNCTION
  31. ErrHandler:
  32.             Application.EnableEvents = True
  33.             Application.ScreenUpdating = True
  34.         End Sub
After modding above code for work in context of a userform, here is the Command Button code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2. 'MACRO TO READ-IN EXTERNAL EXCEL FILE FROM WHICH JOB NO.'S ARE EXTRACTED INTO USERFORM
  3.  
  4.             'IN CASE OF ERROR SEND TO ERROR FUNCTION
  5.                 On Error GoTo ErrHandler
  6.  
  7.             'PREVENT OPENED EXCEL SOURCE FILE FROM SHOWING TO USER
  8.                 Application.ScreenUpdating = False
  9.  
  10.             'OPEN SOURCE EXCEL WORKBOOK IN "READ ONLY MODE"
  11.                 Dim SrcName As String
  12.                 Dim src As Workbook
  13.                 SrcName = Application.GetOpenFilename()
  14.                 Set src = Workbooks.Open(SrcName, True, True)
  15.  
  16.             'GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK
  17.                 Dim iTotalRows As Integer
  18.                 iTotalRows = src.Worksheets("PROJECT LIST").Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Rows.Count
  19.  
  20.             'COPY DATA FROM SOURCE WORKBOOK  -> DESTINATION WORKBOOK
  21.                 Dim iCnt As Integer     '(COUNTER)
  22.                 For iCnt = 1 To iTotalRows
  23.                     Worksheets("LookupLists").Range("B" & (iCnt + 1)).Formula = src.Worksheets("PROJECT LIST").Range("A" & (iCnt + 1)).Formula
  24.                 Next iCnt
  25.  
  26.             'CLOSE THE SOURCE WORKBOOK FILE
  27.                 src.Close False             'FALSE = DONT SAVE THE SOURCE FILE
  28.                 Set src = Nothing           'FLUSH DATA
  29.  
  30.             'ERROR FUNCTION
  31. ErrHandler:
  32.             Application.EnableEvents = True
  33.             Application.ScreenUpdating = True
  34.  
  35. End Sub
Any help would be greatly appreciated! See attached source file.
Attached Files
File Type: xls fake_master_db5.xls (915.5 KB, 260 views)
Aug 4 '16 #1
0 1186

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

Similar topics

0
by: Nelgonde | last post by:
Apologies for the basic nature of this question but I'm very inexperienced in Java. I've been given the task of disabling the cut/copy/paste functions (Ctrl-X,C & V) in our java dialogs and...
2
by: Tim Hochberg | last post by:
During the recent, massive, painful Lisp-Python crossposting thread the evils of Python's whitespace based indentation were once again brought to light. Since Python' syntax is so incredibly...
0
by: James | last post by:
Does annybody know how to disable clipboard copy and paste events in a Rich TextBox control. Thanks
6
by: NuB | last post by:
I have a winform and a menu on the form, It allows the users to copy and paste text from text boxes. I never had to do this before, how can I copy and paste text from one box to another?
2
by: Georg Weiler | last post by:
Hi, I have a form, where the user enters text. After submit, the text is then inserted into a PostgreSQL database. So far, so good. The problem is, that most of my users copy&paste their text...
0
by: Mr. Murad Jamal | last post by:
hi guys, I have a textbox & a button in an .aspx page, when I hit the button i want a selected text to be copied to the clipboard AND paste it into the last cursor position on the textbox BOTH AT...
5
by: dgrabuloff | last post by:
i am inputting records using a form. how do i put a command button on the form that will copy the record i just input----sometimes i have the same record that needs to be put in 3 or 4 times and i...
6
by: rakeshvthu | last post by:
hi all, can we disable the copy and paste option in edit menu bar of browser my customer does not want copying using any technique so can any one help how to disable that options copy and...
5
by: deve8ore | last post by:
Hello, I have a Word document called (), a named range that will open up a new Word doc dependent on what a user selects in Excel. Could someone please guide me on how to set up VBA code to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.