• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Why is Next x in VBA not stopping when a blank line is found.|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Why is Next x in VBA not stopping when a blank line is found.|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosWhy is Next x in VBA not stopping w…
sp_PrintTopic sp_TopicIcon
Why is Next x in VBA not stopping when a blank line is found.
Avatar
Peter Turrell

New Member
Members
Level 0
Forum Posts: 2
Member Since:
December 9, 2020
sp_UserOfflineSmall Offline
1
December 9, 2020 - 5:55 am
sp_Permalink sp_Print

Macro checks the database for "Yes" if blank continues and processes the statement which is saved and emailed. However upon reaching the last row continues to loop . See Next X at bottom. What am i missing to stop it from looping.? My first attempt at VBA. Assistance appreciated.

For y = 2 To 50
If Worksheets("Database").Cells(y, 9).Value = company Then
clientEmail = Worksheets("Database").Cells(y, 26)
Exit For
End If
Next y
'Set the corresponding values on the Statement
Worksheets("Statement").Cells(6, 10).Value = account
Worksheets("Statement").Cells(7, 8).Value = company

'Loop through the Statement sheet and as long as the payment status doesn't _
'change, write it to the table on the statement
StartTableRowa = 15
Do While Worksheets("Database").Cells(x, 6).Value = account And Worksheets("Database").Cells(x, 16) = "BILLED"
conf = Worksheets("Database").Cells(x, 1).Value
puDate = Worksheets("Database").Cells(x, 3).Value
invoiceNo = Worksheets("Database").Cells(x, 23).Value
PmtStatus = Worksheets("Database").Cells(x, 16).Value
passenger = Worksheets("Database").Cells(x, 8).Value
TripTotal = Worksheets("Database").Cells(x, 18).Value
Worksheets("Statement").Cells(StartTableRowa, 3) = puDate
Worksheets("Statement").Cells(StartTableRowa, 4) = invoiceNo
Worksheets("Statement").Cells(StartTableRowa, 5) = PmtStatus
Worksheets("Statement").Cells(StartTableRowa, 6) = conf
Worksheets("Statement").Cells(StartTableRowa, 7) = passenger
Worksheets("Statement").Cells(StartTableRowa, 9) = TripTotal
Worksheets("Database").Cells(x, 27).Value = "Yes"
x = x + 1
StartTableRowa = StartTableRowa + 1
Loop

'Need to subtract one from x to loop through the row again
x = x - 1

Worksheets("Statement").Activate
statementFile = ActiveWorkbook.Path & "/" & ActiveSheet.Name & "_" & _
company & ".pdf"

'Export activesheet as PDF
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=statementFile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End With

'Set Outlook information
Set mailapp = CreateObject("Outlook.Application")
Set myMail = mailapp.CreateItem(0)
With myMail
.To = clientEmail
.CC = ""
.BCC = ""
.Subject = " Statement " & account
.HTMLBody = "Please see the attached statement. Thank you for your business."
.display
.attachments.Add statementFile
End With

'Clear data on invoice

Worksheets("Statement").Cells(7, 8).Value = " "
Worksheets("Statement").Cells(5, 10).Value = " "
Worksheets("Statement").Cells(6, 10).Value = " "

'Clear data in table on invoice
For t = 15 To 20
Worksheets("Statement").Cells(t, 3).Value = ""
Worksheets("Statement").Cells(t, 4).Value = ""
Worksheets("Statement").Cells(t, 5).Value = ""
Worksheets("Statement").Cells(t, 6).Value = ""
Worksheets("Statement").Cells(t, 7).Value = ""
Worksheets("Statement").Cells(t, 8).Value = ""
Worksheets("Statement").Cells(t, 9).Value = ""
Next t

End If

Next x

MsgBox ("All emails have been generated.")

End Sub

Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
2
December 9, 2020 - 10:04 pm
sp_Permalink sp_Print
Good morning everyone,

could you attach a demo of the file, without confidential data, it would be much easier for someone to help, recreating everything takes a long time

Miguel,
Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
3
December 10, 2020 - 7:38 am
sp_Permalink sp_Print

As Miguel has said we need a workbook with the code so that we dont waste time recreating data.

that said, having a quick look though your code it doesnt even look complete - i cant see where you have started the X loop - i can see a start and end Y, and a start and end t, but not X

Avatar
Peter Turrell

New Member
Members
Level 0
Forum Posts: 2
Member Since:
December 9, 2020
sp_UserOfflineSmall Offline
4
December 10, 2020 - 10:52 pm
sp_Permalink sp_Print

Thanks guys, I never thought of posting the whole code as it is rather long. I shall bear that in mind for the future.

Painstakingly I retyped the whole code over again and it is working. Whether there was a space or not in the first time I did it is unknown. 

You may close this item.

Peter

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: dan conner, Christoffer Sandberg
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 204
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Jefferson Granemann
Glen Coulthard
Nikki Fox
Rachele Dickie
Raj Mattoo
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27291

 

Member Stats:
Guest Posters: 49
Members: 31912
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.