Stop macro if in Column “C” there are no highlighted yellow cells, if there at least one run the rest of the code

weinlich

New Member
Joined
Jun 25, 2022
Messages
8
Office Version
  1. 2021
Platform
  1. Windows
Hey, I am looking for a solution to this problem. I just started to learn VBA a month ago and this is my first project at work.
I wroted 12 macros in one macro, and After the 4-th macro i need a code which will check the Column “C” if there is a highlighted yellow cell or not. If there is, then continue and if there is not - don’t run the rest.

Or it can check specific text for example if there is only “b_vu” in column C stop the rest of the code and if there is at least one “p_vu” continue

This is the only step I couldn’t solve alone.
Thank You
 
Report with "car" and "truck" as well
Creation Date;Apliccation ID;Vehicle type;Owner id;Monthly payment amount;Owner name;Payment Info;Registration ID;
6/25/2022;14524687564;car;11223355;45;Adam;XXXXXXXXXXX;6546546;
6/25/2022;65456121965;car;85697412;65;Albert;XXXXXXXXXXX;6878623;
6/25/2022;65413486498;car;85697123;120;Sara;XXXXXXXXXXX;9879312;
6/25/2022;54789244877;car;65895147;36;Steve;XXXXXXXXXXX;9852317;
6/25/2022;42658652185;car;89657123;45;Laura;XXXXXXXXXXX;6134942;
6/25/2022;48798762374;car;89557874;89;John;XXXXXXXXXXX;9843213;
6/25/2022;78951765441;truck;85236971;319;Samuel;XXXXXXXXXXX;3121685;
6/25/2022;97762185314;car;98932475;90;Ester;XXXXXXXXXXX;6876513;
6/25/2022;31684621677;car;61324878;80;Petra;XXXXXXXXXXX;8762138;
6/25/2022;11335548962;truck;51223877;470;David;XXXXXXXXXXX;7984132;
6/25/2022;78987656137;car;44412536;126;Hunter;XXXXXXXXXXX;8976513;
6/25/2022;62494613777;truck;85236985;402;Oswald;XXXXXXXXXXX;2234567;
6/25/2022;66798852222;car;25634893;58;Eva;XXXXXXXXXXX;7456895;
6/25/2022;33365447869;truck;78541369;490;Andrew;XXXXXXXXXXX;7749986;

VBA Code:
Function wbname(MatchName As String) As String
    Dim Wb As Workbook
For Each Wb In Workbooks
    If Wb.Name Like MatchName Then
        wbname = Wb.Name
        Exit Function
    End If
Next Wb
    Wb Name = ""
End Function

Sub Car_Truck_Macro()
'
' Car_Truck_Macro Macro
'

