0

Background:
I am trying to make a template in excel to import data in a database storage system held in Access. I am trying to create some validation lists for the data using already present variables in the database selected via SQL. Each column to be imported needs a header with 5 items, each of which needs to be validated separately.

I have SQL queries that populate sheets with the contents of each of the validation lists. These are in the format of a single column of strings. (Ask for code if you want it, I will leave it out for now as this isn't the problem).

I am using Excel 2007 .xlsm

Problem:
So, my problem is trying to get an xlvalidatelistin the relevant cell, that validates based on the list of data taken out of access, held within a separate sheet. Originally I was trying to create a comma delimited string to use for the validation, but I came up against a problem of the 255 character limit in a string. I am now trying to reference the list of strings in the other sheet directly. I would also like the validation list to accept the contents of the cell being validated; to allow the user to add a new value not in the database (I have code to handle this case when the file is imported into the database).

Code:

Public Function CreateValidationLists(ByVal SheetName As String, Row As Integer)

Dim RowCounter As Integer
Dim Colcounter As Integer

RowCounter = Row


For Colcounter = 2 To 256
   With ActiveWorkbook.Worksheets("Import Data").Cells(RowCounter, Colcounter)
        With .Validation
             .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="Sites!" & Range(Worksheets("Sites").Cells(1, 1), Worksheets("Sites").Cells(1, 1).End(xlDown)).Address
        End With
    End With
Next

End Function

Error
The Error I get is on the line .Add Type:=xlValidateL...
The Error message is
Run-time error '1004':
Application-defined or object-defined error

Any help greatly appreciated. If anyone has any ideas of a better way to go about this I am open to suggestions.

3
  • 1
    It might be easier if your validation lists were created via a Data>Connection to Access, which results in a table. You can then create a named range that's to a column in that table. Point the data validation at the named range and it will re-size dynamically. I've done this and it works nicely. Commented May 30, 2013 at 16:19
  • Is it possible to create a Data Connection in vba? The database location may vary for different PCs. When i create a connection from the ribbon, it goes under the header "This PC", rather than networked connections.
    – Pezzzz
    Commented May 31, 2013 at 7:11
  • It's possible. I'd start by recording a macro that creates a connection. In addition to looking at the resulting VBA, look at the Connection String, Command Text and other items in the Connection Properties window that you get when right-clicking the Table and choosing External Data Properties. Commented May 31, 2013 at 15:48

1 Answer 1

1

The only thing you missed is equal sign in you formula like this:

'....your code here
Formula1:="=Sites!" & Range(Worksheets("Sites").Cells(1, 1), Worksheets("Sites").Cells(1, 1).End(xlDown)).Address
'.... your code here

You idea is quite correct and I like it. You could pass values directly from array. Here is sample code which you could use:

Sub ValueFromArray()
'testing code!
    Dim myArray
        myArray = Array("A", "B", "C", "D")

    With ActiveWorkbook.Worksheets("Import Data").Cells(1, 2)
        With .Validation
             .Add Type:=xlValidateList, _
             AlertStyle:=xlValidAlertInformation, _
             Formula1:=Join(myArray, ", ")
        End With
    End With
End Sub

Look into Join function which is required in the code above.

5
  • I have added "=" as you have suggested and I still get the same error '1004'. I will add more details to the question
    – Pezzzz
    Commented May 30, 2013 at 16:06
  • if so please check names of your sheets (if code match to one in excel app). I did check your code using names of the sheets you have and it works. Commented May 30, 2013 at 16:08
  • I have double checked the sheet names (These names are the sheet names displayed at the bottom of the workbook). What version of Excel are you using? I have 2007.
    – Pezzzz
    Commented May 30, 2013 at 16:12
  • I can confirm, it's working in my both 2007 & 2010 Excel Applications. Commented May 30, 2013 at 16:14
  • Ok, It is time for me to go home, I will have a look at this again tomorrow, I probably have a simple error somewhere. Thanks for your help.
    – Pezzzz
    Commented May 30, 2013 at 16:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.