VBA code not using accurate master sheet when looping back second, third timeWhen writing VBA I cannot get two specific errors to run throughWhen to use Set in my VBA code?sub terminates but desired workbook not displayedCode needed to unhide very hidden sheets in excel vba“Run-Time error '438': Object doesn't support this property or method.” Range.values = Range.valuesHow to preserve/retain hyperlinks in email body when using RangetoHTML from ExcelLoop through worksheets while exporting range as imageConditional copy Excel File-2 data to excel file-1?Excel - Exporting multiple sheets to .pdf via VBA codeShow only selected table column after filter to new worksheetsExcel VBA with matches and index

What's the in-universe reasoning behind sorcerers needing material components?

Do UK voters know if their MP will be the Speaker of the House?

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

How badly should I try to prevent a user from XSSing themselves?

Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?

Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?

Can the Meissner effect explain very large floating structures?

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

Personal Teleportation: From Rags to Riches

Mathematica command that allows it to read my intentions

Size of subfigure fitting its content (tikzpicture)

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

Why no variance term in Bayesian logistic regression?

Why didn't Miles's spider sense work before?

Unable to supress ligatures in headings which are set in Caps

Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?

What is the most common color to indicate the input-field is disabled?

One verb to replace 'be a member of' a club

Should I cover my bicycle overnight while bikepacking?

How seriously should I take size and weight limits of hand luggage?

What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?

Unlock My Phone! February 2018

Alternative to sending password over mail?

Do scales need to be in alphabetical order?



VBA code not using accurate master sheet when looping back second, third time


When writing VBA I cannot get two specific errors to run throughWhen to use Set in my VBA code?sub terminates but desired workbook not displayedCode needed to unhide very hidden sheets in excel vba“Run-Time error '438': Object doesn't support this property or method.” Range.values = Range.valuesHow to preserve/retain hyperlinks in email body when using RangetoHTML from ExcelLoop through worksheets while exporting range as imageConditional copy Excel File-2 data to excel file-1?Excel - Exporting multiple sheets to .pdf via VBA codeShow only selected table column after filter to new worksheetsExcel VBA with matches and index













0















The VBA I created is creating the separate sheets. However (in this example there is three sheets being created), after the first sheet is created and the VBA runs back through the code, it copies the first sheet that was created, rather than the master sheet, which already has the data needed for the second and third tab filtered out so there is no data in the final two tabs. Do you know what could be causing this? Code below:



Option Explicit

Sub InvoiceSeperator()

' Declare objects
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim targetRange As Range
Dim SplitOrderNum As Range
Dim OrderData As Range
Dim sourceCell As Range
Dim allOrders As Range
Dim invoicenumbers As Range

' Declare other variables
Dim sourceSheetName As String
Dim sourceRangeName As String
Dim targetSheetName As String
Dim targetRangeName As String

Dim lastSheetHidden As Boolean

' <<< Customize this >>>
sourceSheetName = "Invoices"
targetSheetName = "SumToLineItem"
sourceRangeName = "SplitOrderNum"
targetRangeName = "OrderData"

' Initialize the source sheet
Set targetSheet = ThisWorkbook.Sheets("SumToLineItem")

Set allOrders = targetSheet.Range("B:B")
allOrders.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Sheets(2).Select
Sheets(2).Name = "Invoices"
Application.CutCopyMode = False
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

Set sourceSheet = ThisWorkbook.Sheets("Invoices")
sourceSheet.Range("A2").Select
sourceSheet.Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="SplitOrderNum", RefersToR1C1:= _
"=Invoices!R2C1:R4C1"

' Initialize the range (Add full qualifier to the current workbook, sheet and range)
Set sourceRange = sourceSheet.Range("SplitOrderNum")
' Get if last sheet is visible in current workbook
lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True

For Each sourceCell In sourceRange
' Copy the source worksheet
targetSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count)
' Rename the new worksheet
Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value
' Reference to the added worksheet
Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

With targetSheet.Range(targetRangeName)
.AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value,.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

' Check this next line if this should point to the orderdata range too (?)
targetSheet.AutoFilter.ShowAllData

Next sourceCell

' Return the last sheet visible state
If lastSheetHidden = False Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden
End If

