1

私は VBA に不慣れで、私よりも VBA に慣れていない他の誰かのコードから構築しています。ヒントやアドバイスをお寄せいただきありがとうございます。

画像を投稿できないので、データセットについて説明しようと思います。データはユーザーフォームからのもので、コンテンツの大部分がテーブル範囲 A14:M34 にあり、列 A に質問があり、列 BM にデータがあります。最初の行は、検査対象のユニットを識別するためにユーザーが入力するヘッダーです。以下のデータには、オプションとして空白、はいおよびいいえを含むプルダウンと、数値または文字列を含むいくつかの行が入力されています。

未回答の質問について可変サイズの範囲で各セルをテストし、質問がある場合はユーザーに通知し、送信する前にデータセットを完成させるオプションを提供したいと考えています。


    Sub new_p()
        Static AbortProc As Boolean
        Dim iRow As Long
        Dim LastColumn As Long
        Dim aCol As Long
        Dim ws As Worksheet, WS1 As Worksheet
        Dim InputRange As Range

        Set ws = Worksheets("PreparationData")
        Set WS1 = Worksheets("ColdWeatherPreparation")
        Set InputRange = WS1.Range("B15:M34")

        If AbortProc Then Exit Sub

        'find last column in range
        LastColumn = WS1.Cells(14, 2).End(xlToRight).Column

        'define variable range of columns
        For aCol = 2 To LastColumn
            'check that the circuit row is not blank
            'If Cells(14, aCol) Is Not Nothing Then
                If IsEmpty(InputRange) Then
                Msg = "All fields are not populated. Stop submission to resume editing?"
                Ans = MsgBox(Msg, vbYesNo)
                    'if yes stop process
                    If Ans = vbYes Then
                    AbortProc = True
                    Exit Sub
                    End If
                    'if no run rest of script
                    If Ans = vbNo Then
                    MsgBox "Run without Correcting?"
                    AbortProc = False
                    Exit Sub
                    End If
                End If
            'End If
        Next
'more code here that seems to be working
End Sub

冗長だと思われる行をコメントアウトしたことがわかります。End(xlToRight) がヘッダー行の最後に入力された列を生成する場合、それらは空白ではないため、テストする必要はありません。それにもかかわらず、最終チェックが完了し、完全に役に立たないことが証明されるまで、使用していないコードを保持します。過度のコメントは、VBA 以外のスタッフの大規模なグループが私のコードを実装する前にフォローして検証するのを助けるためです。

したがって、LastColumn の定義は機能しているようで、後でもう一度使用します。コードをステップ実行すると、偽のデータセットの正しい回数が循環します。isEmpty が落ちているような気がします。

4

2 に答える 2

0

B15:M34 のすべてのセルが空白でない場合は、次のようにすることができます。

If Application.CountBlank(InputRange)>0 Then
    If Msgbox(Msg, vbYesNo) = vbYes Then
        'rest of your code
    End If
End If

編集: これにより、各データ セルが対応するヘッダー セルと照合されます。

Sub new_p()
    Static AbortProc As Boolean
    Dim iRow As Long
    Dim LastColumn As Long
    Dim aCol As Long
    Dim ws As Worksheet, WS1 As Worksheet
    Dim InputRange As Range, rw As Range
    Dim HeaderRange As Range
    Dim x As Long, Msg As String

    Set ws = Worksheets("PreparationData")
    Set WS1 = Worksheets("ColdWeatherPreparation")
    Set HeaderRange = WS1.Range("B14:M14")
    Set InputRange = WS1.Range("B15:M34")

    'are you sure about this next line?
    'once validation has failed once how does it re-run?
    If AbortProc Then Exit Sub

    For Each rw In InputRange.Rows

        For x = 1 To rw.Cells.Count
            If Len(rw.Cells(x).Value) = 0 And _
               Len(Headerange.Cells(x).Value) > 0 Then

                Msg = "All fields are not populated. Stop submission" & _
                       " to resume editing?"

                If MsgBox(Msg, vbYesNo) = vbYes Then
                    AbortProc = True
                    Exit Sub
                Else
                    MsgBox "Run without Correcting?"
                    AbortProc = False
                    Exit Sub
                End If
            End If
        Next x

    Next rw

    'more code here that seems to be working
End Sub
于 2012-08-22T16:27:25.783 に答える
0

Len行でエラー?おそらく、Cells には 2 つのパラメーターがあるためでしょうか。Cells(RowIndex,ColumnIndex).

また、次の方法で LastColumn を設定できます。

LastColumn = ActiveSheet.UsedRange.Columns.Count

行に対しても同じことができます:

LastRow = ActiveSheet.UsedRange.Rows.Count

Forループ内に移動する必要があるかもしれませんIf AbortProc Then Exit Sub(最初/最後の行として)

于 2012-08-23T13:52:12.187 に答える