我已经创建了dropdownbox,它指向包含.csv文件的某个目录。我可以在下拉框中看到这些文件,但是我想在点击它们后立即打开它们。如何在Visual Basic编辑器中完成此操作?这是我的下拉框代码。
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
答案 0 :(得分:2)
右键单击表单上的下拉框,然后选择&#34;查看代码&#34;。在屏幕的右上角选择&#34;更改&#34;事件并使用以下代码:
Private Sub ComboBox1_Change()
Dim WbSource As Workbook
Dim pth As String
Dim fnme As String
fnme = ComboBox1.Value
pth = "C:\Users\Test\Desktop\Macro's\"
Application.Run "CustomMacro", pth & fnme
End Sub
如果答案有效,请标记为已接受。
我知道你现在需要在打开的工作簿上运行一个宏。这可以通过几种方式完成,这是我解决问题的建议:
1)尽量远离ThisWorkbook
和ActiveWorksheet
- 使用尺寸为工作簿和工作表的变量
2)您可以通过多种方式将变量传递给宏 - 您可以将变量设置为全局变量(我建议您阅读不同级别的变量),但最简单的方法是使变量成为函数的参数。
3)对于我的解决方案,我假设您有两个工作簿,每个工作簿都有一个工作表。我将它们称为目标和源:源是从下拉菜单中选择的工作簿,目标是宏所在的目标。下拉框将记住源的路径,并将其传递给另一个宏。
Sub CustomMacro(SrceWBPath as String)
Dim WbSource as workbook
Dim WbTarget as workbook
Dim WrkShtSrc as Worksheet
Dim WrkShtTrgt as Worksheet
Set WbTarget = This.Workbook
Set WbSource = Workbooks.Open (SrceWbPath)
Set WrkShtSrc = wbSource.Worksheets("insert_name_of_the_source_worksheet_here")
Set WrkShtTrgt = wbTarget.Worksheets("insert_name_of_the_target_worksheet_here")
'Then insert the rest of your code here, calling the proper sheets as required. Also, move away from `.Select` command - it just consumes a lot of processing time. Define variable such as `Range` and perform operations on them.
End Sub