Excel VBA拆分函数在一个范围内工作
[乌德帕特] 我尝试将我的解决方案自动化,将一个单元格拆分为一行中的多个其他单元格,但在将一个单元格中的数据拆分时仍存在一些问题,如下所示: 最终版本需要从E2开始划分E列中的数据范围(E2:E) 好的,经过一些修改:Excel VBA拆分函数在一个范围内工作,excel,vba,Excel,Vba,[乌德帕特] 我尝试将我的解决方案自动化,将一个单元格拆分为一行中的多个其他单元格,但在将一个单元格中的数据拆分时仍存在一些问题,如下所示: 最终版本需要从E2开始划分E列中的数据范围(E2:E) 好的,经过一些修改: Sub g() Dim strInput As String, strOutput As String Dim stringValue As String Dim LastRowcheck As Long Dim n1 As Long
Sub g()
Dim strInput As String, strOutput As String
Dim stringValue As String
Dim LastRowcheck As Long
Dim n1 As Long
Dim i As Long
LastRowcheck = Sheets("T4").Range("E" & Rows.Count).End(xlUp).Row
Sheets("T4").Activate
For n1 = 2 To LastRowcheck
With Worksheets("T4").Cells(n1, 1)
stringValue = Sheets("T4").Cells(n1, 5).value
Sheets("T4").Cells(7, n1 + 4) = getPart(stringValue, "price", "price2")
Sheets("T4").Cells(8, n1 + 4) = getPart(stringValue, "price2", "status")
Sheets("T4").Cells(9, n1 + 4) = getPart(stringValue, "status", "min")
Sheets("T4").Cells(10, n1 + 4) = getPart(stringValue, "min", "opt")
Sheets("T4").Cells(11, n1 + 4) = getPart(stringValue, "opt", "category")
Sheets("T4").Cells(12, n1 + 4) = getPart(stringValue, "category", "code z")
Sheets("T4").Cells(13, n1 + 4) = getPart(stringValue, "code z", "", True)
End With
Next n1
Application.DisplayAlerts = False
End Sub
Function getPart(value As String, fromKey As String, toKey As String, Optional isLast As Boolean = False) As String
Dim pos1 As Long, pos2 As Long
pos1 = InStr(1, value, fromKey & ":")
If (isLast) Then
pos2 = Len(value)
Else
pos2 = InStr(pos1, value, toKey & ":") ' HERE DEBUGGER STOPS
End If
getPart = Trim$(Mid$(value, pos1, pos2 - pos1))
End Function
以下测试框架适用于关键字为文本字符串(无空格或以空格结尾)的表单的所有字符串
SpecialSpread
演示了一种使用此选项获取单个单元格并将拆分值分散到右侧单元格上的方法
Function SpecialSplit(Compound As String) As Variant
Dim pos1 As Long, pos2 As Long
Dim firstSplit As Variant
Dim elmts As Integer
Dim elmt As Integer
Dim oneel As String
firstSplit = Split(Compound, ":")
For elmt = LBound(firstSplit) To UBound(firstSplit) - 1
oneel = firstSplit(elmt + 1)
pos1 = InStr(oneel, " ")
pos2 = Len(oneel)
While pos1 > 0 And pos1 < Len(oneel) - 1
pos2 = pos1
pos1 = InStr(pos2 + 1, oneel, " ")
Wend
firstSplit(elmt) = firstSplit(elmt) & ":" & Left(oneel, pos2 - 1)
firstSplit(elmt + 1) = Right(oneel, Len(oneel) - pos2)
Next elmt
elmts = UBound(firstSplit) - LBound(firstSplit) ' one shorter
If elmts > 0 Then ReDim Preserve firstSplit(1 To elmts)
SpecialSplit = firstSplit
End Function
Sub SpecialSpread(FromCell As Range)
Dim splitSet As Variant
splitSet = SpecialSplit(FromCell(1).Text)
If UBound(splitSet) >= 0 Then
FromCell(1).Offset(0, 1).Resize(1, UBound(splitSet) - LBound(splitSet) + 1).value = splitSet
End If
End Sub
函数SpecialSplit(复合为字符串)作为变量
调暗位置1为长,位置2为长
Dim firstSplit作为变体
作为整数的Dim-elmts
Dim-elmt作为整数
像细绳一样的暗绳
firstSplit=Split(复合“:”)
对于elmt=LBound(firstSplit)到UBound(firstSplit)-1
oneel=第一次拆分(elmt+1)
pos1=仪表(一个“一”字)
pos2=Len(oneel)
而pos1>0和pos10,则重拨保留第一次拆分(1到elmts)
SpecialSplit=firstSplit
端函数
子专用表(从单元格作为范围)
Dim splitSet作为变量
splitSet=SpecialSplit(来自单元格(1).Text)
如果UBound(splitSet)>=0,则
FromCell(1).偏移量(0,1).调整大小(1,UBound(splitSet)-LBound(splitSet)+1).值=splitSet
如果结束
端接头
我可以使用stringValue=Sheets(“T4”).范围(“5,I”)作为正确的赋值吗?
这是不正确的。在Google中键入“范围vs单元格vba”。有很多链接解释了如何使用Range()
和Cells()
更正你的代码stringValue=Sheets(“T4”)。Range(“5,i”)
到stringValue=Sheets(“T4”)。cell(n1,5)。value
和good你在Kin Siang的单元格(n1,5)上有一个打字错误。好的,我更新了主题的第一篇帖子,但是getpart函数给我带来了一些麻烦。将“code z”作为关键字打破了这种模式-你确定那里的关键字不仅仅是“z”(如你的图片所示)吗?你好,非常感谢你的回答。我试图找出如何将:a=SpecialSplit(“asdf:asdf-asds:ert-qwe-d:sdfg”)交换为a=SpecialSplit(“?”),其中???是活动工作簿中的单元格地址,工作表“T4”我希望使用工作表T4中E2单元格的数据,而不是(“asdf:asdf asds:ert ert qwe d:sdfg”)。我还有一些麻烦。Sub qtest()将a作为变量,将b作为变量'a=SpecialSplit(“asdf:asdf asds:ert ert qwe d:sdfg”)SpecialSplit=Sheets(“T4”)。范围(“E”和行数)。结束(xlUp)。行活动单元格。偏移(0,1)。调整大小(1,UBound(a)-LBound(a)+1)。值=结束Sub@stasser我修改了示例子例程,使其更适合您的情况,并忽略空白输入。