簡體   English   中英

Excel VBA單元格大寫/小寫,取決於其他單元格

[英]Excel VBA cell upper/lower case depending other cell

我正在編寫代碼以遍歷excel工作表並將文本(在B列中)更改為大寫/小寫,具體取決於同一行N列中的單元格的值。

宏的用途:循環遍歷B列中從第2行開始的字符串,並將字符串從大寫改為小寫,反之亦然,這取決於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

我有點停留在循環部分,而不是真正的技巧來解決當前代碼中的錯誤。

提前致謝 :)

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. 像這樣: 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

嘗試在數組中進行處理,

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

您可以嘗試這樣的事情...

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

在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

我知道您在問題中說的是從第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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM