TotallyConfused
Board Regular
- Joined
- May 4, 2017
- Messages
- 247
- Office Version
- 365
- Platform
- Windows
Hello all you MrExcel experts
This concerns my posting #5 & #6 in a thread started by Hitbid called ‘Question about optimizing simple code’ dated Nov. 3, 2018. The link to this is:
https://www.mrexcel.com/forum/excel-questions/1076409-question-about-optimizing-simple-code.html
I had offered a solution, then Hitbid said that the sheets were NOT named ‘Sheet1’, ‘Sheet2’ etc., as was indicated in the opening post, but that they had been renamed. Thinking my solution would still be valid, I renamed the sheets in my test workbook and ran my code again, only to find that it no longer worked. I now get ‘error msg # 9 – Subscript out of range’ on the line of ‘With Sheets(“Sheet” & X)’. This is happening since Excel doesn’t seem to make the connection between the original name of the sheets and what I renamed them.
It seems to me that quite some time ago, I read in another thread that it didn’t matter whether we renamed sheets or not, because Excel used the original default name, for example Sheet1. Perhaps I misunderstood what was being said at that time. By using Ctrl-R, I was able to display the VBAProject screen, which showed the sheets original name as well as what I renamed them to.
Below is my solution to the OP’s question, though I can’t take full credit for this because all I did was to modify the solution posted by Fluff.
My questions:
1 ) Is there any way to directly refer to the original sheet name when using VBA, so the code wouldn’t crash if a user renamed a sheet?
2 ) If #1 is not possible, then is there a way when using VBA, to access the VBAProject information to determine if anything had been renamed, and if so, to access the sheet’s new name?
3 ) If #2 is possible and using my VBAProject example given above, what code would be necessary to accomplish this? I’m wondering if somehow VBA could activate the VBAProject screen (Ctrl-R), load the results into a two-dimensional array with the original name and new name being placed in separate array elements, then scan the array picking out the needed data, but I have no clue how this could be done.
Thank you in advance for any suggestions or help you may be able to offer.
TotallyConfused
This concerns my posting #5 & #6 in a thread started by Hitbid called ‘Question about optimizing simple code’ dated Nov. 3, 2018. The link to this is:
https://www.mrexcel.com/forum/excel-questions/1076409-question-about-optimizing-simple-code.html
I had offered a solution, then Hitbid said that the sheets were NOT named ‘Sheet1’, ‘Sheet2’ etc., as was indicated in the opening post, but that they had been renamed. Thinking my solution would still be valid, I renamed the sheets in my test workbook and ran my code again, only to find that it no longer worked. I now get ‘error msg # 9 – Subscript out of range’ on the line of ‘With Sheets(“Sheet” & X)’. This is happening since Excel doesn’t seem to make the connection between the original name of the sheets and what I renamed them.
It seems to me that quite some time ago, I read in another thread that it didn’t matter whether we renamed sheets or not, because Excel used the original default name, for example Sheet1. Perhaps I misunderstood what was being said at that time. By using Ctrl-R, I was able to display the VBAProject screen, which showed the sheets original name as well as what I renamed them to.
Code:
VBAProject (Book 1)
Microsoft Excel Objects
Sheet1 (MySheet1)
Sheet2 (MySheet2)
Sheet3 (MySheet3)
Sheet4 (Test)
ThisWorkbook
Modules
Module1
Code:
Function B_Row1(Sht As String)
B_Row1 = Sheets(Sht).Cells(Rows.Count, 1).End(xlUp).Row
End Function
Function B_Row2(Sht As String)
B_Row2 = Sheets(Sht).Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
End Function
Sub Master_Finder()
Application.ScreenUpdating = False
Dim X As Integer
For X = 1 To 3
With Sheets("Sheet" & X)
.Range(.Cells(1, 1), .Cells(B_Row1(.Name), 2)).Copy Sheets("Test").Cells(B_Row2("Test"), 1)
End With
Next
Sheets("Test").Activate
ActiveWindow.ScrollRow = 1
Application.ScreenUpdating = True
End Sub
1 ) Is there any way to directly refer to the original sheet name when using VBA, so the code wouldn’t crash if a user renamed a sheet?
2 ) If #1 is not possible, then is there a way when using VBA, to access the VBAProject information to determine if anything had been renamed, and if so, to access the sheet’s new name?
3 ) If #2 is possible and using my VBAProject example given above, what code would be necessary to accomplish this? I’m wondering if somehow VBA could activate the VBAProject screen (Ctrl-R), load the results into a two-dimensional array with the original name and new name being placed in separate array elements, then scan the array picking out the needed data, but I have no clue how this could be done.
Thank you in advance for any suggestions or help you may be able to offer.
TotallyConfused