Sheet not found via codename - vba

I'm running across and error that appears sporadically. Essentially, a master .xlsm file is used by multiple people to populate data for aggregation. I then use another .xlsm file with macros to pull the data and aggregate it into a single source file.
The code generally works quite well, with one exception:
'define range based on count'
Set rngItemRange = SourceWb.Sheets("Quality").Range(Cells(3, i), Cells((intItemCount + 2), i))
'write concatenated range'
targetwb.Sheets("Raw Data").Cells(pintDest_row, pintCol).Value = concatRange(rngItemRange)
It cannot find the tab "Quality" on some work books (but not all). Okay, maybe someone renamed the tab? checked that, thats not true. One thing that works as a work around: on the open source workbook, when debug throws an error, if i manually click the quality tab, then resume the macro, it will continue.
I also said, okay, well maybe there's some weird character recognition issue, so I started to refer to the sheet by the codename as shown in the vba editor. I experience the same behaviors.
This affects maybe 50% of the workbooks, and I cant find any root cause. I have similar code elsewhere, for different sheets, but this is the only one where i define a range to pass into a function using the "set" command. Again, this only happens sometimes on some workbooks, and i can continue to execute when i manually click on the tab i'm pulling data from.
I'll also add, there is only one workbook open with this sheet name, at any given time, so i dont think it's choking trying to figure which sheet is relevant. Plus sourceWb is a set variable.
Does someone have any clue whats going on? anything to try? solutions? help!
(Forgive any typos, I'm working on a broken thumb right now.)

Related

Excel stops scrolling after a Copy command

I have a set of data entries in the 2nd Worksheet of my Workbook, which are part of a list using Filters. Using VBA I have created a button that when clicked, opens a Userform which lets the user enter their desired data selection to be copied from the 2nd Worksheet to the 1st Worksheet. This is done by having the user checking 1 out of 8 possible OptionButtons and then having the user select 1 of 5 possible options from a ComboBox. All is well and every possible combination in the Userform inputs leads to the correct data being copied from the 2nd Worksheet in to the 1st Worksheet. However, after a successful copy, I am unable to scroll using my scroll wheel in my 1st Worksheet. The "square" moves in the scrollbar when scrolling with the scroll wheel, however the worksheet does not move.
The main differences between the two Worksheets are that in the 1st Worksheet, no filters are present and that the copied selection is always less data then the source data, however this isn't to big of a difference (source data in 2nd worksheet is about 300 rows, smallest copy possibility is around 12 rows). I've searched and tried the following solutions, with the accompanying results.
Unfreeze Panes; One solution suggested was to unfreeze panes as Excel would possibly freeze all rows after the copy. However, when checked there was no "Unfreeze" option, indicating that I had no frozen panes to begin with. Also, when I clicked any of the three "Freeze" options, thinking I would try to manually "Freeze" and then "Unfreeze" to resolve the issue, Excel would stop working, giving me the "Not Responding" in the title bar and needed to be shut down/restarted.
Select Objects; Another suggestion was that Excel would have the "Select Objects" checked in the "Find & Select" portion of the "Home" tab. When un-checking this, the issue should be resolved. However, it wasn't checked to begin with. Checking it and then un-checking it did not resolve the problem.
Switch Worksheets; When I manually click the 2nd Worksheet after a copy command and then click back to the 1st Worksheet, the problem is resolved and I can use the scroll wheel again. However, I intend to use this Workbook a lot and having to constantly click back and forth is going to be a pain. So this isn't really a solution, more of a work around.
Dragging; Another work around I have found is that clicking and then dragging the "square" in the scrollbar does move the worksheet around. However, this is again just a temporarily fix.
One more thing that may be useful to mention. In both Worksheet 1 & 2 I am using collapsible columns. Worksheet 2 also contains collapsible rows, where Worksheet 1 does not.
Okay, so on a whim I tried to enter the following 2 lines of code to the end of the sub
ThisWorkbook.Sheets("Overview").Activate 'Activates the 2nd Worksheet
ThisWorkbook.Sheets("Selection").Activate 'Activates the 1st Worksheet
This mimics the manually triggering of switching Worksheets. And it resolved the problem. Still don't understand why the problem occurred but it's solved anyway.
I had a similar issue and my initial workaround was to make the userForm modeless:
Sub addButton()
builderForm.Show (0)
End Sub
I wasn't a huge fan of leaving it modeless, so I ended up using your sheet toggling method like this:
Sub addButton()
builderForm.Show
Sheets("Cover").Activate
Sheets(Worksheets.count - 10).Activate
End Sub
I still don't fully understand why it is happening; it's almost like windows/excel loses track of which sheet is the active one while the userForm is running.
Thanks for your submission/answer!
This also happens when a worksheet has too many copied Conditional Formatting rules. Once I cleaned up my extra/duplicated rules, I could scroll again after copying a cell.
Found good info about copying/pasting with or without the Conditional Formats here https://superuser.com/questions/419287/how-to-copy-paste-without-conditional-formatting

VBA Import/Copy Specifc Worksheet witout Opening Workbook

thanks in advance.
I've spent nearly two days routing around but can't quite find/workout what I need.
I need to get an entire specific worksheet from a closed workbook without opening (even in the background).
Further info:
1) My source file has several sheets that I don't want/need. I only need one and it seems sloppy to copy all and trim the data down (plus it means moving large amounts of data unnecessarily).
2) The end goal of this is to save the sheet as a third, seperate, workbook. So if you know a way that helps this that doubles your awesomeness.
3)Targeting individual cells is not a viable solution, there's too much and the code would be huge/akward/cumbersome.
4) I've found a way to import all sheets into my operating/active workbook but cant figure out how to make it target a specific sheet (I'm missing osmehting simple I'm sure).
Test code snippet:
Dim Importsheet As Worksheet
Sheets.Add Type:= _
"C:\Users\haa\Documents\Personal\My Hours E2V-mk3.xlsm\"
Seems simple enough right?
5) Finally, I've seen some potential solution routes that uses "executeexcel4macro" (something I've never even seen before) but so far it only seems to target cell ranges not sheets.
That's all the key info. I really have dug around (and would have liked to crack this myself). Reminder that I'm looking for a way to do this without copying individual cells or opening the source sheet.
Thanks again campers!
4) I've found a way to import all sheets into my operating/active workbook but cant figure out how to make it target a specific sheet (I'm missing osmehting simple I'm sure). Test code snippet
Import all the sheets, then delete all but the one you want to keep.

