Excel VBA单元格大小/小写取决于其他单元格

时间:2018-05-18 10:16:08

标签: excel vba excel-vba

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

宏目的: 从第2行开始循环遍历B列中的单元格并将字符串从大写更改为小写,反之亦然,具体取决于N列中单元格的值(如果值= 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

我有点陷入循环部分,并不是一个如何修复当前代码中的错误的线索。

提前致谢:)

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

尝试在数组中处理

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

答案 2 :(得分:1)

您可以尝试这样的事情......

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

答案 3 :(得分:1)

使用Range的每个循环的另一种方法:

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

答案 4 :(得分:0)

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

希望这可以帮助或者至少学习关于循环的新内容:)

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