'
Application.DisplayAlerts = False

    Columns("A:A").Select
    Selection.TextToColumns Destination:=range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :=";", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
        1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1)), TrailingMinusNumbers:=True
    Selection.ColumnWidth = 5.56
    Columns("B:B").EntireColumn.AutoFit
    Columns("C:C").EntireColumn.AutoFit
    Columns("D:D").ColumnWidth = 13.56
    Columns("E:E").EntireColumn.AutoFit
    Columns("E:E").Select
    Selection.NumberFormat = "$#,##0.00"
    Selection.NumberFormat = "$#,##0.0"
    Selection.NumberFormat = "$#,##0"
    Columns("F:F").EntireColumn.AutoFit
    Columns("G:G").EntireColumn.AutoFit
    Columns("H:H").EntireColumn.AutoFit
    range("J1").Select
    ActiveCell.FormulaR1C1 = "Process ID"
    range("J5").Select
    Columns("J:J").ColumnWidth = 16.89
    range("J1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("J:J").Select
    Selection.NumberFormat = "@"
    Rows("1:1").Select
    Selection.Font.Bold = True
    range("A2").Select
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    Sheets("Tabelle1").Select
    Sheets("Tabelle1").Name = "Car_Pay"
    range("A2").Select
    ActiveWorkbook.SaveAs Filename:= _
    "C:\Users\XXXXXXXX\Desktop\Car_Pay" & Format(Now(), "YYYYMMDD") & ".xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
Dim r As range

Vehicle_type = "truck"
Set r = ActiveWorkbook.Worksheets("Car_Pay").UsedRange

For Each cell In r.Cells
If cell.Value = Vehicle_type Then

cell.Interior.Color = 65535

End If

Next


Dim x As Integer
x = Cells(Rows.count, 2).End(xlUp).Row

MsgBox "Quantity of the processing requests for today: " & (x - 1)


'Here i need the stoping code if there is no "truck"


Dim ws As Worksheet
Set ws = Sheets("Car_Pay") '<< copy row1 from "Car_Pay"
Dim h As Double

h = ws.Cells(1, 1).RowHeight
Sheets.Add After:=ws
ActiveSheet.Name = "Truck_Pay"
ws.Rows("1:1").Copy

With ActiveSheet
.Paste
.Cells(1, 1).PasteSpecial Paste:=xlPasteColumnWidths
.Cells(1, 1).RowHeight = h
End With
 
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    
    ActiveWindow.FreezePanes = True
    Application.CutCopyMode = False
    [A1].Select
    

Dim xRg As range

Dim xCell As range

Dim A As Long

Dim B As Long

Dim C As Long

    A = Worksheets("Car_Pay").UsedRange.Rows.count

    B = Worksheets("Truck_Pay").UsedRange.Rows.count

    If B = 1 Then

    If Application.WorksheetFunction.CountA(Worksheets("Truck_Pay").UsedRange) = 0 Then B = 0

End If

    Set xRg = Worksheets("Car_Pay").range("C1:C" & A)

    On Error Resume Next

    Application.ScreenUpdating = False

    For C = 1 To xRg.count

        If CStr(xRg(C).Value) = "truck" Then

        xRg(C).EntireRow.Copy Destination:=Worksheets("Truck_Pay").range("A" & B + 1)

        B = B + 1

End If

Next

    Application.ScreenUpdating = True
    

Workbooks.Add
    ActiveWorkbook.SaveAs "C:\Users\XXXXXXXXXX\Desktop\Truck_Pay" & Format(Now, "YYYYMMDD") & ".xlsx" _
        , FileFormat:=51, CreateBackup:=False

    [A2].Select
    
    
    
Truck_Pay = wbname("Truck_Pay*")
Car_Pay = wbname("Car_Pay*")
        
Windows(Car_Pay).Activate
Sheets("Truck_Pay").Move Before:=Workbooks(Truck_Pay).Sheets(1)
    'Sheets("Truck_Pay").Copy Before:=Workbooks(Truck_Pay).Sheets(1)
'Windows(Car_Pay).Activate SaveChanges:=True



Car_Pay = wbname("Car_Pay*")

    Windows(Car_Pay).Activate

    Columns("A:A").Select

        For i = Selection.Rows.count To 1 Step -1
        If Cells(i, 3).Value = "truck" Then
        Cells(i, 3).EntireRow.Delete

End If
Next i

    [A1].Select
    
Dim xWb As Workbook
For Each xWb In Application.Workbooks
    If Not xWb.ReadOnly And Windows(xWb.Name).Visible Then
        xWb.Save
        
    End If
    Next
    
End Sub

Report with "car" only
Creation Date;Apliccation ID;Vehicle type;Owner id;Monthly payment amount;Owner name;Payment Info;Registration ID;
6/25/2022;14524687564;car;11223355;45;Adam;XXXXXXXXXXX;6546546;
6/25/2022;65456121965;car;85697412;65;Albert;XXXXXXXXXXX;6878623;
6/25/2022;65413486498;car;85697123;120;Sara;XXXXXXXXXXX;9879312;
6/25/2022;54789244877;car;65895147;36;Steve;XXXXXXXXXXX;9852317;
6/25/2022;42658652185;car;89657123;45;Laura;XXXXXXXXXXX;6134942;
6/25/2022;48798762374;car;89557874;89;John;XXXXXXXXXXX;9843213;
6/25/2022;97762185314;car;98932475;90;Ester;XXXXXXXXXXX;6876513;
6/25/2022;31684621677;car;61324878;80;Petra;XXXXXXXXXXX;8762138;
6/25/2022;78987656137;car;44412536;126;Hunter;XXXXXXXXXXX;8976513;

These report are totally fictionals a just made it to imagine what is doing my macro and i leaved a comment in the middle where a imagine the stopping code if there are no "truck", And in the macro you should change the path of the save, because i could not figured it out how to leave out users and username from the path (i just wanted to save directly to the desktop) maybe there is a way for that as well.

Thank You and i hope this is gonna help to imagine everything what we were talking about
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
(UNTESTED) I have cleaned up your macro a bit. If I had access to your file, I may have been able to make it more efficient. Have a look at line 44 (in red) of the code. Also, please note the indentation. It makes the code easier to read.
Rich (BB code):
Sub RunAll()
    Application.DisplayAlerts = False
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:=";", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
    1), Array(6, 1), Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
    Cells.Select
    Selection.Columns.AutoFit
    Selection.Rows.AutoFit
    Columns("A:A").ColumnWidth = 3
    Columns("H:H").ColumnWidth = 2.67
    Range("J1").FormulaR1C1 = "V I. AZ."
    Columns("J:J").ColumnWidth = 17
    With Range("J1")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    Rows("1:1").Font.Bold = True
    Columns("J:J").NumberFormat = "@"
    With Columns("E:E")
        .NumberFormat = "# ##0 [$Ft-hu-HU]"
        .ColumnWidth = 20.11
    End With
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
        .FreezePanes = True
    End With
    Sheets("D_J_t_r_n").Name = "Ba_J"
    ChDir "C:\ XXXXXXXXX \Desktop"
    ActiveWorkbook.SaveAs Filename:="C:\XXXXX\XXXXXXXXX\Desktop\D_J_ba " & Format(Now(), "YYYYMMDD") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    'Checks if "po vi" exists in column C.  If it doesn't, the macro ends.
    If WorksheetFunction.CountIf(ActiveWorkbook.Sheets("Ba_J").Range("C:C"), "po vi") = 0 Then Exit Sub
    'Highlight cells based on specific text (po_vi)
    Dim r As Range, cel As Range
    Set r = ActiveWorkbook.Sheets("Ba_J").Range("C2", ActiveWorkbook.Sheets("Ba_J").Range("C" & Rows.Count).End(xlUp))
    For Each cel In r.Cells
        If cel.Value = "po vi" Then
            cel.Interior.ColorIndex = 6
        End If
    Next
    'Count the quantity for today
    Dim x As Integer
    x = Cells(Rows.Count, 2).End(xlUp).Row
    MsgBox "XXXXXXXXXXXXXX: " & (x - 1)
    'end if - here i need the stoping code if there are no yellow cells in column C or based on specific text no “po-vi“, if the cells contains any of it in column C run the rest
    'Create a "Po_J" sheet with freezed top row
    Dim ws As Worksheet
    Set ws = Sheets("Ba_J") '<< copy row1 from "Ba_J"
    Dim h As Double
    h = ws.Cells(1, 1).RowHeight
    Sheets.Add After:=ws
    ActiveSheet.Name = "Po_J"
    ws.Rows("1:1").Copy
    With ActiveSheet
        .Paste
        .Cells(1, 1).PasteSpecial Paste:=xlPasteColumnWidths
        .Cells(1, 1).RowHeight = h
    End With
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    Application.CutCopyMode = False
    'Move rows based on cell value ("po_vi")
    Dim xRg As Range, xCell As Range, xCell As Range, A As Long, B As Long, C As Long
    A = Worksheets("Ba_J").UsedRange.Rows.Count
    B = Worksheets("Po_J").UsedRange.Rows.Count
    If B = 1 Then
        If Application.WorksheetFunction.CountA(Worksheets("Po_J").UsedRange) = 0 Then B = 0
    End If
    Set xRg = Worksheets("B_J").Range("C1:C" & A)
    On Error Resume Next
    Application.ScreenUpdating = False
    For C = 1 To xRg.Count
        If CStr(xRg(C).Value) = "po_vi" Then
            xRg(C).EntireRow.Copy Destination:=Worksheets("Po_J").Range("A" & B + 1)
            B = B + 1
        End If
    Next
    Application.ScreenUpdating = True
    Workbooks.Add
    ActiveWorkbook.SaveAs "C:\Desktop\D_J_po " & Format(Now, "YYYYMMDD") & ".xlsx", FileFormat:=51, CreateBackup:=False
    'Move Sheet to D_J_po
    D_J_po = wbname("D_J_po *")
    D_J_ba = wbname("D_J_ba *")
    Windows(D_J_ba).Activate
    Sheets("Po_J").Move Before:=Workbooks(D_J_po).Sheets(1)
    'Sheets("Po_J").Copy Before:=Workbooks(D_J_po).Sheets(1)
    'Windows(D_J_ba).Activate SaveChanges:=True
    D_J_b = wbname("D_J_ba *")
    Windows(D_J_ba).Activate
    Columns("A:A").Select
    For i = Selection.Rows.Count To 1 Step -1
        If Cells(i, 3).Value = "p_vu " Then
            Cells(i, 3).EntireRow.Delete
        End If
    Next i
    'Save All open Workbooks
    Dim xWb As Workbook
    For Each xWb In Application.Workbooks
        If Not xWb.ReadOnly And Windows(xWb.Name).Visible Then
            xWb.Save
        End If
    Next xWb
End Sub

Function wbname(MatchName As String) As String
    Dim Wb As Workbook
    For Each Wb In Workbooks
        If Wb.Name Like MatchName Then
            wbname = Wb.Name
            Exit Function
        End If
    Next Wb
    Wb Name = ""
End Function
I didn't see your last post until after I posted this response. Give the macro a try and see how it works for you.
 
Upvote 0

Forum statistics

Threads
1,215,529
Messages
6,125,344
Members
449,219
Latest member
Smiqer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top