2016-04-14 35 views
0

我有一個窗體,當initilised搜索我們的服務器上的文件,並通過填充組合框:用戶窗體中返回第一個值在組合框中(EXCEL 2013)

cmbReportDateTime.AddItem Format(reportDateTime, "ddd dd-mmm-yy hh00") 

有誰知道,如果我能找回稍後在腳本中,組合框的第一個值(人口宏完成後)。

我試過類似於下面的東西(如其他網站所建議的),但它不起作用。

me.cmbReportDateTime.Item(1).value 
me.cmbReportDateTime.ItemData(1).value 

全碼:

Private Sub populate_reportDateTimes() 
DoEvents 
Application.StatusBar = "Searching for archived files... Please Wait..." 
ii = 1 
ThisWorkbook.Worksheets("Lookups").Range("Z1:Z100").ClearContents 
    cmbReportDateTime.Clear 
    currentDateTime = Now 
    reportDateTime = currentDateTime 

    While reportDateTime > currentDateTime - 1 
     report_hour = Hour(reportDateTime) 
     If report_hour > 20 Then 
      reportDateTime = Round(reportDateTime) + 20/24 
     ElseIf report_hour < 7 Then 
      reportDateTime = Round(reportDateTime - 1) + 20/24 
     End If 
     If Dir(r04Dir & r04Prefix & 1 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 2 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 3 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 4 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 5 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 6 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & r04Prefix & 7 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Uddingston_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Stockport_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Oldbury_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Leicester_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(feedsDir & duplicatePrefix & Format(reportDateTime, "yyyymmdd_hh00") & ".txt") <> "" And _ 
      Dir(feedsDir & unmasteredprefix & Format(reportDateTime, "yyyymmdd") & ".txt") <> "" And _ 
      Dir(feedsDir & unpinnedPrefix & Format(reportDateTime, "yyyymmdd") & ".txt") <> "" And _ 
      Dir(feedsDir & mismatchPrefix & Format(reportDateTime, "yyyymmdd_hh00") & ".txt") <> "" Then 

      cmbReportDateTime.AddItem Format(reportDateTime, "ddd dd-mmm-yy hh00") 
      ThisWorkbook.Worksheets("Lookups").Range("Z" & ii).Value = Format(reportDateTime, "ddd dd-mmm-yy hh00") 
      ii = ii + 1 
     ElseIf Dir(r04Dir & "Archive\" & r04Prefix & 1 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 2 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 3 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 4 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 5 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 6 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(r04Dir & "Archive\" & r04Prefix & 7 & "_GAS_*" & UCase(Format(reportDateTime, "dd_mmm_yyyy_hh")) & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Uddingston_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Stockport_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Oldbury_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(stpDir & stpPrefix & "Leicester_" & Format(reportDateTime, "yyyymmddhh") & "*.csv") <> "" And _ 
      Dir(feedsDir & "archive\" & duplicatePrefix & Format(reportDateTime, "yyyymmdd_hh00") & ".txt") <> "" And _ 
      Dir(feedsDir & "archive\" & unmasteredprefix & Format(reportDateTime, "yyyymmdd") & ".txt") <> "" And _ 
      Dir(feedsDir & "archive\" & unpinnedPrefix & Format(reportDateTime, "yyyymmdd") & ".txt") <> "" And _ 
      Dir(feedsDir & "archive\" & mismatchPrefix & Format(reportDateTime, "yyyymmdd_hh00") & ".txt") <> "" Then 

      cmbReportDateTime.AddItem Format(reportDateTime, "ddd dd-mmm-yy hh00") & " (archive)" 
      ThisWorkbook.Worksheets("Lookups").Range("Z" & ii).Value = Format(reportDateTime, "ddd dd-mmm-yy hh00") 
      ii = ii + 1 
     End If 
      reportDateTime = reportDateTime - 1/24 
    Wend 
Application.StatusBar = "Search Complete!" 

End Sub 

回答

0

所有在這裏你去

Public thestuff As String 

Private Sub CommandButton1_Click() 
    With Me.ComboBox1 
     .AddItem "Stuff1" 
     .AddItem "Stuff2" 
     .AddItem "Stuff3" 
     .AddItem "Stuff4" 
    End With 

    thestuff = ComboBox1.List(0) 
End Sub 

Private Sub CommandButton2_Click() 
    Debug.Print ; thestuff 
    ActiveWorkbook.Sheets("Sheet1").Range("a1").Value = thestuff 
End Sub 

這解決了問題,並讓你你想要的。注意「thestuff」被張貼在需要被引用的模塊之上。

+0

嗨,道格,我試着'Debug.Print Me.cmbReportDateTime.ListIndex = 0'來看看什麼值會返回,它說「假」 – Tabias

+0

就這樣我清楚 - 重新讀我的帖子後,我沒有想到這是有道理的。我不想讓組合框顯示第一個條目,我只想檢索第一個條目的值,而框本身對用戶保持空白,直到用戶自己選擇一個值。 – Tabias

+0

嗯,組合框的第一個值總是爲零。一個空白部分是-1等等,等等 –

相關問題