VBA 将字符串位置值解析为新列
VBA parse string Place values to new columns
我有一张表格,其中包含这样的数据行。
NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=18.8200(18.4400,19.2100)
我只想获取值并将它们放在自己的单元格中,例如
207.3980 207.3980 18.8200
206.1990 206.1990 18.4400
208.5970 208.5970 19.2100
我继续收到 "ByRef Argument Mismatch" 错误。我相信与我如何定义参考单元有关。
Sub Parse_Replace()
Dim i As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Dim Col As Range
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
For i = rLastCell.Column To 1 Step -1
Col = ColLett(rLastCell.Column)
Columns(i).Cells(4) = SplitString(Col3, ",", 4)
Columns(i).Cells(5) = SplitString(Col3, ",", 5)
Columns(i).Cells(6) = SplitString(Col3, ",", 6)
Next i
End Sub
Function ColLett(Col As Integer) As String
If Col > 26 Then
ColLett = ColLett((Col - (Col Mod 26)) / 26) + Chr(Col Mod 26 + 64)
Else
ColLett = Chr(Col + 64)
End If
End Function
Function SplitString(pValue As String, pChar As String, pIndex As Integer) As Variant
Dim YString As Variant
YString = Replace(Replace(Replace(Replace(pValue, " ", ""), "=", ""), "(", ","), ")", ",")
SplitString = Split(YString, pChar)(pIndex - 1)
End Function
进程
- 建立数据列数
遍历每一列
将列索引转换为具有 ColLett
的列
设置单元格值 SplitString
循环
谢谢
编辑: 用 inteded 替换了 SplitString
值。
您在此处声明 Col
为一个范围:
Dim Col As Range
然后您尝试将 Col
设置为此处的字符串:
Col = ColLett(rLastCell.Column)
设置范围时,您必须将其设置为范围。此外,您必须使用 SET
关键字来这样做:
Set Col = <a range>
当您设置 Col
时,您只在 For
的每个循环中重复将其设置为 rLastCell.Column
。如果您只需要最后一列的列字母,请在进入 for 循环之前执行此操作。
无论如何,所有这些都是毫无意义的。在任何时候,您都不会使用您在函数中遇到困难检索的列字母。实际上,对于您正在做的事情,您不需要专栏信。专栏字母适用于人类;无论如何,列号在 VBA 中很重要。
我有一张表格,其中包含这样的数据行。
NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=18.8200(18.4400,19.2100)
我只想获取值并将它们放在自己的单元格中,例如
207.3980 207.3980 18.8200
206.1990 206.1990 18.4400
208.5970 208.5970 19.2100
我继续收到 "ByRef Argument Mismatch" 错误。我相信与我如何定义参考单元有关。
Sub Parse_Replace()
Dim i As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Dim Col As Range
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
For i = rLastCell.Column To 1 Step -1
Col = ColLett(rLastCell.Column)
Columns(i).Cells(4) = SplitString(Col3, ",", 4)
Columns(i).Cells(5) = SplitString(Col3, ",", 5)
Columns(i).Cells(6) = SplitString(Col3, ",", 6)
Next i
End Sub
Function ColLett(Col As Integer) As String
If Col > 26 Then
ColLett = ColLett((Col - (Col Mod 26)) / 26) + Chr(Col Mod 26 + 64)
Else
ColLett = Chr(Col + 64)
End If
End Function
Function SplitString(pValue As String, pChar As String, pIndex As Integer) As Variant
Dim YString As Variant
YString = Replace(Replace(Replace(Replace(pValue, " ", ""), "=", ""), "(", ","), ")", ",")
SplitString = Split(YString, pChar)(pIndex - 1)
End Function
进程
- 建立数据列数
遍历每一列
将列索引转换为具有
ColLett
的列
设置单元格值
SplitString
循环
谢谢
编辑: 用 inteded 替换了 SplitString
值。
您在此处声明 Col
为一个范围:
Dim Col As Range
然后您尝试将 Col
设置为此处的字符串:
Col = ColLett(rLastCell.Column)
设置范围时,您必须将其设置为范围。此外,您必须使用 SET
关键字来这样做:
Set Col = <a range>
当您设置 Col
时,您只在 For
的每个循环中重复将其设置为 rLastCell.Column
。如果您只需要最后一列的列字母,请在进入 for 循环之前执行此操作。
无论如何,所有这些都是毫无意义的。在任何时候,您都不会使用您在函数中遇到困难检索的列字母。实际上,对于您正在做的事情,您不需要专栏信。专栏字母适用于人类;无论如何,列号在 VBA 中很重要。