我希望能够将x个文件放在一个文件夹中,并将它们全部放在同一工作簿的单独工作表上。
我发现这段代码有效;
Public Sub consolWB()
Dim FSO As Object
Dim folder As Object, subfolder As Object
Dim wb As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
folderPath = "C:\Users\patrickw\Desktop\exceltest"
Set folder = FSO.GetFolder(folderPath)
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
.AskToUpdateLinks = False
End With
For Each wb In folder.Files
If Right(wb.Name, 3) = "xls" Or Right(wb.Name, 4) = "xlsx" Or Right(wb.Name, 4) = "xlsm" Then
Set openWB = Workbooks.Open(wb)
For Each ws In openWB.Worksheets
ws.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next ws
openWB.Close
End If
Next wb
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
.AskToUpdateLinks = True
End With
End Sub
但是,这只能起作用,因为我已设置目标文件路径以便查看。
我想创建一个按钮,提示用户输入文件路径,然后使用该文件路径引入文件。
我想我必须输入一个字符串,然后在上面的代码中使用这个变量。
这可能吗?
在上面的示例中,新工作表名为'sheet1 ...'是否可以自己命名?
由于
答案 0 :(得分:1)
在button_click事件中包含以下内容,然后在其他代码中使用path变量。
Dim path As String
path = InputBox("Enter a file path")
如果您需要访问表单外的变量,则可以将path
声明为模块级Public
字段:
Public path As String
Private Sub CommandButton1_Click()
path = InputBox("Enter a file path")
End Sub
Private Sub CommandButton2_Click()
Debug.Print path
End Sub
或者更好的是,如果path
不能从表单外部分配,则使用属性封装它:
Private path As String
Public Property Get SelectedPath() As String
SelectedPath = path
End Property
Private Sub CommandButton1_Click()
path = InputBox("Enter a file path")
End Sub
这样,表单外的代码可以读取 SelectedPath
,但不能写到私有字段或修改属性的值。
这就是考虑InputBox
的{{3}},以获得更好的用户体验和更轻松的输入验证。
答案 1 :(得分:1)
这样,用户只需点击,点击即可。您不必检查输入文件夹路径是否正确。
sensor: (-0.560303, -0.627686, 0.467468)
sensor: (-0.561829, -0.626160, 0.466125)
sensor: (-0.556091, -0.623352, 0.471497)
sensor: (-0.558411, -0.625977, 0.468811)
sensor: (-0.557312, -0.626587, 0.468262)
sensor: (-0.557800, -0.625854, 0.465820)
sensor: (-0.563599, -0.624512, 0.464722)
sensor: (-0.555847, -0.623230, 0.467163)
sensor: (-0.557861, -0.621033, 0.468811)
sensor: (-0.555420, -0.625061, 0.470520)
sensor: (-0.559082, -0.626221, 0.475891)
sensor: (-0.559814, -0.625977, 0.466309)
sensor: (-0.561768, -0.624756, 0.467163)
sensor: (-0.551941, -0.628906, 0.469055)
sensor: (-0.556946, -0.626465, 0.471313)
sensor: (-0.558533, -0.626038, 0.469421)
sensor: (-0.557922, -0.625061, 0.467285)
sensor: (-0.562622, -0.623657, 0.469971)
sensor: (-0.554443, -0.625977, 0.465759)
sensor: (-0.559265, -0.626282, 0.471619)
sensor: (-0.558716, -0.625427, 0.471375)
sensor: (-0.559143, -0.626526, 0.467468)
sensor: (-0.554749, -0.626221, 0.468079)
sensor: (-0.554626, -0.622681, 0.467285)
sensor: (-0.557983, -0.625549, 0.475464)
sensor: (-0.555603, -0.626343, 0.466980)
sensor: (-0.559570, -0.625854, 0.470398)
sensor: (-0.556946, -0.626587, 0.466858)
sensor: (-0.557373, -0.626526, 0.470093)
sensor: (-0.558716, -0.629272, 0.464905)
sensor: (-0.555725, -0.625732, 0.473877)
sensor: (-0.560608, -0.626282, 0.469238)
sensor: (-0.556335, -0.626221, 0.467590)
sensor: (-0.558777, -0.623840, 0.468994)