vba - 如果不喜欢 vba

标签 vba excel

关闭。这个问题需要debugging details .它目前不接受答案。












编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将帮助其他人回答问题。


7年前关闭。







Improve this question




如果特定单元格格式不是每小时格式,我想应用条件(删除行)

我尝试了下面的代码,但它不起作用

Sub delete_row()

Dim i As Integer
Dim LR As Long
 LR = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LR
If Not Cells(i, 1).Value Like "##:##-##:##" Then
Cells(n, 1).EntireRow.Delete


End If
Next i
End Sub

最佳答案

我不相信有一种开箱即用的方式或单一模式匹配来评估您输入的时间范围。

这应该可以工作,并且足够灵活以适应一些输入变化(破折号之前或之后的空格,单位或两位数小时输入(没有前导 0)):

' Check for cell values matching the following Time pattern entry:
' [Hour]:[Minute]-[Hour]:[Minute]    

Dim i As Integer
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row

Dim valueParts As Variant
Dim result As Boolean
Dim part As Integer

' Because you are deleting rows as you go,
' move from bottom to top.
' Otherwise rows will be skipped when you delete.
For i = LR To 1 Step -1
    ' Split into parts so we can check each.
    valueParts = Split(Cells(i, 1).Value, "-")

    ' Evalutate each component to make sure
    ' it fits the time format we expect.
    ' Default to True until a non-match is found.
    result = True

    For part = LBound(valueParts) To UBound(valueParts)
        ' Account for single digit (0-9)
        ' and double digit (10-12) hours.
        result = result And _
            (Trim(valueParts(part)) Like "#:##" _
            Or Trim(valueParts(part)) Like "##:##")
    Next

    If Not result Then
        ' This is not a valid time pattern.
        ' Delete the row.
        Rows(i).Delete
    End If

Next

关于vba - 如果不喜欢 vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470498/

相关文章:

excel - 如何在vba中的字符串中加上双引号?

excel - 如何使用 VBA 打开 Powerpoint

Excel:按日期计算值

c# - excel导出异常

excel - 将循环放入 Select Case 会产生错误

vba - 如何使用 VBA 遍历 MS Word 中的每个字母?

excel - 通过代码添加模块

excel - 将列排序为八个单元格的行

Excel:重新格式化/替换单元格中的部分字符串

events - 如何在 VBA for Excel 中为动态选择的单元格定义 ENTER 按键事件