简体   繁体   English

Excel VBA单元格格式,如果是Statemets

[英]Excel VBA Cell Formatting, If Statemets

I have a sheet that is used for sales entry, that has 15 different columns that get formatted based on what is entered in the cell. 我有一张用于销售条目的工作表,它有15个不同的列,根据在单元格中输入的内容进行格式化。 It's simple formatting, converting to proper case, things like that. 这是简单的格式化,转换为适当的情况,这样的事情。

The shortened version of the code is: 缩短版的代码是:

Private Sub Worksheet_Change(ByVal target As Range)
On Error GoTo Cleanup
Application.EnableEvents = False: Application.ScreenUpdating = False: 
Application.Calculation = xlCalculationManual ' etc..

Dim rName As String
If Not (Application.Intersect(target, Range("C2:C" & Me.Cells(Me.Rows.Count,"C").End(xlDown).Row)) Is Nothing) Then
    rName = target.Value2
    target.Value2 = UCase(Trim(rName))
End If

14x more above the above (1 each column)

Cleanup:
Application.EnableEvents = True: Application.ScreenUpdating = True: 
Application.Calculation = xlCalculationAutomatic ' etc..

The reason I have it set to manual, then automatic, is because if I don't, Excel crawls to a halt. 我把它设置为手动,然后自动的原因是因为如果我不这样做,Excel会停止运行。 I'm assuming because when a user enters data, it changes values for hidden columns, and triggers the Change event again. 我假设因为当用户输入数据时,它会更改隐藏列的值,并再次触发Change事件。 The way it works now, is fine, however there is just a second or two delay after each cell is checked and formatted after a user enters the data, so ultimately I'm wondering if there is a quicker way to do it. 它现在的工作方式很好,但是在用户输入数据后检查和格式化每个单元格后只有一两秒的延迟,所以最后我想知道是否有更快的方法来完成它。

Thanks! 谢谢!

One obvious issues: 一个明显的问题:

  • Me.Cells(Me.Rows.Count,"C").End( xlDown ).Row 'returns row 1,048,576 Me.Cells(Me.Rows.Count,"C").End( xlDown ).Row 'returns row 1,048,576

should be 应该

  • Me.Cells(Me.Rows.Count,"C").End( xlUp ).Row Me.Cells(Me.Rows.Count,"C").End( xlUp ).Row

Try this: 尝试这个:


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge = 1 Then

        If Not (Application.Intersect(Target, Me.UsedRange.Columns("C")) Is Nothing) Then

            Application.EnableEvents = False
            Application.Calculation = xlCalculationManual ' etc..
                On Error Resume Next
                Target.Value2 = UCase$(Trim$(Target.Value2))
                On Error GoTo 0
            Application.EnableEvents = True
            Application.Calculation = xlCalculationAutomatic ' etc..

        End If
    End If
End Sub

Notes: 笔记:

Try your intersect as, 尝试你的相交,

If Not Application.Intersect(target, target.parent.usedrange) Is Nothing Then

The worksheet's .UsedRange property is decided beforehand. 工作表的.UsedRange属性是事先确定的。 If you made an entry outside the usedrange, the usedrange would instantly expand to encompass it. 如果您在usedrange之外创建了一个条目,则usedrange会立即扩展以包含它。 This known as 'overhead' and it's one of the reasons that vba is slower than C or hex. 这称为“开销”,这是vba比C或hex慢的原因之一。

After you've determined that one or more cells in target is involved with something you want to do, parse each cell in target to determine how it should be processed. 在确定目标中的一个或多个单元格涉及您要执行的操作后,解析目标中的每个单元格以确定应如何处理它。

you may try this: 你可以试试这个:

Private Sub Worksheet_Change(ByVal target As Range)
    If Intersect(target, Columns("C:Q")) Is Nothing Then Exit Sub ' exit if changed cells are completely outside relevant columns (change "C:Q" to your actual relevant columns indexes)

    Application.EnableEvents = False: Application.ScreenUpdating = False:
    Application.Calculation = xlCalculationManual ' etc..
    On Error GoTo Cleanup

    With Intersect(target, Intersect(UsedRange, Columns("C:Q"))) 'consider only changed cells in relevant columns (change "C:Q" to your actual relevant columns indexes)
        .Value2 = UCase(Trim(.Value2))
    End With

Cleanup:
    Application.EnableEvents = True: Application.ScreenUpdating = True:
    Application.Calculation = xlCalculationAutomatic ' etc..
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM