Error 1004 After I added a new sheet?

elcolio

New Member
Joined
Jan 25, 2012
Messages
7
I added another sheet to my workbook and now my macro buttons are messed up, even though I am referring to the worksheet by sheet number.

This is the problem function, it is supposed to pull a random percentage of the rows on the FromSheet, then copy them to a temporary hidden sheet, Sheet7, then clear the FromSheet and copy the selected rows back:

Code:
Sub PullRandom(FromSheet As Integer, Pct As Double)
Randomize
Dim SelRow As Range
Dim MyRows() As Integer
Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
  numRows = Sheets(FromSheet).Range("A" & Rows.Count).End(xlUp).Row - 1
   percRows = CInt(numRows * Pct)
    ReDim MyRows(percRows)
     For nxtRow = 2 To percRows
getNew:
      nxtRnd = Int((numRows) * Rnd + 1)
       For chkRnd = 2 To nxtRow
        If MyRows(chkRnd) = nxtRnd Then GoTo getNew
       Next
      MyRows(nxtRow) = nxtRnd
     Next
 
  For copyRow = 2 To percRows
   Sheets(FromSheet).Rows(MyRows(copyRow) + 1).EntireRow.Copy _
     Destination:=Sheets(7).Cells(copyRow, 1)
  Next
 
Sheets(FromSheet).Range("A2:IV65536").Clear
  For copyRow = 2 To percRows
   Sheets(7).Rows(copyRow).EntireRow.Copy _
     Destination:=Sheets(FromSheet).Cells(copyRow, 1)
  Next
Sheets(7).Cells.Clear
Sheets(FromSheet).Rows("1:10000").RowHeight = 13.5
Sheets(FromSheet).Select
insNumCol (percRows)
End Sub

I have 3 buttons that reference this sub, the first works perfectly, the second gives error 1004 at the Sheets(FromSheet).Select line, and the third works but is off by one sheet.

What I don't understand here is that I am referencing the sheets by sheet number. Shouldn't Sheets(8) be Sheets(8) every time? How would adding a sheet (Sheet20, if it matters) mess up this macro?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Please provide more details about the following:

1. You are referencing sheets by their index number, which means you are maintaining sheets order, and sheet7 is always the 7<sup>th</sup> sheet from left with Index value of 7. Correct?

2. Are the three buttons calling the same procedure PullRandom? What is the code associated with each one?


Since you are refering to sheet by index number, adding a sheet could create a mess if it is inserted in the middle or at the beginning. For example, if sheet20 is added as the first (physical) sheet, its index will be 1 though its name is Sheet20 and sheet7 will be shifted to the right and becomes Sheets(8).
 
Upvote 0
Ah there's my problem! I was referencing Sheets(7), but should have been referencing Sheet7. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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