• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

Opgelost Uitsluiten kolom voor controle

Dit topic is als opgelost gemarkeerd

vrouw

Terugkerende gebruiker
Lid geworden
27 mrt 2010
Berichten
1.558
Hoi,

Onderstaande controleert volgens mij of kolom 5 is ingevuld en C3 niet leeg is?.
in deze regel:
Code:
If lastRowMin >= startRow And Not IsEmpty(wsIn.Range("C3").Value) Then

Is de kolom 5 uit te sluiten van die controle?


Code:
Sub GenerateOutput()

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
   
    'Declaring variables
    Dim wsIn As Worksheet
    Dim wsOut As Worksheet
    Dim startRow As Long
    Dim checkRange As Range
       
    Set wsIn = Sheets("Invoer")
    Set wsOut = Sheets("Export")
   
    'Emptying sheet
    ThisWorkbook.Unprotect "BlaBla"
    wsIn.Unprotect Password:="BlaBla"
    wsOut.Visible = True
    For Each Sheet In ActiveWorkbook.Worksheets
         If Sheet.Name = "Export" Then
              wsOut.Cells.Clear
         End If
    Next Sheet
       
    'Defining range of relevant cells
    startRow = 10
    lastRow1 = wsIn.Cells(Rows.Count, 1).End(xlUp).row
    lastRow2 = wsIn.Cells(Rows.Count, 2).End(xlUp).row
    lastRow3 = wsIn.Cells(Rows.Count, 3).End(xlUp).row
    lastRow4 = wsIn.Cells(Rows.Count, 4).End(xlUp).row
    lastRow5 = wsIn.Cells(Rows.Count, 5).End(xlUp).row
    lastRow6 = wsIn.Cells(Rows.Count, 6).End(xlUp).row
    lastRowMin = WorksheetFunction.Min(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
    lastRowMax = WorksheetFunction.Max(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
    Set checkRange = wsIn.Range(wsIn.Cells(startRow, 1), wsIn.Cells(lastRowMax, 6))
       
    'Check for empty cells in range
    i = 0
    For Each checkCell In checkRange
        c = c + 1
        If IsEmpty(checkCell) Then
            i = i + 1
        End If
    Next checkCell
   
    'Validations if all cells are filled
    If Not IsEmpty(wsIn.Range("C3").Value) Then
        If lastRowMin >= startRow And Not IsEmpty(wsIn.Range("C3").Value) Then
            If i = 0 Then
 
Die regel kijkt alleen naar regelnummers, niet naar kolommen.
 
Aangezien in deze regels
Code:
lastRowMin = WorksheetFunction.Min(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
lastRowMax = WorksheetFunction.Max(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
kolom5 niet wordt meegenomen in de berekening lijkt het mij duidelijk dat kolom 5 uitgesloten is.
 
Nu heb ik de volledige code geplakt. Er zat een fout in de code van het eerste bericht, mijn excuses daarvoor.

@Warme bakkertje je bedoeld dat als ik "lastRow5" uit de regels 33 & 34 haal dat ie niet meer kijkt of er in die kolom wat is ingevuld?

Code:
Sub GenerateOutput()

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
  
    'Declaring variables
    Dim wsIn As Worksheet
    Dim wsOut As Worksheet
    Dim startRow As Long
    Dim checkRange As Range
      
    Set wsIn = Sheets("Invoer")
    Set wsOut = Sheets("Export")
  
    'Emptying sheet
    ThisWorkbook.Unprotect "BlaBla"
    wsIn.Unprotect Password:="BlaBla"
    wsOut.Visible = True
    For Each Sheet In ActiveWorkbook.Worksheets
         If Sheet.Name = "Export" Then
              wsOut.Cells.Clear
         End If
    Next Sheet
      
    'Defining range of relevant cells
    startRow = 10
    lastRow1 = wsIn.Cells(Rows.Count, 1).End(xlUp).row
    lastRow2 = wsIn.Cells(Rows.Count, 2).End(xlUp).row
    lastRow3 = wsIn.Cells(Rows.Count, 3).End(xlUp).row
    lastRow4 = wsIn.Cells(Rows.Count, 4).End(xlUp).row
    lastRow5 = wsIn.Cells(Rows.Count, 5).End(xlUp).row
    lastRow6 = wsIn.Cells(Rows.Count, 6).End(xlUp).row
    lastRowMin = WorksheetFunction.Min(lastRow1, lastRow2, lastRow3, lastRow4, lastRow5, lastRow6)
    lastRowMax = WorksheetFunction.Max(lastRow1, lastRow2, lastRow3, lastRow4, lastRow5, lastRow6)
    Set checkRange = wsIn.Range(wsIn.Cells(startRow, 1), wsIn.Cells(lastRowMax, 6))
      
    'Check for empty cells in range
    i = 0
    For Each checkCell In checkRange
        c = c + 1
        If IsEmpty(checkCell) Then
            i = i + 1
        End If
    Next checkCell
  
    'Validations if all cells are filled
    If Not IsEmpty(wsIn.Range("C3").Value) Then
        If lastRowMin >= startRow And Not IsEmpty(wsIn.Range("C3").Value) Then
            If i = 0 Then
                          
                'Check file name from inputbox
                If strReturn Like "*[!,.)'( _0-9A-Za-z(?)-]*" Then
                    MsgBox "Bestandnaam bevat tekens die niet toegestaan zijn"
                Else
                              
                    wsIn.Range(Cells(startRow, 4), Cells(lastRowMin, 4)).NumberFormat = "dd-mm-yyyy"
                    wsIn.Range(Cells(startRow, 5), Cells(lastRowMin, 5)).NumberFormat = "dd-mm-yyyy"
                    wsIn.Range(wsIn.Cells(startRow, 1), wsIn.Cells(lastRowMax, 6)).HorizontalAlignment = xlLeft
                              
                    'Copy data from input to output worksheet
                    wsIn.Range("A10:F" & lastRowMin).Copy _
                        Destination:=Worksheets("Export").Range("A2:F" & lastRowMin - startRow + 1)
                    wsOut.Range("F1").EntireColumn.Insert
                  
                    'Copy reason code in first row
                    wsIn.Range("A3").Copy
                    wsOut.Range("F2").PasteSpecial Paste:=xlPasteValues
                    Application.CutCopyMode = False
                  
                    'Fill reason code in all rows
                    Dim lastRowEx As Long
                    lastRowEx = wsOut.Cells(Rows.Count, 1).End(xlUp).row
                    If lastRowEx > 2 Then
                        wsOut.Range("F2:F" & lastRowEx).FillDown
                    End If
                  
                    'Format numbers and date
                    wsOut.Range("A2", "A" & lastRowEx).NumberFormat = "General"
                    wsOut.Range("B2", "B" & lastRowEx).NumberFormat = "General"
                    wsOut.Range("C2", "C" & lastRowEx).NumberFormat = "General"
                    wsOut.Range("D2", "D" & lastRowEx).NumberFormat = "dd-mm-yyyy"
                    wsOut.Range("E2", "E" & lastRowEx).NumberFormat = "dd-mm-yyyy"
                    wsOut.Range("F2", "F" & lastRowEx).NumberFormat = "General"
                  
                    'Fill column titles in output worksheet
                    wsOut.Range("A1").Value = "1st_NUMMER"
                    wsOut.Range("B1").Value = "2e_NUMMER"
                    wsOut.Range("C1").Value = "AANTAL"
                    wsOut.Range("D1").Value = "1stDATUM"
                    wsOut.Range("E1").Value = "2eDATUM"
                    wsOut.Range("F1").Value = "1stCODE"
                    wsOut.Range("G1").Value = "relevant"
                            
                    'Save sheet to CSV
                    omschrijving = wsIn.Cells(startRow, 6).Value
                    saveSheetToCSV omschrijving
                  
                    MsgBox ("file is saved")
                  
                End If
            Else
                MsgBox ("Not all cells are filled")
            End If
          
        'Else
         '   MsgBox ("fill the data in")
        End If
    Else
        MsgBox ("Choose a code in C3")
    End If

      
    Sheets("Export").Cells.Clear
    Worksheets("Export").Visible = False
    wsIn.Protect Password:="BlaBla", UserInterfaceOnly:=True
    ThisWorkbook.Protect "BlaBla"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
  
End Sub

Toch als ik "lastRow5" eruit haal krijg ik deze poup
1770992320942.png
Bericht automatisch samengevoegd:

Update:

het lijkt in deze regel te zittuen, als ik 6 aanpas naar 4 dan loopt de code wel en mag inderdaad kolom 5 leeg zijn.

Code:
Set checkRange = wsIn.Range(wsIn.Cells(startRow, 1), wsIn.Cells(lastRowMax, 6))

maar dan word er dus ook niet meer naar kolom 6 gekeken.
Hoe is dat te onder vangen dat ie kijkt naar kolom 1-4 & kolom 6?
 
Laatst bewerkt:
Wat met deze dan ?
Code:
    lastRowMin = Application.Min(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
    lastRowMax = Application.Max(lastRow1, lastRow2, lastRow3, lastRow4, lastRow6)
    Set checkRange = Union(wsIn.Range(wsIn.Cells(startRow, 1), wsIn.Cells(lastRowMax, 4)), _
                            wsIn.Range(wsIn.Cells(startRow, 6), wsIn.Cells(lastRowMax, 6)))
 
Terug
Bovenaan Onderaan