填写' n' Excel中的单元格通过读取n的值

时间:2015-05-11 07:18:50

标签: excel vba excel-vba

我想读取任何no的值。 (4或5或......)来自其中一个单元格(比如B2)并填写那些没有。特定位置的细胞(比如b11,b12,b13,b14)...... 如果我没有填写那么它应该给msg"请填写字段"。 这些字段也应标有不同的颜色,以表明它们是强制性的。

Private Sub CommandButton1_Click()
Dim msg As String
Dim val As Integer
Dim i As Integer

val = Cells(2, "B").Value

 For i = 11 To 11 + val

  If IsEmpty(Cells(2, i).Value) Then
     msg = "please enter name"
    End If

  Next i
End Sub

以上代码无效。

纠正我。 谢谢。

下一个问题......

每次我必须点击此宏才能获得此类消息... 而不是单击宏..是否可以在我保存文件时收到错误消息,或者如果字段未填充则关闭文件?

2 个答案:

答案 0 :(得分:1)

您正在创建msg字符串,但您并未将其生成给用户。你也没有为细胞着色。

If (IsEmpty(Cells(2, i).Value)) Then
    MsgBox("Please fill in " & i & " fields.")
End if

着色:

Range("B" & i).Interior.ColorIndex = 3

更新

如果您想避免必须单击按钮来检查单元格,您可以使用Worksheet_Change并仅查看您感兴趣的值。每次更改其中一个时,这将显示MsgBox值如果其中任何一个为空:

Private Function RangeContains(range1 As Range, range2 As Range)
    Dim intersects
    Set intersects = Application.intersect(range1, range2)
    RangeContains = Not intersects Is Nothing
End Function

Private Function GetColumnLetter(number As Integer)
    GetColumnLetter = Split(Cells(1, number).Address(True, False), "$")(0)
End Function

Private Sub Worksheet_Change(ByVal Target As Range)
    ' This is the "11" in your question - adjust to suit
    Const offset As Integer = 11

    ' This is the address of the first cell you want to check - adjust to suit
    Dim firstCell As String
    firstCell = GetColumnLetter(offset) & "2"

    ' This is the number of cells you want to check (the number in your B2 cell)
    Dim numCells As Integer
    numCells = Int(Cells(2, "B").Value)

    ' This is the last cell you want to check
    Dim lastCell As String
    lastCell = GetColumnLetter(offset + numCells) & "2"

    ' This is the range that contains all the cells you want to check
    Dim myRange As Range
    Set myRange = Range(firstCell, lastCell)

    ' This is a boolean flag used to determine if we need to display the MsgBox or not
    Dim valid As Boolean
    valid = True

    ' This is our iterator for looping through the range
    Dim cell

    ' Reset the background color of our range
    myRange.Interior.Color = vbWhite

    ' If the cell that was changed (Target) is inside our range, we need to do the check
    If (RangeContains(myRange, Target)) Then
        ' Check for empty values
        For Each cell In myRange
            If (IsEmpty(cell.Value)) Then
                ' This cell is inside our range but is empty.  Change its background
                ' Color to Red and prompt to show a message
                cell.Interior.ColorIndex = 3
                valid = False
            End If
        Next cell
    End If

    If Not valid Then
        MsgBox "Please fill in red fields"
    End If
End Sub

答案 1 :(得分:0)

试试这个

Private Sub CommandButton1_Click()
Dim i%, cl As Range
i = 0
For Each cl In Range(Cells(2, 11), Cells(2, 11 + Cells(2, "B").Value))
    If cl.Value = "" Then
        cl.Interior.Color = vbRed
        i = i + 1
    End If
Next cl

If i > 1 Then
    MsgBox "Mondatory fields (" & i & ") not filled yet!"
ElseIf i = 1 Then
    MsgBox "One mandatory field has not been filled yet!"
End If
End sub