私はすでにドロップダウンボックスを作成しており、.csvファイルを含む特定のディレクトリを指しています。私はドロップダウンボックスにファイルを見ることができますが、私はそれらをクリックするとすぐに開きたいです。 Visual Basicエディタでこれをどのように達成できますか?ここに私のドロップダウンボックスのコードがあります。ドロップダウンリストでファイルを選択し、Excelで開くことはできますか?
P.S.私はVisual Basicでプログラミングを始めました。
Option Explicit
Private Sub DropdownFiles()
Dim name
For Each name In ListDirectory(Path:="C:\Users\Test\Desktop\Macro's\", AttrInclude:=vbNormal, AttrExclude:=vbSystem Or vbHidden)
Me.ComboBox1.AddItem name
Next name
End Sub
Function ListDirectory(Path As String, AttrInclude As VbFileAttribute, Optional AttrExclude As VbFileAttribute = False) As Collection
Dim Filename As String
Dim Attribs As VbFileAttribute
Set ListDirectory = New Collection
Filename = Dir(Path, AttrInclude)
While Filename <> ""
Attribs = GetAttr(Path & Filename)
' If Attribs And AttrInclude And Not (Attribs And AttrExclude) Then
If Not (Attribs And AttrExclude) Then
ListDirectory.Add Filename, Path & Filename
End If
Filename = Dir
Wend
End Function
Private Sub OpenButton()
End Sub
私が動かしているマクロ。 .csvファイルの近くのディレクトリにあります
Private Sub Macro()
'
' Macro
'
'
Dim docpath As String
docpath = "TEXT;" & ThisWorkbook.Path & "\Test.csv"
'With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Users\Test\Desktop\Macro's\Test.csv", Destination:=Range("$A$1"))
With ActiveSheet.QueryTables.Add(Connection:=docpath, Destination:=Range("$A$1"))
' docpath, Destination:=Range("$A$1"))
' .CommandType = 0
.name = "calllog"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(4, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
ActiveSheet.name = "Sheet1"
End With
Range("A1:I9999").Select
Selection.Columns.AutoFit
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("I2:I829").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("I2:I9999")
.Header = xlNo
.MatchCase = True
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("T1").Select
ActiveCell.FormulaR1C1 = "60"
Range("T1").Select
Selection.Copy
Range("F2:F829").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.00"
Range("G2:G829").Select
Range("T1").Select
Selection.Copy
Range("G2:G829").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.00"
Range("T1").Select
Selection.ClearContents
Range("A437:I829").Select
Selection.Cut Destination:=Range("A439:I831")
Range("A439:I831").Select
Range("A439:I506").Select
Range("I506").Activate
Selection.Cut Destination:=Range("A438:I505")
Range("L452").Select
Range("A2:A436,F2:F436").Select
Range("F436").Activate
ActiveSheet.Shapes.AddChart2(216, xlBarClustered).Select
ActiveChart.SetSourceData Source:=Range(_
"Sheet1!$A$2:$A$436,Sheet1!$F$2:$F$436")
ActiveSheet.Shapes("Chart 1").IncrementLeft 304.5
ActiveSheet.Shapes("Chart 1").IncrementTop -139.5
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "Aantal gebelde minuten (Inbound)"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Aantal gebelde minuten (Inbound)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 32).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 14).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 14
.Italic = msoFalse
.Kerning = 12
.name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Spacing = 0
.Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(15, 18).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 14
.Italic = msoFalse
.Kerning = 12
.name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Spacing = 0
.Strike = msoNoStrike
End With
Range("O27").Select
Range("A438:A505,F438:F505").Select
Range("F505").Activate
ActiveSheet.Shapes.AddChart2(216, xlBarClustered).Select
ActiveChart.SetSourceData Source:=Range(_
"Sheet1!$A$438:$A$505,Sheet1!$F$438:$F$505")
ActiveSheet.Shapes("Chart 2").IncrementLeft 312.75
ActiveSheet.Shapes("Chart 2").IncrementTop -61.5
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "Aantal gebelde minuten (Internal)"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Aantal gebelde minuten (Internal)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 33).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 33).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 14
.Italic = msoFalse
.Kerning = 12
.name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Spacing = 0
.Strike = msoNoStrike
End With
Range("M458").Select
Application.CommandBars("Format Object").Visible = False
Range("A507:A831,F507:F831").Select
Range("F831").Activate
ActiveSheet.Shapes.AddChart2(216, xlBarClustered).Select
ActiveChart.SetSourceData Source:=Range(_
"Sheet1!$A$507:$A$831,Sheet1!$F$507:$F$831")
ActiveSheet.Shapes("Chart 3").IncrementLeft 301.5
ActiveSheet.Shapes("Chart 3").IncrementTop -73.5
ActiveChart.ChartTitle.Select
Application.CommandBars("Format Object").Visible = False
ActiveChart.ChartTitle.Text = "Aantal gebelde minuten (Outbound)"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Aantal gebelde minuten (Outbound)"
With Selection.Format.TextFrame2.TextRange.Characters(1, 33).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 33).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 14
.Italic = msoFalse
.Kerning = 12
.name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Spacing = 0
.Strike = msoNoStrike
End With
Range("P535").Select
ActiveWindow.SmallScroll Down:=-18
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Parent.Cut
Sheets("Sheet1").Select
Sheets("Sheet1").name = "Log"
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Sheets("Sheet1").name = "Chart"
Range("A1").Select
ActiveSheet.Paste
Sheets("Log").Select
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.Parent.Cut
Sheets("Chart").Select
Range("J1").Select
ActiveSheet.Paste
Sheets("Log").Select
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.Parent.Cut
Sheets("Chart").Select
Range("E19").Select
ActiveSheet.Paste
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.1479166667, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 1").ScaleHeight 1.1770833333, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.Shapes("Chart 2").ScaleWidth 1.1416666667, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 2").ScaleHeight 1.1736111111, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveSheet.Shapes("Chart 3").ScaleWidth 1.1979166667, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 3").ScaleHeight 1.2361111111, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.PlotArea.Select
Selection.Left = 85.964
Selection.Top = 37.09
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 3").IncrementLeft 36
ActiveSheet.Shapes("Chart 3").IncrementTop -0.75
Sheets("Log").Select
End Sub
ありがとうございます!コードは機能しますが、Excelの別のインスタンスで開くので、マクロは実行されません。マクロを実行できるように、同じExcelインスタンスで.csvを開く方法はありますか? – Sub
開いているブックで実行しようとしているマクロは何ですか?元のブックとDropboxのマクロですか? –
そのブックでマクロを実行している場合は、そのマクロにファイル名を渡す必要があります。元の質問を実行しようとしているマクロとその場所で編集できますか? –