2016-12-07 12 views
0

Excelスプレッドシートからデータをインポートする必要があります。スプレッドシートには複数のワークシートがありますが、私はコンポーネントのワークシートにのみ興味があります。ワークシート名は毎月変わりますが、常に「コンポーネント」から始まります。プログラムで正しいワークシートを見つける方法を探しています。毎月ワークシート名が異なるExcelスプレッドシート

解決策は、ワークシートのコード名プロパティを取得することにありますが、その方法はわかりません。

FWIW、私はスプレッドシートを制御しないため、名前付き範囲の使用(それらを使用しない)やワークシートの命名規則を制御することはできません。

Private Sub cmdGetFile_Click() 
'Import components spreadsheet into components table 
    Dim fDlg As FileDialog 
    Dim flNme As String 
    Dim flChsn As Integer 

    Set fDlg = Application.FileDialog(msoFileDialogOpen) 

    fDlg.Title = "Select Products & Components file" 
    flChsn = fDlg.Show 
    fDlg.FilterIndex = 1 

    If flChsn <> -1 Then 
     MsgBox "No file selected" 
    Else 
     flNme = fDlg.SelectedItems(1) 
    End If 

    DoCmd.TransferSpreadsheet , acSpreadsheetTypeExcel12, "component", flNme, -1, [this is where I need help] 
End Sub 
+0

常に左から右に同じ索引にシートである:だからここ

は、最終的なコードはありますか? – NiVeR

答えて

0

私は(それは常にシート3です)ワークシートのコード名のプロパティを知っているので、私は手動でダイアログボックスに名前を入力するプロセスを経るシート番号を参照していないことができています。

ウェイン・G・ダンに感謝します。あなたの明快で丁寧な答えが、私にとって欠けているものをいくつか記入するのを助けました!スプレッドシート名を検索し、表示し、ユーザーの入力を許可するコードは、将来の使用のための素晴らしい例として保存されます。

Option Compare Database 
Option Explicit 

Private Sub cmdGetFile_Click() 
'Import components spreadsheet into components table 
Dim fDlg As FileDialog 
Dim flNme As String 
Dim flChsn As Integer 

Set fDlg = Application.FileDialog(msoFileDialogOpen) 

fDlg.Title = "Select Products & Components file" 
flChsn = fDlg.Show 
fDlg.FilterIndex = 1 

If flChsn <> -1 Then 
    MsgBox "No file selected" 
Else 
    flNme = fDlg.SelectedItems(1) 
End If 

' Open the Workbook and display a list of sheet names 
Dim excelApp As Excel.Application 
Dim oWB   As Excel.Workbook 
Dim oWS   As Excel.Worksheet 
Dim strSheets As String 
Dim strRange As String 
Dim i   As Integer 
Dim strSheet As String 
Dim iLastrow As Long 
Dim iLastCol As Long 

Set excelApp = New Excel.Application 
Set oWB = excelApp.Workbooks.Open(flNme) 
excelApp.Visible = False 'Don't need to see the workbook 

' Sheet 3 is the worksheet that I want to import 
i = 3 
strSheet = strSheets & oWB.Worksheets(3).Name 

' Get Cell Range starting in A1 
Set oWS = oWB.Sheets(strSheet) 

' Get last used row 
iLastrow = oWS.Cells(oWS.rows.Count, 1).End(xlUp).Row 

' Build Import range (Sheet & cells) 
strRange = strSheet & "!A1:L" & iLastrow ' i.e. "SheetName!A1:P25" 

oWB.Close SaveChanges:=False  ' Close, don't save any changes 
Set oWB = Nothing 
Set excelApp = Nothing 

' Import worksheet 
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "component", flNme, -1, strRange 


End Sub 
1

シートの名前を「推測」するパターンはありますか?すなわち、それは「構成要素」で始まる唯一のシートであろうか?シートの1つのセルに日付がありますか?英語では、見たい場合にシートを選択する方法を説明します。

以下は、ブックのシート名をすべて一覧にしてから、シート名を入力するように求められます。識別方法を定義できる場合は、コードを変更して識別することができます。

Option Compare Database 
Option Explicit 

Private Sub cmdGetFile_Click() 
    'Import components spreadsheet into components table 
    Dim fDlg As FileDialog 
    Dim flNme As String 
    Dim flChsn As Integer 

    Set fDlg = Application.FileDialog(msoFileDialogOpen) 

    fDlg.Title = "Select Products & Components file" 
    flChsn = fDlg.Show 
    fDlg.FilterIndex = 1 

    If flChsn <> -1 Then 
     MsgBox "No file selected" 
    Else 
     flNme = fDlg.SelectedItems(1) 
    End If 

    ' Open the Workbook and display a list of sheet names 
    Dim excelApp As Excel.Application 
    Dim oWB   As Excel.Workbook 
    Dim oWS   As Excel.Worksheet 
    Dim strSheets As String 
    Dim strRange As String 
    Dim i   As Integer 
    Dim strSheet As String 
    Dim iLastrow As Long 
    Dim iLastCol As Long 

    Set excelApp = New Excel.Application 
    Set oWB = excelApp.Workbooks.Open(flNme) 
    excelApp.Visible = True 

    ' Get all Sheet Names 
    strSheets = "" 
    For i = 1 To oWB.Worksheets.Count 
     strSheets = strSheets & oWB.Worksheets(i).Name & vbCr 
    Next i 

AskAgain: 
    ' Display the list 
    MsgBox "List of all worksheet names:" & vbCrLf & strSheets 

    strSheet = InputBox("Please enter the name of the Worksheet to import.", "Sheet Name?") 
    If InStr(1, strSheets, strSheet) = 0 Then 
     MsgBox "You entered a sheet name that does not exist.", vbOKOnly, "Unknown Sheet Name" 
     GoTo AskAgain 
    End If 

    ' Get Cell Range... Assume range starts in A1???? 
    Set oWS = oWB.Sheets(strSheet) 

    ' Get last used row 
    iLastrow = oWS.Cells(oWS.rows.Count, 1).End(xlUp).Row 
    ' Build Import range (Sheet & cells) 
    strRange = strSheet & "!A1:BM" & iLastrow     ' i.e. "SheetName!A1:P25" 

    oWB.Close SaveChanges:=False  ' Close, don't save any changes 
    Set oWB = Nothing 
    Set excelApp = Nothing 

    ' Import worksheet 
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "component", flNme, -1, strRange   '[this is where I need help] 


End Sub 
+0

タブの名前は、特定のパターン「コンポーネントm-d」に従います。例:コンポーネント11-23。私が手動でそれを行うつもりなら、コンポーネントの11-23タブを探します。 – Greg