【发布时间】:2016-03-02 16:53:59
【问题描述】:
全部
我是这里的 VBA 新手,我的任务是在我的新工作中开发一些宏。目前,我正在研究一个通过文本文件的宏,应用一些格式,隔离所需的数字数据,复制它,然后将复制的信息输出到新的工作表中。
这是格式化的代码,只是为了确保我发布它:
`Perform Text-To-Columns on Column A. Delimited by the character "#"
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="#", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
`Perform Text-To-Columns on Column B. Delimited by the character ")"
Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=")", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
`Format Column B for Numbers to have zero decimal places
Selection.NumberFormat = "0"
`Filter Column B for all numbers greater than 500
Selection.AutoFilter
ActiveSheet.Range("$B$1:$B$1720").AutoFilter Field:=1, Criteria1:=">500", _
Operator:=xlAnd
`Sort Filtered numbers from lowest to highest
ActiveWorkbook.Worksheets(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(1).Sort.SortFields.Add Key:=Range( _
"B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(1).Sort
.SetRange Range("B1").EntireColumn
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
此时,我现在有 B 列,其中包含 12 位数字,这些数字因文件而异。宏的下一部分是一个循环,现在应该查看此 B 列,并开始检查 B 列的单元格以查看它们是否包含 12 位数字,如果包含,则开始将它们复制为一个范围。找到 B 中的所有 12 位数字后,应将它们全部复制,打开一个新选项卡,然后粘贴结果:
' Declare loop variables
Dim myLastRow As Long
Dim myRow As Long
Dim i As Long
Dim myValue As String
Dim myStartRow As Long
Dim myEndRow As Long
' Find last row with data in column B
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all data in column B until you find a 12 order number Number
For myRow = 1 To myLastRow
' If 12 digit entry is found, capture the row number,
' then go down until you find the first entry not 12 digits long
If (Len(Cells(myRow, "B")) = 12) And (IsNumeric(Cells(myRow, "B"))) Then
myStartRow = myRow
i = 1
Do
If Len(Cells(myRow + i, "B")) <> 12 Then
' If found, capture row number of the last 13 digit cell
myEndRow = myRow + i - 1
' Copy the selected data
Range(Cells(myStartRow, "B"), Cells(myEndRow, "B")).Copy
' Add "Results" as a new sheet for the copied Card Numbers to be pasted into
Sheets.Add.Name = "Results"
Sheets("Results").Activate
' Paste clipboard to "Results" and format the results for viewing
Range("A1").Select
ActiveSheet.Paste
Columns("A:A").EntireColumn.AutoFit
Application.CutCopyMode = False
Exit Do
Else
' Otherwise, move row counter down one and continue
i = i + 1
End If
Loop
Exit For
End If
Next myRow
无论出于何种原因,当我浏览宏时,它所做的只是捕获 B1 中的第一个值,然后将其放入结果表中。我终其一生都无法弄清楚为什么。可能是由于我应用了过滤吗?如果有人能给我一些见解,我会全神贯注。非常感谢您提供的任何帮助。
【问题讨论】:
-
B2中值的长度是 12吗?
-
不。在这个特定的示例中,B1-B130 的长度都应该为 12。我确实注意到它在经过一次之后就退出了 DO 循环,但我无法解释原因。
-
您是否对 B1 进行过简单测试,以确保 Len(Cells(myRow + i, "B")) 确实等于 12 并且最后没有空格?