End Sub









share|improve this question
























  • Code looks incomplete

    – Ricardo Diaz
    Mar 7 at 23:34











  • Updated, not sure why that happened.

    – Connor Smith
    Mar 7 at 23:37











  • You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

    – Ricardo Diaz
    Mar 8 at 2:05















0















The VBA I created is creating the separate sheets. However (in this example there is three sheets being created), after the first sheet is created and the VBA runs back through the code, it copies the first sheet that was created, rather than the master sheet, which already has the data needed for the second and third tab filtered out so there is no data in the final two tabs. Do you know what could be causing this? Code below:



Option Explicit

Sub InvoiceSeperator()

' Declare objects
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim targetRange As Range
Dim SplitOrderNum As Range
Dim OrderData As Range
Dim sourceCell As Range
Dim allOrders As Range
Dim invoicenumbers As Range

' Declare other variables
Dim sourceSheetName As String
Dim sourceRangeName As String
Dim targetSheetName As String
Dim targetRangeName As String

Dim lastSheetHidden As Boolean

' <<< Customize this >>>
sourceSheetName = "Invoices"
targetSheetName = "SumToLineItem"
sourceRangeName = "SplitOrderNum"
targetRangeName = "OrderData"

' Initialize the source sheet
Set targetSheet = ThisWorkbook.Sheets("SumToLineItem")

Set allOrders = targetSheet.Range("B:B")
allOrders.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Sheets(2).Select
Sheets(2).Name = "Invoices"
Application.CutCopyMode = False
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

Set sourceSheet = ThisWorkbook.Sheets("Invoices")
sourceSheet.Range("A2").Select
sourceSheet.Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="SplitOrderNum", RefersToR1C1:= _
"=Invoices!R2C1:R4C1"

' Initialize the range (Add full qualifier to the current workbook, sheet and range)
Set sourceRange = sourceSheet.Range("SplitOrderNum")
' Get if last sheet is visible in current workbook
lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True

For Each sourceCell In sourceRange
' Copy the source worksheet
targetSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count)
' Rename the new worksheet
Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value
' Reference to the added worksheet
Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

With targetSheet.Range(targetRangeName)
.AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value,.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

' Check this next line if this should point to the orderdata range too (?)
targetSheet.AutoFilter.ShowAllData

Next sourceCell

' Return the last sheet visible state
If lastSheetHidden = False Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden
End If

End Sub









share|improve this question
























  • Code looks incomplete

    – Ricardo Diaz
    Mar 7 at 23:34











  • Updated, not sure why that happened.

    – Connor Smith
    Mar 7 at 23:37











  • You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

    – Ricardo Diaz
    Mar 8 at 2:05













0












0








0








The VBA I created is creating the separate sheets. However (in this example there is three sheets being created), after the first sheet is created and the VBA runs back through the code, it copies the first sheet that was created, rather than the master sheet, which already has the data needed for the second and third tab filtered out so there is no data in the final two tabs. Do you know what could be causing this? Code below:



Option Explicit

Sub InvoiceSeperator()

' Declare objects
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim targetRange As Range
Dim SplitOrderNum As Range
Dim OrderData As Range
Dim sourceCell As Range
Dim allOrders As Range
Dim invoicenumbers As Range

' Declare other variables
Dim sourceSheetName As String
Dim sourceRangeName As String
Dim targetSheetName As String
Dim targetRangeName As String

Dim lastSheetHidden As Boolean

' <<< Customize this >>>
sourceSheetName = "Invoices"
targetSheetName = "SumToLineItem"
sourceRangeName = "SplitOrderNum"
targetRangeName = "OrderData"

' Initialize the source sheet
Set targetSheet = ThisWorkbook.Sheets("SumToLineItem")

Set allOrders = targetSheet.Range("B:B")
allOrders.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Sheets(2).Select
Sheets(2).Name = "Invoices"
Application.CutCopyMode = False
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

Set sourceSheet = ThisWorkbook.Sheets("Invoices")
sourceSheet.Range("A2").Select
sourceSheet.Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="SplitOrderNum", RefersToR1C1:= _
"=Invoices!R2C1:R4C1"

