Forum Discussion
waygar
Nov 10, 2022Brass Contributor
To run a macro on another excel sheet with a different number of rows
Hi, I recorded a macro but when I try to use it on another spreadsheet with a different number of rows I get Error 9. Help I am new to this.
HansVogelaar
Jan 11, 2023MVP
I streamlined the code a bit. It works on the first run for me...
See the attached version (now a macro-enabled workbook).
waygar
Jan 11, 2023Brass Contributor
Thanks Hans,
However when I put your code into a module within my PERSONAL.XLSB there are errors.
This was where my own macro sat and had the unusual two pass requirement. Could placing the macro within PERSONAL.XLSB cause issues? Your code now produces a runtime error 9
However when I put your code into a module within my PERSONAL.XLSB there are errors.
This was where my own macro sat and had the unusual two pass requirement. Could placing the macro within PERSONAL.XLSB cause issues? Your code now produces a runtime error 9
- waygarJan 11, 2023Brass ContributorOk I now know what the cause is - assigning a shortcut key to the macro.
Even with your code, assigning a shortcut key such as CTRL SHFT U makes the code only work if run twice. I don't know why! - HansVogelaarJan 11, 2023MVP
I cannot explain that.
P.S. This discussion has become very long and awkward to navigate.
If you have a new question unrelated to the current discussion, would you be so kind to start a new discussion? Thanks in advance.
- waygarJan 11, 2023Brass ContributorSorry Hans
I did have a typo and your code now works in PERSONAL.XLSB
Why my code needs the dual run is a mystery? - HansVogelaarJan 11, 2023MVP
In that case, I have no idea why it doesn't work, but you could try
Set wbkClass = ActiveWorkbookas in your original code.
- waygarJan 11, 2023Brass ContributorYes
- HansVogelaarJan 11, 2023MVP
Are you sure that your workbook is named Classf.xlsx and that it is open in Excel?
- waygarJan 11, 2023Brass ContributorNope same error.
Runs perfectly if I remove Personal.XLSB from my XLSTART - HansVogelaarJan 11, 2023MVP
Yes, that requires a change to the code. Change the line
Set wbkClass = ThisWorkbookto
Set wbkClass = Workbooks("Classf.xlsx")