2016-04-14 37 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等等,等等 –

相关问题