如果整列中有值(减去标题),请提醒用户错误的文件

时间:2017-07-20 20:01:13

标签: excel vba excel-vba

我有一个宏拿两个电子表格并进行查找。我需要能够从组合框(ComboBox1.value)中获取用户选择的值,并指定列引用,然后在继续之前检查列是否为空。这是为了防止用户在电子表格中选择月份列,并提供已更新的月份的更新。然后我会提醒他们,他们可以选择如何继续。

组合框包含所有12个月的清单。我需要设置一个值作为其选择的列引用,如果该列为空(从列中的A2到最后一个条目,或者从第2行到第2行开始的任何单元格中有任何值...无论哪个最有意义的,提醒他们。

此表格有超过20K行。我不能只选择一个参考,因为它可能有空白,这就是为什么我要检查整列中的值。

我为Pete的爱,在寻找interwebz之后,在我所有的新手荣耀中,都无法实现这一目标......

下面的代码没有给出错误,但它也无法正常工作。我有一张测试表,其中包含四月份的值,并且它没有提醒,它会进入下一步行动。

我尝试执行此操作的代码块看起来像......

Dim UpdateMonthColumn As String

If ABCMatrixMonthSelect.ComboBox1.value = "January" Then
UpdateMonthColumn = "U"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "February" Then
UpdateMonthColumn = "W"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "March" Then
UpdateMonthColumn = "Y"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "April" Then
UpdateMonthColumn = "C"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "May" Then
UpdateMonthColumn = "E"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "June" Then
UpdateMonthColumn = "G"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "July" Then
UpdateMonthColumn = "I"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "August" Then
UpdateMonthColumn = "K"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "September" Then
UpdateMonthColumn = "M"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "October" Then
UpdateMonthColumn = "O"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "November" Then
UpdateMonthColumn = "Q"
ElseIf ABCMatrixMonthSelect.ComboBox1.value = "December" Then
UpdateMonthColumn = "S"
End If

If WorksheetFunction.CountA(Columns(UpdateMonthColumn)) = 0 Then

'If WorksheetFunction.CountA(Range("Sheet 1[UpdateMonthColumn]")) <> 0 Then
Msgbox12 = Msgbox("The column you will place the ABC Count within for " &   
ABCMatrixMonthSelect.ComboBox1 & "is not empty." & vbCr & " Do you still 
wish to proceed with an update for " & ABCMatrixMonthSelect.ComboBox1 & "?" 
& vbCr & "Select Yes to proceed and update with new values." & vbCr 
& "Select No to return to drop down list and chose a different month." & 
vbCr & "Select Cancel to return to ABC Matrix Main Menu.", vbQuestion Or 
vbYesNoCancel, "ABC Matrix Macro")
If Msgbox12 = vbYes Then GoTo SelectFile2:
If Msgbox12 = vbNo Then ActiveWorkbook.Close
    GoTo SelectMonth:
If Msgbox12 = vbCancel Then
    Msgbox "ABC Count update cancelled by user!" & vbCr & vbCr & "Returning 
    to ABC Matrix Macro Main Menu.", vbCritical, "ABC Matrix Macro Alert!"
     With ABCMatrixMainMenu
     .StartUpPosition = 0
     .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
     .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
     .Show
    Exit Sub
  End With
 End If
Else

1 个答案:

答案 0 :(得分:1)

Dim rng As Range, sht As WorkSheet

Set sht = thisworkbook.sheets("Sheet1")

'select case is cleaner than a long If Then block
Select Case ABCMatrixMonthSelect.ComboBox1.value
    Case "January": UpdateMonthColumn = "U"
    Case "February": UpdateMonthColumn = "W"
    Case "March": UpdateMonthColumn = "Y"
    '...etc
End Select


Set rng = sht.Range(sht.Cells(2,UpdateMonthColumn), _
                    sht.Cells(sht.rows.count, UpdateMonthColumn))
If WorksheetFunction.CountA(rng) = 0 Then
    'OK
End If