我試圖用打開的檔案填充串列框。整個程序會將選定的作業表從現有檔案匯出到另一個作業簿,并將日期時間戳附加到同一目錄中。它根據表單上串列框中的專案構建此匯出名稱和檔案。
宏將從 Personal.XLSB 運行;問題是這變成了活動作業簿,匯出的檔案從它繼承了路徑和檔案名。我想通過讓用戶從串列框中選擇檔案名來構建匯出名稱,但我不希望 Personal.XLSB 顯示在串列中,此外,一旦用戶選擇檔案名(從打開的串列中檔案)我想讓該檔案成為活動檔案。我花了三個小時處理了許多變體 INSTR(使用數字或僅使用文本)甚至文本比較,或者在下面的“instr”中使用“PERSONAL.xlsb”,但我根本無法讓它作業。曾經有過這樣的日子嗎????
任何人都可以提供任何見解?
代碼如下:
' Populate the open file name list box excluding Personal.xlsb
Dim wbOpen As Workbook
Dim wbopenText As String
ListBoxOpenFiles.Clear
For Each wbOpen In Workbooks
wbopenText = wbOpen.Name
If InStr(1, wbopenText, "Per") = 0 Then
ListBoxOpenFiles.AddItem wbOpen.Name ' add the name to the list
MsgBox wbOpen.Name & "Added to list"
End If
If InStr(1, wbopenText, "Per") = 1 Then ' If "Personal" is NOT found skip
End If 'Loop again
Next
' next step - make the existing file the active workbook
'still to do
uj5u.com熱心網友回復:
所有這些都應該有效(我是中間變數的忠實粉絲,但我認為wbopenText在這種情況下不需要該變數):
If StrComp("ABC", "personal.xlsb", vbTextCompare) = 0 Then
ListBoxOpenFiles.AddItem wbOpen.Name
End If
If wb.Name <> "PERSONAL.XLSB" Then
ListBoxOpenFiles.AddItem wbOpen.Name
End If
If InStr(wb.Name, "PER") = 0 Then
' Dangerous as it would also skip "MySuperWorkbook"
ListBoxOpenFiles.AddItem wbOpen.Name
End If
If InStr(1, wb.Name, "per", vbTextCompare) = 0 Then
' Also dangerous as it would also skip "MySuperWorkbook"
ListBoxOpenFiles.AddItem wbOpen.Name
End If
If不需要你的第二個(除非你想對個人作業簿做一些特別的事情)。
有關說明,請參閱https://stackoverflow.com/a/45216693/7599798。
要告訴您如何從 ListBox 中獲取所選專案,您需要告訴我們串列框的位置:在作業表上?在用戶表單上?假設您可以自己獲取所選名稱并將其放入變數中SelectedWorkbook
Dim wb As workbook
set wb = workbooks(SelectedWorkbook)
wb.Activate
Debug.Print wb.FullName
uj5u.com熱心網友回復:
謝謝你。我嘗試了許多變體,包括您發送的變體。然后一分錢掉了,我的檔案名是“PERSONAL.xlsb”我將代碼更改為:
新代碼如下
'填充打開的檔案名串列框,不包括 Personal.xlsb Dim wbOpen As Workbook Dim wbopenUcase As String
ListBoxOpenFiles.Clear
For Each wbOpen In Workbooks
wbopenUcase = UCase(wbOpen.Name) ' convert name to uppercase just in case Personal XLSB has different cases
If wbopenUcase <> "PERSONAL.XLSB" Then
ListBoxOpenFiles.AddItem wbOpen.Name ' add the name to the list
End If 'Loop again
Next
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/369559.html
上一篇:當只選擇一個單元格時,為什么“ActivecellisSelection”是假的?
下一篇:按降序排序圖表
