vendredi 17 mai 2019

For a repetitive VBA input form (e.g. 100 checkboxes, which populate 100 cells) is there a way to auto-generate the code?

I'm using Excel and VBA to create a database of people and their Hobbies/Skills. I've created a User Form, where the user can input their name (into a nameTextBox), Department (deptTextBox), and then tick which skills they have from a list of 150.

If the user clicks the 'Save and Close' command button on the form, their data should populate an Excel spreadsheet. The checkboxes correspond to 'True' or 'False'. However, there are so many, I'm wondering if it's possible to generate this code automatically (or find a simpler way to code it).

I wrote a formula in Excel to generate these lines of code automatically. However, if I past this into VBA, the code appears red and I get a message box with the error "Compile error: Expected: list separator or )".

I've tried pasting it into Notepad or WordPad first, and then into VBA, but I get the same error. This occurs even if the code and characters appear to be EXACTLY THE SAME as what I'm typing - it works if I type it, it fails if I copy-paste it.

'If the user clicks 'Save and Close', I want to save their data in Sheet3, in next available row, and then close the form Dim lRow As Long Dim ws As Worksheet Set ws = Worksheets("Sheet3") lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row With ws .Cells(lRow, 1).Value = nameTextBox.Value .Cells(lRow, 2).Value = deptTextBox.Value .Cells(lRow, 3).Value = notesTextBox.Value

            ' This next bit adds all the skills as TRUE (checkbox ticked) or FALSE (unticked). There are 150, so I'm wondering if I can generate this code automatically rather than write it all by hand!

            .Cells(lRow, 5).Value = CheckBox1.Value
            .Cells(lRow, 6).Value = CheckBox2.Value
            .Cells(lRow, 7).Value = CheckBox3.Value
            .Cells(lRow, 8).Value = CheckBox4.Value
            .Cells(lRow, 9).Value = CheckBox5.Value
            .Cells(lRow, 10).Value = CheckBox6.Value
            .Cells(lRow, 11).Value = CheckBox7.Value
            .Cells(lRow, 12).Value = CheckBox8.Value
            .Cells(lRow, 13).Value = CheckBox9.Value
            .Cells(lRow, 14).Value = CheckBox10.Value
            .Cells(lRow, 15).Value = CheckBox11.Value
            .Cells(lRow, 16).Value = CheckBox12.Value
            .Cells(lRow, 17).Value = CheckBox13.Value
            .Cells(lRow, 18).Value = CheckBox14.Value
            .Cells(lRow, 19).Value = CheckBox15.Value
            .Cells(lRow, 20).Value = CheckBox16.Value
            .Cells(lRow, 21).Value = CheckBox17.Value
            .Cells(lRow, 22).Value = CheckBox18.Value
            .Cells(lRow, 23).Value = CheckBox19.Value
            .Cells(lRow, 24).Value = CheckBox20.Value
            .Cells(lRow, 25).Value = CheckBox21.Value
            'And so on, until....
            .Cells (1Row, 151).Value = Checkbox148.Value

Expected result: When the UserForm is saved, each checkbox populates a new cell with TRUE or FALSE. Actual result: I get the error message "Compile error: Expected: list separator or )". Note that this code works when I type it by hand, but fails if I paste it in from the Clipboard, despite appearing identical.




Aucun commentaire:

Enregistrer un commentaire