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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I'm not sure why you would need 12 macros but it is hard to help without seeing your macros and your data and without knowing what you are trying to do. It would be easier to help if you could use code tags to post your code and use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
VBA Code:
Function wbname(MatchName As String) As String

Dim Wb As Workbook

For Each Wb In Workbooks

If Wb.Name Like Match Name Then

wbname = Wb.Name

Exit Function

End If

Next Wb

Wb Name = ""

End Function





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

range("A2").Select

Columns("A:A").ColumnWidth = 3

Columns("H:H").ColumnWidth = 2.67

range("J1").Select

ActiveCell.FormulaR1C1 = "V I. AZ."

Columns("J:J").ColumnWidth = 17

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

Rows("1:1").Select

Selection.Font.Bold = True

Columns("J:J").Select

Selection.NumberFormat = "@"

Columns("E:E").Select

Selection.NumberFormat = "# ##0 [$Ft-hu-HU]"

Selection.ColumnWidth = 20.11

range("B2").Select

With ActiveWindow

.SplitColumn = 0

.SplitRow = 1

End With

ActiveWindow.FreezePanes = True

Sheets("D_J_t_r_n").Name = "Ba_J"

range("B2").Select

ChDir "C:\ XXXXXXXXX \Desktop"

ActiveWorkbook.SaveAs Filename:= _

"C:\XXXXX\XXXXXXXXX\Desktop\D_J_ba " & Format(Now(), "YYYYMMDD") & ".xlsx" _

, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False







'Highlight cells based on specific text (po_vi)



Dim r As range



V = "po vi"

Set r = ActiveWorkbook.Worksheets("Ba_J").UsedRange



For Each cell In r.Cells

If cell.Value = V Then



cell.Interior.Color = 65535



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

[A1].Select







'Move rows based on cell value ("po_vi")



Dim xRg As range



Dim xCell As range



Dim A As Long



Dim B As Long



Dim 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



[A2].Select







'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



[A1].Select



'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





End Sub
 
Last edited by a moderator:
Upvote 0
Thank you for posting the code. When posting code you should use code tags. You can do this by highlighting all the code and then clicking the 'VBA' icon in the menu. Please edit your post to add the code tags.

It seems that much of your code was created by recording a macro. This is a good way to start but the resulting code is usually inefficient. Rather than trying to interpret your code, it would be easier to help if you could follow my suggestion in Post #2 to upload a copy your file and explain in words what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data.
 
Upvote 0
Thank you for posting the code. When posting code you should use code tags. You can do this by highlighting all the code and then clicking the 'VBA' icon in the menu. Please edit your post to add the code tags.

It seems that much of your code was created by recording a macro. This is a good way to start but the resulting code is usually inefficient. Rather than trying to interpret your code, it would be easier to help if you could follow my suggestion in Post #2 to upload a copy your file and explain in words what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data.
VBA Code:
Function wbname(MatchName As String) As String

Dim Wb As Workbook

For Each Wb In Workbooks

If Wb.Name Like Match Name Then

wbname = Wb.Name

Exit Function

End If

Next Wb

Wb Name = ""

End Function





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

range("A2").Select

Columns("A:A").ColumnWidth = 3

Columns("H:H").ColumnWidth = 2.67

range("J1").Select

ActiveCell.FormulaR1C1 = "V I. AZ."

Columns("J:J").ColumnWidth = 17

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

Rows("1:1").Select

Selection.Font.Bold = True

Columns("J:J").Select

Selection.NumberFormat = "@"

Columns("E:E").Select

Selection.NumberFormat = "# ##0 [$Ft-hu-HU]"

Selection.ColumnWidth = 20.11

range("B2").Select

With ActiveWindow

.SplitColumn = 0

.SplitRow = 1

End With

ActiveWindow.FreezePanes = True

Sheets("D_J_t_r_n").Name = "Ba_J"

range("B2").Select

ChDir "C:\ XXXXXXXXX \Desktop"

ActiveWorkbook.SaveAs Filename:= _

"C:\XXXXX\XXXXXXXXX\Desktop\D_J_ba " & Format(Now(), "YYYYMMDD") & ".xlsx" _

, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False







'Highlight cells based on specific text (po_vi)



Dim r As range



V = "po vi"

Set r = ActiveWorkbook.Worksheets("Ba_J").UsedRange



For Each cell In r.Cells

If cell.Value = V Then



cell.Interior.Color = 65535



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

[A1].Select







'Move rows based on cell value ("po_vi")



Dim xRg As range



Dim xCell As range



Dim A As Long



Dim B As Long



Dim 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



[A2].Select







'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



[A1].Select



'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





End Sub
 
Upvote 0
It is a report which is received every day just once in the morning. It is a mess and it contains two different datas x and y or "po vi" and "bo vi" (i cannot specified more cause of the sensitive informations). They are specified in column "C" and rows are containing more datas but for the macro are unimportant. The point is we have a report which is a mess and we need the report to be readable this is the first part which was recorded. Than we have to separate x and y datas in two workbooks and the workbooks always has to have in the name the exact date (now YYYYMMDD). Shortly it is an efficiency project. So we are do not have to do it manually.

In the first part which is recorded it is just changing the column width from Column A to J and make the first row bold and freeze the top row. Nothing more and after that macro will save the workbook with a name and with the changing date by now.

next step is highlight the y datas or "po_vi" it highlights just the cell in the column "C" not the entire row
after the macro creates a sheet with a specific name and with the same first row as the original sheet then

a message box the quantity of work for today

and here i need the code which is tell the macro it do not have to continue if there is no y datas at all

based on specific text "po_vi" copying the matching rows to the new sheet then

the new sheet is moved to the second wordbook which has the changing date in the name as well
(this is the reason why i started with the function, so i don´t need to change the date in the macro every day)

after that in the first wokrbook and in the original sheet the macro will delete the rows which containing the y data in column C

and save all the workbooks

But sometimes in the report we only have the x datas or "ba_vi" so in this case the rest of the code is unnecessary.

I am a newbie so i did it step by step maybe there is an easier way to do it. I do not know. But the macro is working perfectly just if there is no y datas still opening an unnecessary empty workbook. This is what I could not solve alone. Thank You again and i hope i did the edit well.
 
Upvote 0
Thank you for the detailed explanation but it is still hard to visualize. Could you de-sensitize the data and upload a sample workbook? I wouldn’t need all the rows, just a dozen or so.
 
Upvote 0
Thank you for the detailed explanation but it is still hard to visualize. Could you de-sensitize the data and upload a sample workbook? I wouldn’t need all the rows, just a dozen or so.
Unfortunately i cannot do that there are only sensitive datas there. I would need a loop for column c if there is y - if no stop if yes continue. I do not know how to describe more. I run out of ideas. I would like to share the sheet but there is no way i can do that. Thank you
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,894
Members
449,477
Latest member
panjongshing

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