Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2006, 01:41 PM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default run-time error 13: type mismatch

Not sure whats going on here. I keep getting this error but everything looks good to me. Any help would be appreciated - lines causing error are in itallics. This code is attached to my form:

Option Explicit
Private Data As Variant
Private RangeData As Range

Public Cancelled As Boolean


Private Sub cmdClear_Click()
  Cancelled = True
  Me.Hide
End Sub

Private Sub cmdSubmit_Click()

  Cancelled = False
  SaveRecord
End Sub

Private Sub ExpForm_Initialize()

With Range("expenditures")
      Set RangeData = .Rows(5)
End Sub

Private Sub ExpForm_QueryClose(Cancel As Integer, _
  CloseMode As Integer)

  If (CloseMode = vbFormControlMenu) Then
    Cancel = True
    Beep
  End If
End Sub



Private Sub SaveRecord()
   'Copy values from ExpenditureForm controls to Data array
        Data(1, 1) = txtSEQ.Value
        Data(1, 2) = cboOrgShp.Value
        Data(1, 3) = txtNsn.Value

        'Data(1, 4) = VLookup(.txtNsn, "Stock_num:AF_noun_tx", 2)
        Data(1, 5) = txtDoc.Value
        Data(1, 6) = txtLot.Value
        Data(1, 7) = txtQty.Value
        Data(1, 8) = txtCatCode.Value



   'Assign Data array values to current record in expenditure form
   RangeData.Value = Data
   Me.Hide
End Sub

Arc
 
Old April 19th, 2006, 02:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Bryan,

You need to declare the Data variable as some sort of array. You can do this in two ways; either as a fixed size array or a variable sized array.

With a fixed size array you merely need to declare how big each dimension of the array needs to be when declaring the variable, as follows:
Code:
Option Base 1

Private Data(1, 8) As Variant
or
Code:
Private Data(1 To 1, 1 To 8) As Variant
Alternatively you could declare a variable sized array, which you can reseize during run-time. This is a bit more flexible if you don't know what the size of your data may be in each run. I've attached an extract from your code including the variable sized array set-up.
Code:
Private Data() As Variant
Private RangeData As Range

Private Sub SaveRecord()
'Copy values from ExpenditureForm controls to Data array

    Set RangeData = ThisWorkbook.Sheets("Sheet1").Range("A1:H1")

    ReDim Preserve Data(1 To 1, 1 To 8)

    Data(1, 1) = "a" 'txtSEQ.Value
    Data(1, 2) = "b" 'cboOrgShp.Value
    Data(1, 3) = "c" 'txtNsn.Value
    Data(1, 4) = "d" 'Application.WorksheetFunction.VLookup(txtNsn.Value, "Stock_num!AF_noun_tx", 2, False)
    Data(1, 5) = "e" 'txtDoc.Value
    Data(1, 6) = "f" 'txtLot.Value
    Data(1, 7) = "g" 'txtQty.Value
    Data(1, 8) = "h" 'txtCatCode.Value


   'Assign Data array values to current record in expenditure form
   RangeData.Value = Data

End Sub
Hope this helps,
Maccas

 
Old April 19th, 2006, 04:39 AM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help!





Similar Threads
Thread Thread Starter Forum Replies Last Post
getting runtime error 13: type mismatch rita9 Beginning VB 6 2 March 14th, 2008 04:36 AM
Error Number 13 : Type Mismatch ryoga_7482 BOOK: Expert Access 2007 Programming ISBN 978-0-470-17402-9 2 January 31st, 2008 02:20 AM
runtime error '13' type mismatch denkidude Visual Basic 2005 Basics 5 March 15th, 2007 10:28 PM
OpenRecordset And Run-Time Error 13 Type Mismatch Pavesa Access VBA 5 March 22nd, 2005 05:20 PM
Error 13 Type Mismatch lguzman Access VBA 2 January 6th, 2005 10:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.