我想确定我范围内的某个单元格是否包含日期(任何日期),以及是否确实要通过消息退出子单元。 日期格式如下:dd-mmm-yy,但由单元格内的公式生成。
这是我已经编写的一些代码以及要实现的伪代码。
Sub RemoveRowButton()
'This Macro deletes a row where the button is clicked.
'Variables
Dim row As Long
Dim varResponse As Variant
Application.ScreenUpdating = False
'Message box confirming user is doing the right thing
varResponse = MsgBox("Delete this row? 'Yes' or 'No'", vbYesNo, "Delete Row")
If varResponse <> vbYes Then Exit Sub
'Carry on with deleting row.....
Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell.EntireRow
*******Pseudo Code *******
'Check if the row to be deleted has a date in the D Column of the range (which is a Row)
'If IsDate **in D column of the Range is ture*** Then
'MsgBox "This Row Contains a Date!"
'End If
'Unprotect sheet
ActiveSheet.Unprotect Password:="***"
'Delete row on button row
rng.Delete
'Protect sheet again
ActiveSheet.Protect Password:="***"
End Sub
如果您也能解释您的代码/答案,谢谢,谢谢。
编辑:
感谢您提供的所有帮助,通过反复试验,创建了一个对我有用的东西。
Set rng2 = rng.Cells(, 4)
If IsDate(rng2.Value) Then 'Check Cell for Date
MsgBox "Warning: This Row Cannot be deleted!"
Exit Sub
End If
由于我不熟悉VBA,因此我不知道这在最佳实践中是否还可以。如果没有,并且您想进行更正,请这样做。
答案 0 :(得分:2)
这是解决您的问题的主意。将代码安装在您希望对其执行操作的工作表的代码表中(不在“ Module1”之类的标准模块中!)请注意,该代码会对双击D列从第2行到第二行做出反应。 列A 中的最后使用的行。您可以调整。遵循代码本身中的指示。我使用这种方法来代替您似乎在工作表的每一行中都有的按钮-出于优先考虑,但是在这里用于演示和避免创建按钮。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' 03 Jan 2019
Dim Rng As Range
Dim R As Long
Dim Cell As Range
Dim i As Integer
R = Cells(Rows.Count, "A").End(xlUp).Row ' last used row in column A
Set Rng = Range(Cells(2, 4), Cells(R, 4)) ' used range in column D
If Not Application.Intersect(Target, Rng) Is Nothing Then
' if a cell in Rng as double-clicked:-
R = Target.Row
Set Rng = Range(Cells(R, "A"), Cells(R, "S"))
For Each Cell In Rng
With Cell
If IsDate(.Value) Then
For i = 3 To 1 Step -1
' check if the Numberformat contains all of "m", "d" and "y"
If InStr(1, .NumberFormat, Mid("dmy", i, 1), vbTextCompare) = 0 Then Exit For
Next i
If i = 0 Then ' all 3 were found
If MsgBox("Do you want to delete row " & R & " ?", _
vbQuestion Or vbYesNo, _
"Click ""No"" to keep the row") = vbYes Then
Rows(R).Delete
End If
Exit For
End If
End If
End With
Next Cell
Cancel = True ' ends in-cell editing
End If
End Sub
代码对每个单元格(A:S)进行两次检查。它首先检查其值是否为日期。然后,假设它是一个数字,它将检查单元格格式。如果NumberFormat包含所有字母“ m”,“ d”和“ y”,则将其确认为日期并发布以供删除,然后用户可以确认其意图。 此方法可能需要一些微调。首先,如果该单元格具有文本日期,则必须执行另一次第二次检查。其次,如果日期格式仅由3个标准中的2个组成,则必须相应地减少其在掩码中的存在的测试。一旦更好地理解了数据的性质,就可以实施这些修改之一,或者两者都可以实现。
答案 1 :(得分:1)
@ J4C3N-14您是否尝试过:
<table style="border-collapse: collapse; width: 75%; height: 216px;" border="1">
<tbody>
<tr style="height: 36px;">
<td style="width: 30%; height: 36px;">A</td>
<td style="width: 70%; height: 36px;" colspan="4">B</td>
</tr>
<tr style="height: 36px;">
<td style="width: 30%; height: 72px;" rowspan="2">C</td>
<td style="width: 15%; height: 36px;">1</td>
<td style="width: 10%; height: 36px;">D</td>
<td style="width: 20%; height: 144px;" rowspan="4">Image</td>
<td style="width: 25%; height: 144px;" rowspan="4">List</td>
</tr>
<tr style="height: 36px;">
<td style="width: 15%; height: 36px;">2</td>
<td style="width: 10%; height: 36px;">F</td>
</tr>
<tr style="height: 36px;">
<td style="width: 30%; height: 72px;" rowspan="2">Image</td>
<td style="width: 15%; height: 36px;">3</td>
<td style="width: 10%; height: 36px;">H</td>
</tr>
<tr style="height: 36px;">
<td style="width: 15%; height: 36px;">4</td>
<td style="width: 10%; height: 36px;">L</td>
</tr>
<tr style="height: 36px;">
<td style="width: 100%; height: 36px;" colspan="5">Link</td>
</tr>
</tbody>
</table>