【发布时间】:2020-04-10 02:47:15
【问题描述】:
总的来说,我对 VBA 不太熟悉,与我习惯的(主要是汇编语言)相比,Excel 编程有点让我失望。
基本上,我工厂的一台机器将它记录的所有数据放入 Excel 表中;它标记所有内容并记录每个数据,但它将所有数据保留在第一列中每个周期的单个单元格中,因此我每天都会归档大约 500 行。我正在尝试分析和分离每个单元格中的数据,以便我可以绘制它并希望证明我遇到的一些问题的解决方案。
任何帮助我创建某种程序我可以运行以将多个 Excel 工作表中的数据拆分到同一个 Excel 工作表但分成多个列,将不胜感激。请注意,所有不同的数据都用分号分隔。
所以我使用以下数据循环浏览给定文件夹中的所有 Excel 工作表。我从另一个开发者那里得到了这段代码:
Sub AllWorkbooks()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
'Replace the line below with the statements you would want your macro to perform
Call Filter
Call Rearrange
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub
Sub Filter()
With ActiveSheet.UsedRange
.Columns.AutoFit
.Rows.AutoFit
End With
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If
End Sub
Sub Rearrange()
'
' Rearrange Macro
' Split all the data in the individual cells in the first column into individual columns.
'
' Keyboard Shortcut: Ctrl+Shift+R
'
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Rows("1:1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array _
(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), _
Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1)), TrailingMinusNumbers:=True
Columns("A:A").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Rows("1:1").Select
Range("G1").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("G1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
ActiveSheet.Range("$G$1:$AC$8000").AutoFilter Field:=1
Range("G1").Select
End Sub
问题是,我无法在我的一生中获得应用过滤器的第一行,也无法获得 AutoFit 的列。这里有什么建议吗?
【问题讨论】:
-
只需使用内置的Split text into different columns with the Convert Text to Columns Wizard。如果您需要一些 VBA 代码,请使用宏记录器作为第一个想法并根据您的需要改进该代码。如果您遇到困难或错误返回您的代码并向其提问。
-
这取决于放在第一个单元格中的数据格式。它是由一些分隔符分隔还是具有固定长度?如果您在问题中包含一些示例,将会有所帮助。
-
@z32a7ul 明显用分号隔开:“请注意,所有不同的数据都用分号隔开。”
-
@Pᴇʜ,我有成千上万的 Excel 工作表要过滤,我需要很长时间才能为每一张工作表做到这一点。这就是为什么我尝试编写一个宏来为我做这件事,并希望一次做不止一张。
-
好吧,请理解这不是免费的代码编写服务(见How to Ask和No attempt was made)。所以在问你之前,你应该做一些研究并自己尝试一些东西。一个很好的起点是宏记录器使用向导手动执行并将其记录为宏。然后开始围绕它构建一个循环,循环遍历所有工作表。试一试,然后将您拥有的代码包含在您的问题中,并提出与之相关的问题。