【问题标题】:Excel VBA cell upper/lower case depending other cellExcel VBA单元格大小写取决于其他单元格
【发布时间】:2018-05-18 10:16:08
【问题描述】:

我正在编写代码以循环遍历 Excel 工作表并将文本(B 列中的)更改为大写/小写,具体取决于同一行 N 列中单元格的值。

宏目的: 从第 2 行开始循环遍历 B 列中的单元格,并将字符串从大写更改为小写,反之亦然,具体取决于 N 列中单元格的值(如果 value = 5,则小写,其他情况下文本应为大写)

到目前为止我得到的代码:

Sub CAPS()
'
' CAPS Macro
'
Dim Rang As Integer
Dim j As Integer


j = 2
    For Each N In Source.Range("N2:N10000")   ' Do 10000 rows
        Rang = Cells(j, 14)
        If Rang = 5 Then
           Cells(j, 2).Range("A1").Select
            ActiveCell.Value = LCase$(ActiveCell.Text)
            Else
            ActiveCell.Value = UCase$(ActiveCell.Text)
           j = j + 1
        End If
    Next N


End Sub

我有点卡在循环部分,不知道如何修复当前代码中的错误。

提前致谢:)

【问题讨论】:

  • 当前代码有哪些错误?一件显而易见的事情是您使用Nj,它们始终具有相同的值,因此我建议您将所有j 替换为N
  • 还将您的FOR EACH... 重写为FOR N is 2 to 10000

标签: vba excel


【解决方案1】:
Sub CAPS()
'
' CAPS Macro
'
Dim N as long  'use long here as integer is limite to a 32b character

For N Is 2 to 10000   ' Do 10000 rows
   If Cells(N, 14) = 5 Then
       Cells(N, 2) = LCase(Cells(N,2)
   Else
       Cells(N, 2) = UCase(Cells(N,2)
   EndIf     
Next N

End Sub

这应该可以解决问题,但未经测试。

您目前要测试的行数是固定的。要优化您的代码,您可以首先检查有多少行填充了数据。为此,您可以使用:

DIM lastrow as long lastrow = Cells(Rows.Count, "B").End(xlUp).Row

然后使用For N Is 2 to lastrow 进行循环

另外,明确引用您的工作表也是一种很好的做法,因为这样可以防止出现不希望的结果。例如,您在代码运行时单击另一个工作表,它将继续在该工作表上进行格式化。为此,请声明一个变量作为您的工作表:

DIM ws as worksheet

并为您的变量设置一个值,在本例中为 Sheet1。

Set ws as ThisWorkbook.Worksheets("Sheet1")

现在每次您引用 Cells() 时,您都可以通过在其前面添加 ws. 来明确说明必须是哪张表,如下所示:ws.Cells()

将所有这些总结到您的代码中:

Sub CAPS()
'
' CAPS Macro
'

Dim N as long  'use long here as integer is limite to a 32b character
Dim lastrow as long
Dim ws as worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1") 'Set the code to run on Sheet 1 of your current workbook.
lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row

For N Is 2 to lastrow   ' Do all rows that have data in column B
   If ws.Cells(N, 14) = 5 Then
       ws.Cells(N, 2) = LCase(ws.Cells(N,2)
   Else
       ws.Cells(N, 2) = UCase(ws.Cells(N,2)
   EndIf     
Next N

End Sub

【讨论】:

    【解决方案2】:

    尝试在数组中处理,

    Sub CAPS()
    '
    ' CAPS Macro
    '
        Dim arr As variant, j As Integer
    
        with worksheets("sheet1")
            arr = .range(.cells(2, "B"), .cells(.rows.count, "B").end(xlup).offset(0, 12)).value2
            for j= lbound(arr, 1) to ubound(arr, 1)
                if arr(j, 13) = 5 then
                    arr(j, 1) = lcase(arr(j, 1))
                else
                    arr(j, 1) = ucase(arr(j, 1))
                end if
            next j
            redim preserve arr(lbound(arr, 1) to ubound(arr, 1), 1 to 1)
            .cells(2, "B").resize(ubound(arr, 1), ubound(arr, 2)) = arr
        end with
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      你可以试试这样的……

      Sub CAPS()
      Dim ws As Worksheet
      Dim lr As Long, i As Long
      
      Application.ScreenUpdating = False
      
      Set ws = Sheets("Sheet1")   'Sheet where you have to change the letter case
      lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
      
      For i = 2 To lr
          Select Case ws.Cells(i, "N")
              Case 5
                  ws.Cells(i, "B") = LCase(ws.Cells(i, "B"))
              Case Else
                  ws.Cells(i, "B") = UCase(ws.Cells(i, "B"))
          End Select
      Next i
      Application.ScreenUpdating = True
      End Sub
      

      【讨论】:

        【解决方案4】:

        使用范围为每个循环的另一种方法:

        Sub UCaseLCase()
        
          Dim rng, cell As Range
        
          Dim Test As Integer
          Test = 5
          Set rng = Range(Cells(2, 14), Cells(10000, 14))
        
          For Each cell In rng.Cells
            If cell.Value = Test Then
                cell.Offset(0, -12) = LCase(cell.Offset(0, -12))
            Else
                cell.Offset(0, -12) = UCase(cell.Offset(0, -12))
            End If
          Next cell
        
        End Sub
        

        【讨论】:

          【解决方案5】:

          我知道您在问题中说过从第 2 行开始,但从最后一行到第 2 行更容易。

          希望这能有所帮助,或者至少,学习一些关于 Loops 的新知识:)

          Sub CAPS()
          Dim j As Integer
          
          For j = Range("B2").End(xlDown).Row To 2 Step -1
              If Range("N" & j).Value = 5 Then
                  'uppercase
                  Range("B" & j).Value = UCase(Range("B" & j).Value)
              Else
                  'lowercase
                  Range("B" & j).Value = LCase(Range("B" & j).Value)
              End If
          Next j
          
          End Sub
          

          【讨论】:

          • 为什么在这种情况下从下到上更容易?在删除行的情况下更容易,以防止您错过行,但在这种情况下没有真正需要反转步骤。
          • 是的,你是对的。我想我已经习惯了这种循环并且输入更少的代码行。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-25
          • 1970-01-01
          相关资源
          最近更新 更多