gpt4 book ai didi

用于将 "&&","&&-","&-"等符号和数字提取到不同列中的 VBA 代码

转载 作者:行者123 更新时间:2023-12-04 09:01:33 24 4
gpt4 key购买 nike

我有一张工作表,其中包含 A 列中的值范围,例如“5670&&2”、“1281&&-3&-5&&7”...等。

请帮助我以下列方式提取 VBA 中的输出:

例如 5670&&2 我要求 A1 单元格包含 5670,B1 单元格包含 &&,C1 单元格包含 2。

例如1281&&-3&-5&&7,我要求A1单元格包含1281,B1单元格包含&&-,C1单元格包含3,D1单元格包含&-,E1单元格包含5,F1单元格包含&&,G1单元格包含7.

请帮忙。

谢谢,

最佳答案

在这里,我尝试编写代码来将数字与非数字分开。数字和非数字被复制到不同的列,如 Excel 文本到列。代码有点疯狂,如果你需要我会提供评论。作为输入,使用 ActiveSheet.UsedRange.Columns(1).Cells。

Option Explicit

Sub SeparateNumbers()
Dim targetRange As Range
Dim cellRange As Range
Dim charIndex As Integer
Dim oneChar As String
Dim nextChar As String
Dim start As Integer
Dim copiedCharsCount As Integer
Dim cellValue As String
Dim columnIndex As Integer

Set targetRange = ActiveSheet.UsedRange.Columns(1).Cells

For Each cellRange In targetRange
columnIndex = cellRange.Column
start = 1
copiedCharsCount = 0
cellValue = cellRange.Value
If (VBA.Strings.Len(cellValue) <= 1) Then GoTo nextCell

For charIndex = 2 To Len(cellValue)
oneChar = VBA.Strings.Mid(cellValue, charIndex - 1, 1)
nextChar = VBA.Strings.Mid(cellValue, charIndex, 1)
If VBA.IsNumeric(oneChar) And VBA.IsNumeric(nextChar) Then GoTo nextCharLabel
If Not VBA.IsNumeric(oneChar) And Not VBA.IsNumeric(nextChar) Then GoTo nextCharLabel

cellRange.Offset(0, columnIndex).Value = VBA.Strings.Mid(cellValue, start, charIndex - start)
columnIndex = columnIndex + 1
copiedCharsCount = copiedCharsCount + (charIndex - start)
start = charIndex

nextCharLabel:
If charIndex = Len(cellValue) Then
cellRange.Offset(0, columnIndex).Value = VBA.Strings.Right(cellValue, charIndex - copiedCharsCount)
End If
Next charIndex

nextCell:
Next cellRange
End Sub

关于用于将 "&&","&&-","&-"等符号和数字提取到不同列中的 VBA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com