Solved

Insert data into another workbook using vba

Posted on 2004-04-04
11
2,449 Views
Last Modified: 2007-12-19
Hello, I keep getting the type mismatch error on this code i have written.  What I have is a useform when the command button is clicked it will open another workbook and input data from the current workbook to the workbook that was just opened.  The problem i am having is it is not recognizing the Data1 values which comes from my listbox on my userform.  The listbox contains names of all the sheets.  For example I select "Jan" in the listbox and press the command button, it will recognize the Listbox value of "Jan" but then when it gets to the copying data from "ah13" to c32 it give me the runtime error, any suggestions.  THank you
Private Sub CommandButton5_Click()
Set Data1 = ListBox2

Application.ScreenUpdating = False
Workbooks.Open Filename:="\\Flieaircwt1\elibrary\20 Temp\QA Statistics Beta"

       
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C32") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah13")
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C33") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah25")
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C34") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah37")
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C35") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah49")
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C36") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah61")
Workbooks("QA Statistics Beta").Sheets(Data1).Range("C37") = Workbooks("EOM_Beta").Sheets(Data1).Range("ah73")
0
Comment
Question by:sandramac
11 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 10755210
If its an OLEObject (from the Controls Toolbox) try

Dim Data1 As OLEObject
Set Data1 = ActiveSheet.OLEObjects("ListBox2")

Application.ScreenUpdating = False
Workbooks.Open Filename:="\\Flieaircwt1\elibrary\20 Temp\QA Statistics Beta"
     
Workbooks("QA Statistics Beta.xls").Sheets(Data1.Object.Value).Range("C32").Value = 12 'Workbooks("EOM_Beta").Sheets(Data1.Object.Value).Range("ah13").Value
etc

Cheers

Dave
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 250 total points
ID: 10755235
sorry misread the form, same idea but try

Private Sub CommandButton1_Click()
Set Data1 = Me.ListBox2

Application.ScreenUpdating = False
Workbooks.Open Filename:="\\Flieaircwt1\elibrary\20 Temp\QA Statistics Beta"

'exit if no value chosen in ListBox
If IsNull(Data1.Value) Then Exit Sub
Workbooks("QA Statistics Beta.xls").Sheets(Data1.Value).Range("C32").Value = 12 'Workbooks("EOM_Beta").Sheets(Data1.Value).Range("ah13").Value

Cheers

Dave

0
 
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 10755559
Is Data1 defined in your macro? And if so in what way?

Is it like
data1 = listbox1.value

where listbox1 is the name of the listbox.

regards,

Jeroen
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:lbertacco
ID: 10757021
Just remove the "set" and leave it as:
data1=listbox1
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 10757122
Why remove the Set?
0
 
LVL 11

Assisted Solution

by:lbertacco
lbertacco earned 250 total points
ID: 10757254
Set is used to assign a value to an object. Here data1 must be a string and to set a value to a string variable you don't have to use set but just
var = "something"
When you do
data1=listbox1
this is interpreted as
data1=listbox1.value
since "value" is the default property for the listbox control
On the other hand, if you do
set data1=listbox
then data1 becomes a listobox object  itself and then Sheets(Data1) fails since Sheets() expect a string or number as the argument.
Also I'd recommend to declare variables as in
Dim data1 as string
to improve readability and type checking.

Ofcourse you could also do directly
Workbooks("QA Statistics Beta").Sheets(listbox1.value).Range("C32") = Workbooks("EOM_Beta").Sheets(listbox1.value).Range("ah13")
but I prefer the use of an auxiliary variable as "data1", maybe just call it "month" rather than "Data1".

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 10763572
I know what set is used for - I was questioning why you would leave it out

I left Dim out of my second example by mistake, but I prefer to use Dim and Set to properly access the object rather than rely on Variants.

Dim data1 As MSForms.ListBox
Set data1 = Me.ListBox2
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 10763619
Ok then:
leave out "set" because you don't want to have data1 to be a listbox object but a string.
You may argue that you WANT data1 to be a listbox, but then (besides the fact that this is not what sandramac is doing) data1 would be totally useless since it would just be an alias for litsbox2 (and you could directly write Sheets(listbox2.Value) in place of Sheets(Data1.Value)).
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 10763856
I didn't think or intend my last post to be offensive - sorry if it came accross that way

Its a matter of techique rather than the final outcome that we are debating  so we may as well agree to disagree about how we choose to return the value via data 1  



0
 
LVL 11

Expert Comment

by:lbertacco
ID: 10766385
brettdj, I didn't mean to be offensive either (I was was late for something so I tried to be concise, maybe too much..)
0
 

Author Comment

by:sandramac
ID: 10766544
Thank you both for the help, it helped me out alot.  thanks again
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question