2012-10-29 77 views
0

我有一個多方面的問題,關於從VBA表格插入複選框,廣播和下拉值到Excel電子表格中。我正在嘗試在我的代碼中容納幾個案例場景。從VBA表格插入複選框,廣播和下拉值到Excel電子表格

第一套我要插入的項目是一個複選框具有以下名稱/值:

Name   Value 
========== ===== 
chk_week1 1 
chk_week2 2 
chk_week3 3 
chk_week4 4 
chk_week5 5 
chk_week6 6 
chk_week7 7 
chk_week8 8 
chk_week9 9 
chk_week10 10 
chk_week11 11 
chk_week12 12 
chk_week13 13 
chk_week14 14 
chk_week15 15 

如果用戶作出選擇幾個複選框,那麼它應該在的形式1,2,4,5插入的 - 例如,如果用戶選擇chk_week1, chk_week2, chk_week4 and chk_week5


第二組項目我要插入是從無線電基團的單拾取與幀fr_Priority內followng名稱/值:

Name   Value 
========== ===== 
priority_y Yes 
priority_n No 

因此,如果用戶選擇priority_y然後Yes是插入到Excel電子表格中。


我想插入的第三組項目來自三個下拉菜單。這很簡單,但用戶需要在所有三個下拉列表中進行選擇。如果他們對偏好不感興趣,那麼他們選擇'No Preference'。如果用戶做出這個決定,那麼任何東西都不應該插入到單元中。下列名稱/值都存在:

Name   
========== 
cbo_fac1 
cbo_fac2 
cbo_fac3 

例如,如果用戶在cbo_fac1選擇的111,222,No Preferencecbo_fac2cbo_fac3然後僅​​被插入。如果選擇111,No Preference,No Preference,則僅插入111


這是我使用的是現在的代碼:

Private Sub btnSubmit_Click() 

Dim ws As Worksheet 
Dim rng1 As Range 
Set ws = Worksheets("main") 

' Copy the data to the database 
' Get last empty cell in column A 
Set rng1 = ws.Cells(Rows.Count, "a").End(xlUp) 

deptCodeSplit = Split(cbo_deptCode.Value, " ") 

rng1.Offset(1, 0) = deptCodeSplit(0) 
rng1.Offset(1, 1) = cbo_moduleCode.Value 
rng1.Offset(1, 2) = cbo_moduleName.Value 
rng1.Offset(1, 3) = txt_studentNo.Value 
rng1.Offset(1, 4) = cbo_day.Value 
rng1.Offset(1, 5) = cbo_period.Value 
' rng1.Offset(1, 6) = weeks 
rng1.Offset(1, 7) = cbo_weeks.Value 
rng1.Offset(1, 8) = cbo_semester.Value 
rng1.Offset(1, 9) = cbo_rounds.Value 
rng1.Offset(1, 10) = cbo_priority.Value 
' rng1.Offset(1, 11) = lectureStyle 
rng1.Offset(1, 12) = txt_noRooms.Value 
rng1.Offset(1, 13) = cbo_park.Value 
' rng1.Offset(1, 14) = fac 
' rng1.Offset(1, 15) = pref 
rng1.Offset(1, 16) = txt_specialReq.Value 

End Sub 

感謝這麼多提前!

回答

1

一些關於你有什麼問題的說明會幫助我給你一個更好的答案。聽起來像是你試圖填充一個尺寸未知的範圍,因爲用戶可以在第一組中選擇15個項目,在第二組中選擇1個,在第三組中選擇最多三個。所以,我會做一些陣列持有的方式,如用戶選擇的項目的第一件事..

 Dim group1 as arraylist 
     Dim group2 as arraylist 
     Dim group3 as arraylist 

     ' Go through checkboxes and add values if they are checked 
     if checkbox1.checked = true then 
     group1.add(1) 
     end if ' do that for each checkbox or if you have a checklistbox do 

     for each thing in checklistbox1 
     if thing.checked=true then 
     group1.add(checklistbox1.indexof(thing)-1) 
     end if 
     next 

做同樣類型的程序的第2組和第3組

 if not dropdown1.selecteditem = "no preference" then 
      group3.add(...) 
     end if ' so an and so forth 

現在,你可以添加1組,第2組和第3組的片狀

  Dim StartingRange as range = ws.Cells(Rows.Count, "a").End(xlUp) 
     Dim total_items as integer = (group1.items.count + group2.items.count + group3.items.count) 

我不知道你要添加的項目,該行或列中的哪一種方式,但你可以調整大小由startingrange .. 。

  startrange.resize(totalitems, 1) ' resizes range to 1 column and total items in rows 

然後

  Dim cell as variant 

      for each cell in startrange 
      cell.value = group1.item(0) 
      group1.removeat(0) ' this effectively moves the item at index 1, to index 0 
      next 

做團體

的其餘部分,如果沒有幫助,讓我知道。我的VBA有點生疏,所以我可能錯過了代碼中的一些東西,比如'Set'等等,所以我爲此道歉。

+0

我想在行中添加項目 – methuselah

+0

然後不是調整大小(totalitems,1),而是調整大小(1,totalitems) –

+0

''rng1.Offset(1,6)= weeks'是代碼段,它是指所有星期必須去的單元格。我如何將它們插入該單元格? – methuselah

相關問題