+ Reply to Thread
Results 1 to 9 of 9

Excel Form (VBA) validation help needed urgently

Hybrid View

neo5000 Excel Form (VBA) validation... 09-21-2012, 11:00 AM
blobbles Re: Excel Form (VBA)... 09-21-2012, 11:33 AM
blobbles Re: Excel Form (VBA)... 09-21-2012, 11:41 AM
neo5000 Re: Excel Form (VBA)... 09-21-2012, 02:00 PM
neo5000 Re: Excel Form (VBA)... 09-25-2012, 02:30 PM
blobbles Re: Excel Form (VBA)... 09-25-2012, 10:30 PM
neo5000 Re: Excel Form (VBA)... 09-26-2012, 10:27 AM
blobbles Re: Excel Form (VBA)... 09-26-2012, 11:37 AM
neo5000 Re: Excel Form (VBA)... 09-27-2012, 09:16 AM
  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    9

    Excel Form (VBA) validation help needed urgently

    Hello all, Im customising some code I got online at the page http://www.contextures.com/xlForm02.html
    I need some help editing the code in red. Basically, the code is used in conjunction with an excel form, so the user form is on one worksheet and the data collected is stored in another sheet. Currently there's a validation check to ensure that all cells have data entered. I want to adjust it since there are specific cells I wont need data in. So i figure the key piece of code is the line
     If Application.CountA(myRng) <> myRng.Cells.Count
    so i want to adjust it to something like If Application.CountA(myRng) <> myRng.Cells.Count excluding cell D13. Im just not sure of the correct syntax. Hope i was clear enough. Thank you all in advance for your speedy responses.

    Dim historyWks As Worksheet
        Dim inputWks As Worksheet
    
        Dim nextRow As Long
        Dim oCol As Long
    
        Dim myRng As Range
        Dim myCopy As String
        Dim myCell As Range
        
        'cells to copy from Input sheet - some contain formulas
        myCopy = "D5,D7,D9,D11,D13"
    
        Set inputWks = Worksheets("Input")
        Set historyWks = Worksheets("PartsData")
    
        With historyWks
            nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
        End With
    
        With inputWks
            Set myRng = .Range(myCopy)
    
            If Application.CountA(myRng) <> myRng.Cells.Count Then
                MsgBox "Please fill in all the cells!"
                Exit Sub
            End If
        End With
    
        
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
        End With

  2. #2
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Excel Form (VBA) validation help needed urgently

    You should be able to use:

    If myRng.Count <> Application.WorksheetFunction.CountA(myRng) then

  3. #3
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Excel Form (VBA) validation help needed urgently

    Oh, if you want to remove D13, take it out of your line "myCopy = "D5,D7,D9,D11,D13"" change to myCopy = "D5,D7,D9,D11"

  4. #4
    Registered User
    Join Date
    09-21-2012
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Excel Form (VBA) validation help needed urgently

    Thanks Blobbles, i'll try it

  5. #5
    Registered User
    Join Date
    09-21-2012
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Excel Form (VBA) validation help needed urgently

    ok im still having a problem, this one may not be restricted to the validation.

    im getting an "Application Defined or Object Defined Error" error at line this line
     historyWks.Cells(nextRow, oCol).Value = myCell.Value
    So basically, the code is supposed to get the data in the MyCopy cells on the input sheet, then copy the contents to the salesdata sheet. on the sales data sheet, its supposed to start inputting the contents that were copied into cell D3, contiguously within that sheet. so it would just paste into cell D3,E3,F3,H3 Etc etc.I think i may have some syntax errors. Can anyone assist??
    again, a link to the original sample code i got online is at:
    http://www.contextures.com/xlForm02.html#edit


    Option Explicit
    
    Sub UpdateLogWorksheet()
    
        Dim historyWks As Worksheet
        Dim inputWks As Worksheet
    
        Dim nextRow As Long
        Dim oCol As Long
    
        Dim myRng As Range
        Dim myCopy As String
        Dim myCell As Range
        
        'cells to copy from Input sheet - some contain formulas
        myCopy = "D7,D9,D11,D13,I7,I9,I11,I13,N7,N9,N11,N13"
    
        Set inputWks = Worksheets("Input")
        Set historyWks = Worksheets("SalesData")
    
        With historyWks
            nextRow = .Cells(.Rows.Count, "3,2").End(xlUp).Offset(2, 1).Row
        End With
    
        With inputWks
            Set myRng = .Range(myCopy)
        
          '  If myRng.Count <> Application.WorksheetFunction.CountA(myRng) Then
                If Application.CountA(myRng) <> myRng.Cells.Count Then
                MsgBox " Please fill out all the cells "
                Exit Sub
            End If
        End With
    
        With historyWks
                With .Cells(nextRow, "3,2")
                'was b3
                End With
            
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
        End With
        
        'clear input cells that contain constants
        With inputWks
          On Error Resume Next
             With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
                  .ClearContents
                  Application.GoTo .Cells(1) ', Scroll:=True
             End With
          On Error GoTo 0
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Excel Form (VBA) validation help needed urgently

    You pretty much need to upload the sheet with all sensitive information taken out. There could be lots of reasons for this...

  7. #7
    Registered User
    Join Date
    09-21-2012
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Excel Form (VBA) validation help needed urgently

    humm, but thats the entire code their i've posted

  8. #8
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Excel Form (VBA) validation help needed urgently

    OK, its because you haven't defined the value of oCol. As such it tries to paste the data to column 0, which doesn't exist of course!

    ...
    
            oCol = 1
            
            For Each myCell In myRng.Cells
                historyWks.Cells(nextRow, oCol).Value = myCell.Value
                oCol = oCol + 1
            Next myCell
    
    ...

  9. #9
    Registered User
    Join Date
    09-21-2012
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Excel Form (VBA) validation help needed urgently

    thanks for the help Blobbles! that did the trick. have a great day!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1