' Initialize the range (Add full qualifier to the current workbook, sheet and range)
Set sourceRange = sourceSheet.Range("SplitOrderNum")
' Get if last sheet is visible in current workbook
lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True

For Each sourceCell In sourceRange
' Copy the source worksheet
targetSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count)
' Rename the new worksheet
Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value
' Reference to the added worksheet
Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

With targetSheet.Range(targetRangeName)
.AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value,.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

' Check this next line if this should point to the orderdata range too (?)
targetSheet.AutoFilter.ShowAllData

Next sourceCell

' Return the last sheet visible state
If lastSheetHidden = False Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden
End If

End Sub









share|improve this question
















The VBA I created is creating the separate sheets. However (in this example there is three sheets being created), after the first sheet is created and the VBA runs back through the code, it copies the first sheet that was created, rather than the master sheet, which already has the data needed for the second and third tab filtered out so there is no data in the final two tabs. Do you know what could be causing this? Code below:



Option Explicit

Sub InvoiceSeperator()

' Declare objects
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim targetRange As Range
Dim SplitOrderNum As Range
Dim OrderData As Range
Dim sourceCell As Range
Dim allOrders As Range
Dim invoicenumbers As Range

' Declare other variables
Dim sourceSheetName As String
Dim sourceRangeName As String
Dim targetSheetName As String
Dim targetRangeName As String

Dim lastSheetHidden As Boolean

' <<< Customize this >>>
sourceSheetName = "Invoices"
targetSheetName = "SumToLineItem"
sourceRangeName = "SplitOrderNum"
targetRangeName = "OrderData"

' Initialize the source sheet
Set targetSheet = ThisWorkbook.Sheets("SumToLineItem")

Set allOrders = targetSheet.Range("B:B")
allOrders.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Sheets(2).Select
Sheets(2).Name = "Invoices"
Application.CutCopyMode = False
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

Set sourceSheet = ThisWorkbook.Sheets("Invoices")
sourceSheet.Range("A2").Select
sourceSheet.Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="SplitOrderNum", RefersToR1C1:= _
"=Invoices!R2C1:R4C1"

' Initialize the range (Add full qualifier to the current workbook, sheet and range)
Set sourceRange = sourceSheet.Range("SplitOrderNum")
' Get if last sheet is visible in current workbook
lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True

For Each sourceCell In sourceRange
' Copy the source worksheet
targetSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count)
' Rename the new worksheet
Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value
' Reference to the added worksheet
Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

With targetSheet.Range(targetRangeName)
.AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value,.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

' Check this next line if this should point to the orderdata range too (?)
targetSheet.AutoFilter.ShowAllData

Next sourceCell

' Return the last sheet visible state
If lastSheetHidden = False Then
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden
End If

End Sub






excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 7:37









Pᴇʜ

25.1k63052




25.1k63052










asked Mar 7 at 22:35









Connor SmithConnor Smith

35




35












  • Code looks incomplete

    – Ricardo Diaz
    Mar 7 at 23:34











  • Updated, not sure why that happened.

    – Connor Smith
    Mar 7 at 23:37











  • You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

    – Ricardo Diaz
    Mar 8 at 2:05

















  • Code looks incomplete

    – Ricardo Diaz
    Mar 7 at 23:34











  • Updated, not sure why that happened.

    – Connor Smith
    Mar 7 at 23:37











  • You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

    – Ricardo Diaz
    Mar 8 at 2:05
















Code looks incomplete

– Ricardo Diaz
Mar 7 at 23:34





Code looks incomplete

– Ricardo Diaz
Mar 7 at 23:34













Updated, not sure why that happened.

– Connor Smith
Mar 7 at 23:37





Updated, not sure why that happened.

– Connor Smith
Mar 7 at 23:37













You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

– Ricardo Diaz
Mar 8 at 2:05





You're mixing the sourceSheet and targetSheet concepts. Please explain in steps what you want to achieve with "Invoices" and the ranges you defined.

– Ricardo Diaz
Mar 8 at 2:05












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053905%2fvba-code-not-using-accurate-master-sheet-when-looping-back-second-third-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053905%2fvba-code-not-using-accurate-master-sheet-when-looping-back-second-third-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved