• 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

Nested For Loop Error|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Nested For Loop Error|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 & MacrosNested For Loop Error
sp_PrintTopic sp_TopicIcon
Nested For Loop Error
Avatar
Carlos Arcilla
Member
Members
Level 0
Forum Posts: 10
Member Since:
June 1, 2020
sp_UserOfflineSmall Offline
1
July 25, 2020 - 6:40 am
sp_Permalink sp_Print

I will explain what I am trying to do. On the active sheet that I am running the macro on, there are 2 potential cells that I want pasted on the sheet "Shipping Dept". These active sheet cells are K5 and L5. K5 is considered the packing number and is calculated by a formula depending on numbers that are assigned to each row in the Shipping Dept sheet. This calculation is also dependent on which rows on Shipping Dept are visible because of filters. Provided this information, I have 3 situations that I want and I thought this code could achieve it, but I keep getting an "for control variable already in use" error. As seen below, each designated column is assigned to names called check1, check2, etc... I want K5 to be copy and pasted on the designated column for the visible rows through these situations....

1) It looks through each cell in check1 first. If that visible cell is empty, paste K5 on there.

2) If there has already been a Packing # put on there and it is the SAME, then I want to copy and paste L5 (L5 is from active sheet, not Shipping Dept) onto that cell.

3) If the cell already has a Packing # but it is not the same, I want it placed on that same row but for the column of check2. 

... check 2 has to run through these same conditions though... and so on all the way to check5. If that specific row is filled in all columns up to check 5, there should be an error.

Other than the error, I thought I wrote my code correctly. Turns out it did not work. Please help. Here is my code:

 

ActiveSheet.Range("K5").Select
Selection.Copy
Sheets("Shipping Dept").Select
Set check1 = Range("Q2:Q6402").SpecialCells(xlCellTypeVisible)
Set check2 = Range("U2:U6402").SpecialCells(xlCellTypeVisible)
Set check3 = Range("Y2:Y6402").SpecialCells(xlCellTypeVisible)
Set check4 = Range("AC2:AC6402").SpecialCells(xlCellTypeVisible)
Set check5 = Range("AG2:AG6402").SpecialCells(xlCellTypeVisible)
For Each cell In check1
If cell = "" Then
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell = Sheets("Enerflex Shipping Ticket").Range("K5") Then
Sheets("Enerflex Shipping Ticket").Range("L5").Select
Selection.Copy
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell <> "" And cell <> Sheets("Enerflex Shipping Ticket").Range("K5") Then
'How do I say paste on same row but PL #2 column, but once it does this, it has to check the same if's..?
For Each cell In check2
If cell = "" Then
Sheets("Shipping Dept").Select
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell = Sheets("Enerflex Shipping Ticket").Range("K5") Then
Sheets("Enerflex Shipping Ticket").Range("L5").Select
Selection.Copy
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell <> "" And cell <> Sheets("Enerflex Shipping Ticket").Range("K5") Then
For Each cell In check3
If cell = "" Then
Sheets("Shipping Dept").Select
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell = Sheets("Enerflex Shipping Ticket").Range("K5") Then
Sheets("Enerflex Shipping Ticket").Range("L5").Select
Selection.Copy
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell <> "" And cell <> Sheets("Enerflex Shipping Ticket").Range("K5") Then
For Each cell In check4
If cell = "" Then
Sheets("Shipping Dept").Select
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell = Sheets("Enerflex Shipping Ticket").Range("K5") Then
Sheets("Enerflex Shipping Ticket").Range("L5").Select
Selection.Copy
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell <> "" And cell <> Sheets("Enerflex Shipping Ticket").Range("K5") Then
For Each cell In check5
If cell = "" Then
Sheets("Shipping Dept").Select
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell = Sheets("Enerflex Shipping Ticket").Range("K5") Then
Sheets("Enerflex Shipping Ticket").Range("L5").Select
Selection.Copy
MsgBox "Warning. There is already shipment date(s) for past shipment(s). You may proceed."
cell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ElseIf cell <> "" And cell <> Sheets("Enerflex Shipping Ticket").Range("K5") Then
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
July 25, 2020 - 11:11 am
sp_Permalink sp_Print

Hi Carlos,

https://www.myonlinetraininghu.....this-first

Please post your workbook so we don't have to recreate it.

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Lynnette Altomari, Jessica Stewart, Roy Lutke, Jeff Krueger
Guest(s) 12
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Sopi Yuniarti
sandra parker
LAFONSO HERNANDEZ
Hayden Hao
Angela chen
Sean Moore
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Forum Stats:
Groups: 3
Forums: 24
Topics: 6214
Posts: 27243

 

Member Stats:
Guest Posters: 49
Members: 31894
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.