【问题标题】:Excel VBA Macro to Loop Through Cells (Including Blanks) to Last Cell with Data and ReplaceExcel VBA宏循环遍历单元格(包括空白)到最后一个单元格的数据并替换
【发布时间】:2021-10-31 09:58:38
【问题描述】:

我是 VBA 新手,需要帮助编写代码...我的目标是遍历每一行以转到该行中的最后一个非空白单元格(每行中有多个空白)并替换“ ;"和 ”}”。然后继续循环到数据集的底部,直到A行没有数据为止。

下面是“之前”的例子:

      1          2          3          4          5
 1   {1,$1.00;              3,$2.00;
 2   {1,$1.00;   2,$3.00;              4,$1.00;
 3   {1,$1.00;              3,$2.50;               5,$1.00;

下面是“之后”的例子:

      1          2          3          4          5
 1   {1,$1.00;              3,$2.00}
 2   {1,$1.00;   2,$3.00;              4,$1.00}
 3   {1,$1.00;              3,$2.50;               5,$1.00}

如果有帮助,代码的模式是:

Regex: .[0-9]{1,2},[$][0-9]{1,3}[.][0-9][0-9].|[0-9]{1,2},[$][0-9]{1,3}[.][0-9][0-9]

奖金,如果你也可以合并替换“1;”用“{1;”对于 A 列中的每个单元格(那太好了 - 我目前只是手动将 A 列上的“1;”替换为“{1;”。

让您了解数据量:大约 250 列,超过 25,000 行。

这是我现有的代码,不知道如何循环下一行(也很想知道我是否可以在不显式调用最后一行的情况下执行此操作以防数据更改)。

Dim LastCell As String
Dim rng As Range
Dim i As Long

For i = 1 To 223127
' Use all cells on the sheet
'Set rng = Sheets("Paste").Cells

'Or use a range on the sheet
Set rng = Sheets("Parse").Range("29:29")

' Find the last cell
LastCell = Last(3, rng)

' Select from A1 till the last cell in Rng
With rng.Parent
    .Select
    .Range(LastCell).Select
   Selection.Replace What:=";", Replacement:="}", Lookat:=xlPart, _
   SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
   ReplaceFormat:=False

感谢您的帮助,如果我可以提供更多信息,请告诉我。

【问题讨论】:

  • 这里的最终目标是什么——您只是在描述更广泛任务的一部分吗?大概您发布的数据是“之后”,而不是“之前”?
  • @BigBen,非常感谢您的反馈(感谢您的编辑 - 我是 SO 的新用户,昨天刚注册)。我曾尝试编写自己的代码,但目前卡在可以替换一行的位置 - 不知道如何转到下一行并循环公式。
  • @TimWilliams 嗨,蒂姆,感谢您昨天的反馈和帮助 - 我在问题中添加了之前/之后的场景,并输入了我到目前为止的代码...抱歉回答您的问题问题,最终目标是 1. 找到行中数据的最后一列。 2.替换“;”和 ”}”。 3. 转到下一行并重复

标签: excel vba loops foreach


【解决方案1】:

替换字符串

Option Explicit

Sub ReplaceStrings()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets("Parse")
    
    Dim rg As Range: Set rg = ws.UsedRange
    Dim rCount As Long: rCount = rg.Rows.Count
    Dim cCount As Long: cCount = rg.Columns.Count
    
    Dim Data As Variant
    If rCount + cCount = 2 Then
        ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
    Else
        Data = rg.Value
    End If
        
    Dim r As Long
    Dim c As Long
    Dim cLen As Long
    Dim cString As String
    
    For r = 1 To rCount
        cString = CStr(Data(r, 1))
        cLen = Len(cString)
        If cLen > 0 Then
            If Left(cString, 1) = "1" Then
                Data(r, 1) = "{1" & Right(cString, cLen - 1)
            End If
        End If
        For c = cCount To 1 Step -1
            cString = CStr(Data(r, c))
            cLen = Len(cString)
            If cLen > 0 Then
                If Right(cString, 1) = ";" Then
                    Data(r, c) = Left(cString, cLen - 1) & "}"
                    Exit For
                End If
            End If
        Next c
    Next r
       
    rg.Value = Data
 
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多