Trouble with Copying VBA Code

I've been working on an independent project for a client of mine. They wanted to produce a button that, upon the user-click, it would open up a user-form and have a variety of macro-related options to choose from: a drop-down list, checkbox, option select button, etc.
I created a test formula and submitted it to the client; they enjoyed it thoroughly and decided to sent me a file to 'copy & paste' my original code within their excel file.
Problem is; because I'm a tad bit inexperienced with VBA I've run into a problem where once I click the button - the user form doesn't show up.
Below is a Dropbox link of the original file I created and it's original code; as well as the file that I am trying to copy.
Any help would be all welcome and appreciated.
Link to dropbox: https://www.dropbox.com/sh/l1t37lz8uritrua/AAAdWPGvw0GDZ6hW4SwmbBdRa?dl=0
OriginalProject.xlsm has a form named honor_roll_form which contains 100 lines of code.
CopyOfOriginal.xlsm has a form named UserForm1 which contains no useful code.
I do not believe there is any method of directly copying user forms from one workbook to another. Instead
Within VB Editor of OriginalProject.xlsm, select honor_roll_form.
Click File then Export File and save the form on your desktop or where ever you like.
You will now have two files on your desktop; one with an extension of frm and one with an extension of frx.
Within VB Editor of CopyOfOriginal.xlsm, click File then Import file.
Import honor_roll_form.frm
When I try clicking button "Honor Roll", I get "Method or data member not found" for project1Box. I will investigate after dinner (18:57 here) unless you tell me you already know why I am getting this error.
Extra comments in response to request from OP
It is late here but I have started looking down sub execute_button_Click within the second CopyOfOriginal.xlsm. I will comment on what I see even if it is not directly relevant to the non-execution of the macro.
If you open the VB Editor and look on the left you will see the Project Explorer. Near the top you will see:
Microsoft Excel Objects
Sheet1 (Sheet1)
I have always found this confusing. The first “Sheet1” is Excel’s Id for the worksheet and cannot be changed. The second “Sheet1” is the default name for the worksheet which can be changed. You can write Sheet1.Range("A1") or Worksheets("Sheet1").Range("A1"). That is: you can reference a worksheet by its Id or its name. You have named a variable of type Worksheet as Sheet1. Using Excel’s names as variable names can lead to bizarre errors so it is important to avoid doing anything like this.
It is better to always use meaningful names. At the moment, you know what Sheet1 means but if you come back to this macro in six or twelve months will you remember. I would use a variable as you have but I would name it WshtCis208 or WshtVBAProg or something similar.
Set ID = Range(Sheet1.Cells(2, 1), Sheet1.Cells(52, 1)) could be written as:
With WshtCis208
Set ID = Range(.Cells(2, 1), .Cells(52, 1))
End With
Using With statements produces faster code and, almost always, code that it easier to read.
“52” is the current bottom row for this table. Will you amend the macro for them every time they add or remove a student? There are several techniques for finding the last row, none of which is perfect in every situation. The technique that is the most convenient most of the time is:
Const ColCis208Id as Long = 1
Const ColCis208MidTermExam as Long = 5
Dim RowCis208Last as Long
RowCis208Last = .Cells(.Rows.Count, ColCis208Id).End(xlUp).Row
At the moment, column 1 is the Id column. It is perhaps unlikely that the Id column will move but it is very likely that some of the others columns will move when some new column is identified as useful. Do you want to scan the code trying to decide which 5s refer to the MidtermExam column when a Project3 column is added?
Constants allow you to name literals that might change. It makes your code easier to read and saves so much pain when a value changes.
.Rows.Count gives the number of rows in a worksheet for the current version of Excel so .Cells(.Rows.Count, ColCis208Id) identifies the bottom cell of column 1. End(xlUp).Row says go up until you hit a cell with a value and returns its row number. It is the VBA equivalent of Ctrl+Up.
The next statement subjectCount = … fails because projectBox does not exist on the form. You have changed the captions but not the names.
As far as I can see the form fails to execute because you have started updating it but have not finished.

