Solved

Insert data into another workbook using vba

Posted on 2004-04-04
11
2,447 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 Install Options 11 71
Office.Com /myAccount 9 52
Recover lost Hyperlink destination/target in OneNote 3 37
Office 365 home questions 7 65
Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

821 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