Excel VBA赢得';除非从文本转换为列,否则不能在单元格上运行
我有一个VBA,可以选择特定的列来创建图表。我遇到了一个问题,某些列将从图表中省略,我不知道为什么。排除故障后,我发现一旦省略的列从文本转换为列,它们就可以工作了。知道为什么吗 我曾尝试使用VBA将每一列从文本转换为另一列,但出现了一个错误 …一次只能转换一列 一次做一个会花很多时间,因为我有几百个专栏要做。是否有VBA可以快速处理此问题 以下是我创建图表的代码(如果有帮助):Excel VBA赢得';除非从文本转换为列,否则不能在单元格上运行,excel,vba,text-to-column,Excel,Vba,Text To Column,我有一个VBA,可以选择特定的列来创建图表。我遇到了一个问题,某些列将从图表中省略,我不知道为什么。排除故障后,我发现一旦省略的列从文本转换为列,它们就可以工作了。知道为什么吗 我曾尝试使用VBA将每一列从文本转换为另一列,但出现了一个错误 …一次只能转换一列 一次做一个会花很多时间,因为我有几百个专栏要做。是否有VBA可以快速处理此问题 以下是我创建图表的代码(如果有帮助): Sub Graph2() ' Graphs for monitoring Dim my_range A
Sub Graph2()
' Graphs for monitoring
Dim my_range As Range, t, co As Shape
t = Selection.Cells(1, 1).Value & " - " & ActiveSheet.Name
Dim OldSheet As Worksheet
Set OldSheet = ActiveSheet
Set my_range = Union(Selection, ActiveSheet.Range("A:A"))
Set co = ActiveSheet.Shapes.AddChart2(201, xlLine) 'add a ChartObject
With co.Chart
.FullSeriesCollection(1).ChartType = xlXYScatter
.FullSeriesCollection(1).AxisGroup = 1
.FullSeriesCollection(2).ChartType = xlLine
.FullSeriesCollection(2).AxisGroup = 1
.SetSourceData Source:=my_range
'highlight final dot of data
.FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count - 1).ApplyDataLabels Type:=xlShowValue
.HasTitle = True
.ChartTitle.Text = t
'ResolveSeriesnames co.Chart
.Location Where:=xlLocationAsObject, Name:="Graphs"
End With
OldSheet.Activate
End Sub
这是我的答案
目的:获取列列表,并尽可能快地逐个应用
Range.TextToColumns
方法
算法:1.创建所需列的数组
2.逐列浏览此数组并:
-2.1检查右侧是否有数据
-2.2确保插入足够的列以保留右侧的数据
-2.3应用
Range.TextToColumns
方法
测试日期:200行200列的范围,填充
“样本数据”
文本,并随机插入“样本数据”
文本,以测试不同的分隔符数量。使用空格作为分隔符:代码:
Sub SplitColumns()
Dim rToSplit() As Range, r As Range
Dim i As Long, j As Long, k As Long
Dim sht As Worksheet
Dim delimiter As String
Dim consDelimiter As Boolean
Dim start As Single, total As Single
Dim delimitersCount() As Long
'========================== TESTING STUFF =======================================
' set working sheet
Set sht = ThisWorkbook.Sheets("Sheet2")
' re-create sample data (it is changed on each macro run)
sht.Cells.Clear
ThisWorkbook.Sheets("Sheet2").Cells.Copy Destination:=sht.Cells(1, 1)
' timer for testing purposes - start point
start = Timer
'======================== END OF TESTING STUFF ===================================
' Set the delimiter
' I've used space
delimiter = " "
' assign a ConsecutiveDelimiter state
consDelimiter = False
Application.ScreenUpdating = False
'=================== CREATING A LIST OF COLUMNS FOR SPLIT ========================
' create an array of columns to be changed
' at this sample I take all 200 columns
' you have to assign your own range which is to be splitted
With sht
For i = .Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
' add columns to an array
If Not .Cells(1, i) = "" Then
ReDim Preserve rToSplit(j)
Set rToSplit(j) = Range(.Cells(1, i), .Cells(Rows.Count, i).End(xlUp))
j = j + 1
End If
Next
End With
'=============== END OF CREATING A LIST OF COLUMNS FOR SPLIT ======================
'============================= PERFORMING SPLIT ===================================
' go through each column in array
' from left to right, because
' there may be a need to insert columns
For j = LBound(rToSplit) To UBound(rToSplit)
' check whether there is any data on the right from the top cell of column
' note - I'm checking only ONE cell
If Not rToSplit(j).Cells(1, 1).Offset(0, 1) = "" Then
' creating another array:
' purpose - check cells in column
' and count quantity of delimiters in each of them
' quantity of delimiters = quantity of columns to insert
' in order not to overwrite data on the right
For Each r In rToSplit(j).Cells
ReDim Preserve delimitersCount(k)
delimitersCount(k) = UBound(Split(r.Text, delimiter))
k = k + 1
Next
' get the maximun number of delimiters (= columns to insert)
For i = 1 To WorksheetFunction.Max(delimitersCount)
' and insert this quantity of columns
rToSplit(j).Cells(1, 1).Offset(0, 1).EntireColumn.Insert
Next
' split the column, nothing will be replaced
rToSplit(j).TextToColumns Destination:=rToSplit(j).Cells(1, 1), ConsecutiveDelimiter:=consDelimiter, Tab:=False, Semicolon:=False, Comma:=False, _
Space:=False, Other:=True, OtherChar:=delimiter
Else
' here I just split column as there is no data to the right
rToSplit(j).TextToColumns Destination:=rToSplit(j).Cells(1, 1), ConsecutiveDelimiter:=consDelimiter, Tab:=False, Semicolon:=False, Comma:=False, _
Space:=False, Other:=True, OtherChar:=delimiter
End If
' clear the delimiters count array
Erase delimitersCount
' go to next column
Next
' done
'========================= END OF PERFORMING SPLIT ===================================
' timer for testing purposes - time difference in seconds
total = Timer - start
Debug.Print "Total time spent " & total & " seconds."
Application.ScreenUpdating = True
End Sub
希望有帮助。产生错误的代码在哪里?没有“错误”,代码运行正常。但是,除非将列从文本转换为列,否则代码将不会在所选列上运行,因此我的图表会丢失重要数据。因此,我希望先逐个转换所需的列,然后运行图表子部分。我有大约200个要转换。当然有一个更快的方法@vitaliyprushak当然有,但是你必须付出一些努力来实现它,稍后检查我的答案。