All reference values in Excel sheet becomes '!#REF! when refering values from another sheet

One of our excel program stops working and I am trying to figure out why.
In the program, there are multiple sheets. The one cannot calculate value is called "Piston", and the reason is that all reference values from another sheet called "General Input" fail to refer.
The following are some failed cells in "Piston" sheets:
='General Input'!#REF!*(0.0063/0.0111)*EXP(-1*0.0214*$C$9)
='General Input'!#REF!*EXP(-1*0.0214*$C$9)
=AB5*(AD5+AC5*DG5)/(AD5+AB5*DG5)/'General Input'!#REF!
=$C$8*0.0000032/('General Input'!#REF!*Piston!$C$4*Piston!N10*(2*$C$6/1000)^2*('General Input'!$C$16/1000)^2)
=((BV9^3)*(BQ9^2)/DJ9/8+(BW9^3)*(BR9^2)/DK9/4+(BX9^3)*(BS9^2)/AE9/4+(BY9^3)*(BT9^2)/DL9/4+(BZ9^3)*(BU9^2)/DM9/8)/12/'General Input'!#REF!*$G$26/(1000000)^3
Apparantly all different values in "General Input" fail suddenly at the same time. I am not the one who wrote the code, and I don't use it as well. I am just asked to fix the bug.
But I just want to ask, is there any reason that suddenly make all referred value fail from a sheet?
Sorry if the information I provided is too vague.
!#REF! used to be a cell reference - e.g. $G$9
='General Input'!#REF!*(0.0063/0.0111)*EXP(-1*0.0214*$C$9)
However, such link/reference to G9 doesn't exist anymore - e.g. G9 was deleted.
The best hope is that you saved different version of such Excel. If yes, go back to an older version and copy those formula.
Good luck.

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal