Help - VBA If no data in column stop code and display message (Code included)

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
Hello,

I have the following start code to my document.
I am trying to edit it so that if the sheet Data has no data in A2 or below it stops the SUB and displays a message saying there is no data based on the user selection, please change parameters and try again.

I tried with IF and ELSE but it is giving errors. Any help is appreciated!





VBA Code:
Sub AStart()


Application.ScreenUpdating = False ' Added to speed up the process
Sheets("Data").Cells.Clear ' Clear Data WS in case data was accidentally left over.

    Workbooks.Open Filename:="C:\temp\ReportOutput.xls" ' Opens Access exported Excel file
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy ' Select and copy all the data from ReportOutput.xls
    Windows("Report.xlsm").Activate
    Sheets("Data").Select
    Range("A1").Select
    ActiveSheet.Paste    ' Activate and Paste data back over to
    Application.CutCopyMode = False
    'Sheets.Add Before:=ActiveSheet
    
    
[B] ' This code is being placed so if there is no data to exit any further code and let user no there is no data between that range.
 
If Data.Range("A2").Value = "" Then Exit Sub[/B]


    Sheets("PivotTable").Select
    Application.DisplayAlerts = True
    Set PSheet = Worksheets("PivotTable")
    Set DSheet = Worksheets("Data")
    'Define Data Range
    lastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set PRange = DSheet.Cells(1, 1).Resize(lastRow, LastCol)
    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(5, 1), _
    TableName:="PivotTable2")


Call AMasterBuild2

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi LNG2013,

what about

VBA Code:
Sub MrE1219954()
'https://www.mrexcel.com/board/threads/help-vba-if-no-data-in-column-stop-code-and-display-message-code-included.1219954/
With ActiveSheet
  If .UsedRange.Cells.Count = 1 And .Range("A1").Value = "" Then Exit Sub
End With
MsgBox "Continuing..."
End Sub

You should start working on the code to get rid of all the Selections...

Ciao,
Holger
 
Upvote 0
Solution
Hi LNG2013,

what about

VBA Code:
Sub MrE1219954()
'https://www.mrexcel.com/board/threads/help-vba-if-no-data-in-column-stop-code-and-display-message-code-included.1219954/
With ActiveSheet
  If .UsedRange.Cells.Count = 1 And .Range("A1").Value = "" Then Exit Sub
End With
MsgBox "Continuing..."
End Sub

You should start working on the code to get rid of all the Selections...

Ciao,
Holger
Thank you
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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