Object Variables That Don't Go Out Of Scope?
Oct 4, 2013
How to prevent a variable holding a pointer to an object from going out of scope and releasing the object pointer after the VB project is reset due to a runtime error during code execution or due to manually resetting the vb project ?
Hooking the Application events in a class module is a scenario where this could be particularly useful - For example, let's assume the application events are hooked upon opening the workbook . Now if the variables go out of scope the application events are no longer functioning which is what i am trying to avoid.
I did once worked around this by creating a VBS script on the fly and continually monitoring the state of the variable from outside excel but this approach has proven problematic on some machines where running scripts is restricted.
I thought about writing an activeX dll in VB6 , loading it in the excel process and hook the application events from there but I don't think this would work because the dll shares the same memory space as the host process.
View 2 Replies
ADVERTISEMENT
Dec 7, 2006
How important is it? I've read some books that recommend it and others that don't even mention it. I have a procedure that uses a lot range, worksheet and workbook variables. Should take the time to set each one to "Nothing"?
View 3 Replies
View Related
May 21, 2007
In a sub with object variables ( ranges, etc) not declared as Static, what is the point of setting them to Nothing prior to completion? When the subroutine terminates and the variables go out of scope, do they not implicitly release memory?
View 7 Replies
View Related
Jan 28, 2013
If I declare a variable i to be an integer and then initialise it say i=10, I can see its value in the locals window.
However, if I define myrng as range and set myrange = Cells(1,1), looking at the locals window for myrng brings up a whole wealth of information.
View 6 Replies
View Related
Jun 18, 2006
Just starting with excell and am working on a project for my class. I cannot figure out why the value of the variable "changer" defined by command buttons on sheet1 are not being passed to the procedure contained in command button on sheet 2.
I declared the variable in the general declarations section of sheet1 with
Public changer As Integer
The command buttons on sheet 1 contain
Private Sub CommandButton1_Click()
changer = 1
Cells(1, 1) = changer
End Sub
Private Sub CommandButton2_Click()
changer = 2
Cells(1, 1) = changer
End Sub
The command button on sheet2 contains
Private Sub CommandButton1_Click()
Cells(1, 1) = changer
End Sub
When I click on the command button on sheet2 nothing happens.
View 3 Replies
View Related
Jan 19, 2007
What does the term variable scope mean?
View 3 Replies
View Related
Feb 10, 2007
I have a few lines of code as follows:
Private Sub Workbook_Open()
fileName = ThisWorkbook. Name
fileLocation = ThisWorkbook.Path
Dim strFound As Boolean, pos As Long
If Not fileName = "" Then
strFound = False
pos = 1
Do While strFound = False
stringFound = Mid(fileName, pos, 1)
If stringFound = "-" Then
productName = Mid(fileName, 1, pos - 1)
MsgBox "Name of Product Is: " & productName
strFound = True
Else
pos = pos + 1
End If
Loop
End If
End Sub
It would return "Test" if the file's name is "Test-Part1.xls". I then declare the productName variable as Public (Public productName As String) in one of the modules. But the productName only holds the value for a certain amount of time. After a while, it's empty.
View 4 Replies
View Related
Apr 13, 2007
I am trying to create a primitive counter. My idea is to write a value to a file, read it, add one and write it back. The file as a single value tab-delimted of 5000. The code below produces the odd output on debug. I really cant understand it.
Public Function nextInv() As Integer
Dim fname As String
Dim fnum As Integer
Dim stringArray()
Dim invNum As Integer
fnum = FreeFile()
fname = "nextInv.txt"
stringArray() = parse(OpenRecordClassesFn(fname), vbTab)
For i = UBound(stringArray) + 1 To LBound(stringArray)
invNum = stringArray(i)
Next
If FileExists(fname) Then
Open fname For Output As #fnum
End If
invNum = invNum + 1
Debug.Print "invNum"
Print #fnum, (invNum & vbTab)
Close #fnum
invNum = invNum + 1
End Function
Public Sub testHarness()
Debug.Print nextInv()
End Sub
with a output of
5000
invNum
0
and the value in the file of 1. It should be 5001.
View 9 Replies
View Related
Mar 30, 2014
I'm trying to find definitions/commands to make excel find the best combinations of several defined scopes and restrictions to make an output number as high as possible.
This is by using a combination of several different equations, maybe excel has a magic trick for all the variables and formulas.
Attached File: forum_heatexchanger.xlsx‎
View 2 Replies
View Related
Jan 5, 2009
It's some code I found and altered that automatically updates the cell to the right of a column that has drop down box data validation, creating a list of the selected values seperated by commas. The thing is, I only want it to do this in columns 8 and 10 but data validation drop-down boxes have been used in several other columns. I've tried various methods of limiting the scope, the latest being the line: If Target.Column = 8 Or 10 Then. But the auto update is still being applied to all drop down boxes in the sheet. So, how can I make sure the auto update only happens when the drop down boxes in columns 8 and 10, updating into columns 9 and 11.
View 4 Replies
View Related
Jan 27, 2008
about variable lifetime/scope in a workbook. Basically I have a Class Module that I set up in the Workbook_Open() event as follows:
Private Sub Workbook_Open()
Set CurrentClient = New CClient
End Sub
Once the workbook is open the user can select their "client" and all properties/methods for that "client" are available throughout the workbook until they close it, and it all works fine. However, whilst developing I'm obviously making lots of changes to the code, and every time I do the object is no longer available in memory.
I've tried manually running the Workbook_Open event after I make a code change, and even put in a button with the same code, but whatever I do the object won't remain in memory until I close & reopen the workbook. It's so frustrating having to close & reopen the whole workbook every time I want to test a code change I've made!
View 9 Replies
View Related
Oct 19, 2007
I have two numbers I need to compare using and IF statement. The two numbers are virtually the same, and I want the IF statement to round the numbers so any difference under 100,000 is not detected. For example:
Cell A1 = $32,194,884.45
Cell B1 = $32,194,884.37
I would like the statement to look at this number as 32.19 (divided by 1M). I am using the below equation currently, but it is not working:
=IF(ROUND(A1/1000000,1)<>ROUND(B1/1000000,1),"CHECK","0")
View 7 Replies
View Related
Jun 28, 2009
I get an error: Duplicate declaration in current scope? The formula calculates the compound annual growth rate.
View 5 Replies
View Related
Nov 19, 2009
I have a spreadsheet and I’m trying to create several different staffing scenarios
Basically what I want is to populate column E with the amount of staffing based on the number of classes in column B. The range that I want to apply is in the green box in the sample. I’m trying to have excel give me the staffing value if the current number in cell B5 is a certain value – so in the example 1 through 4 = .2, 5 through 8 = .4 and 9 through 12 = .6. this site gave me some ideas so I tried V lookup and Index but I seem to be missing something and wonder if it is not combinations of nested functions?
View 3 Replies
View Related
May 27, 2008
I'm trying to do some sort of a league table
I have something like this with data in around 1000rows there is text in collums A B D and E.
A B C D E
1
2 100
3 60
4 30
5 20
and i would like to do this:
if value in cell x in collum C is over 80 then background color in Ax,Bx,Cx,Dx,Ex is yellow
if value in cell x in collum C is between 60-80 then background color in Ax,Bx,Cx,Dx,Ex is brown
if value in cell x in collum C is between 40-60 then background color in Ax,Bx,Cx,Dx,Ex is blue
if value in cell x in collum C is between 0-40 then background color in Ax,Bx,Cx,Dx,Ex is red
Obviously i cant use conditional formating because i have more then 3 conditions.
Does anybody know what code to use for VBA?
View 6 Replies
View Related
Feb 5, 2009
I have a worksheet with 3 particular columns (L,M,N) that have drop down menus. The menu is populated with items that are referenced on a different workbook (different file). Because there are multiple worksheets accessing the referenced list, this is a master. When I change the master, the lists update, but the cell text does not. Example:
If I chose apple from the list, but later in the master I decide I want apple to be banana, while the drop dowm box will now have banana instead of apple, my cell still says apple.
I don't necessarily want apple to change to banana, but I want a visual cue that this is incorrect. I wanted the cell text to change to red if it is not equal to any of the list items on the master file. I want this to be passive, i.e., I want it to change automatically, so I don't have to press a button or some other activation.
View 12 Replies
View Related
Feb 17, 2012
I have the following code which performs the same action on 2 different tabs within the same workbook:
Dim Rng As Range, Dn As Range
Set Rng = Range(Range("O1"), Range("O" & Rows.Count).End(xlUp))
Rng.NumberFormat = "@"
For Each Dn In Rng
If Len(Dn) < 7 Then Dn = String(7 - Len(Dn), "0") & Dn
Next Dn
i get the following error:
Compile Error: Duplicate declaration in current scope
View 5 Replies
View Related
Oct 27, 2011
I am trying to automate the creation of 3 pivot tables. At first I was having a problem with deselecting all items in the drop-down except one, then I found the code to fix it.
I replaced this code:
ActiveSheet.PivotTables("PivotTable15").PivotFields( _
"Beta")
.PivotItems("Escalated").Visible = False
.PivotItems("Hang Up").Visible = False
.PivotItems("New Hire Requests").Visible = False
.PivotItems("No Trouble Found").Visible = False
.PivotItems("Priority Exchange").Visible = False
[code].....
This fixed the problem, but there are still two other pivot tables that need to be created after this first one. I scrolled down through the code and deleted the other code blocks for the selection of the PivotItems and replaced it with the code above, but then when I ran it I got the "Compile Error: Duplicate Declaration in current scope".
I read more through the forum and realize that it's because VBA is dimensioning it twice, and I read that you're supposed to Dimension at the beginning of your program, but how do I implement this into my coding?
I can't dimension literally at the start of the code, there is some formatting that needs to take place first. (Basically I paste in a bunch of data into Sheet1 of my workbook, center it, space it out, then insert a pivot table into the pre-existing Sheet2 based on that data, filtered on the blank entries in Column N. Then I need to create ANOTHER pivot table based on that same data in Sheet1, but filtered on the blank entries in Column O. Then I center the words in the Pivot tables' headers in Sheet2, and finally I need to create one last pivot table on pre-existing Sheet3 based on the data on Sheet1, filtered for 2 specific entry types in Column N.)
View 2 Replies
View Related
Feb 5, 2009
Unzip Code - Works without Variables, Breaks with Variables.... This has been driving me bananas...
I have the
View 2 Replies
View Related
Jan 22, 2008
I have the following code to compare two columns and delete adjacent rows if 1 is greater than or equal the other...
Sub LastReceipt_GT_Confirmed()
Dim intLstRow As Integer
For intLstRow = Range("E" & Rows.Count).End(xlUp).Row To 2 Step -1
With Range("E" & intLstRow)
If .Value > .Offset(0, 1).Value Then .EntireRow.Delete
End With
Next intLstRow
For intLstRow = Range("E" & Rows.Count).End(xlUp).Row To 2 Step -1 .............
View 9 Replies
View Related
Jan 17, 2005
I'm getting the following error:
"Object library invalid or contains references to object definitions that could not be found"
I wasn't getting that error last night and I'm not sure what I may have done to cause this error.
It seems to be cause by code running on one sheet of my workbook, but I'm not really sure about that. I'm still a bit of a novice at VBA.
I'm using Excel 2002 SP3 and I'm running MS XP Home as my OS.
Do you have any ideas what can cause this error and/or how to trace down the offending objects/code?
View 9 Replies
View Related
Feb 6, 2008
I am comparing a set of common variables across 2 sets of independent financial data. When comparing a variable across the 2 sets e.g. profit I want to colour a neighbouring cell based on the size of the difference. For example if the difference between the two values is within plus or minus 1 make it green, plus or minus 5 red etc.
View 5 Replies
View Related
Jun 19, 2008
I'm having a problem with data validation. I set an entire column so that it could only be a date between the first date in the list and the current date. I tested a few cells to make sure that it was working the way I wanted and I noticed that it allows a random number like 3 or 5 to be input after row 50. This would create a date in the 1900s. Why is this happening and how can I stop it.
View 9 Replies
View Related
Jul 27, 2006
Can a Function give two or more output variables. e.g.
Sub a()
x = 5
result = Y(x)
End Sub
Function Y (x As Integer) As Integer
Dim B
B = ... * x
Y = ... * B
this will give back Y as a result. But if I want to get 2 or more output variables (let's say I need to get also B into sub) from one function, how should I do that?
I need this because function works with large matrix and I want to extract some values appeared in between.
View 2 Replies
View Related
Apr 27, 2006
I'm trying to loop through a range in excel from access, checking where the titles (in Excel row 1) match with the fields (in a recordset in Access that is passed to the function) - and where they do, I want to dimension a variable to hold the column number - I'm not sure it's possible, but I'd be interested to know either way. The line I'm asking about is at the bottom of the code - the rest of the code is just to give context...
Sub ImportGeneric(rsImported As ADODB.Recordset, rsConfirmed As ADODB.Recordset)
Dim fd As FileDialog
Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Worksheet
Dim iFilePicked As Integer
Dim strFilePath As String
fd.Filters.clear
fd.Filters.Add "Excel files", "*.xls"
fd.ButtonName = "Select"
iFilePicked = fd.Show
If iFilePicked = -1 Then
strFilePath = fd.SelectedItems(1)
Else ..................
View 3 Replies
View Related
Mar 21, 2007
1) i have office 2003 on a laptop. within powerpoint, i can create a 'microsoft excel chart 11' object. to create a link to the excel data source, do i have to go through the odbc sql setup? it works, but i don't want my powerpoint to be dependent on some excel file somewhere. what are the other options to insert/make a functional pivot chart in powerpoint with the data also within powerpoint? the data as sheet option does not result in the chart being a pivot, it's just a plain chart. it has to be a proper object, not an image paste or a chart that updates links with the excel file open.
2) i have office 2007 on my other laptop. i can not find any suitable object to choose from to make a pivot chart in powerpoint. what's the best way to go about in 2007 version?
3) am i going about this the wrong way with the objects? should i be after vba code?
View 4 Replies
View Related
Jan 16, 2007
i have a "problem" to empty / reset my variables. I defined them as vHour1_KW2 where the "1" is from 1 to 21 and the "2" starts from 1 to 53. Now I want to erase all of this variables or to set the value of them to "0".
At moment I use following
vHour1_KW1 = 0
vHour1_KW2 = 0
...
vHour1_KW53 = 0
vHour2_KW1 = 0
vHour2_KW2 = 0
...
vHour2_KW53 = 0
until...............................
View 3 Replies
View Related
May 13, 2009
I am having difficulty getting a form to work the way that I would like it to work. I have a form that is used to display questions that my students will be answering. The form also is used to put the answers into a worksheet. I have 2 sheets. Sheet2 has the questions, student answers, and correct answers. Sheet1 is used to indicate correct answers and to keep track of percentage correct. I am fairly new to VB. I have 2 pieces of code that I am going to post. The first one works and the second one doesn't.
This is in the "This Workbook" section and it works.
Option Explicit
Public intNoQ As Integer
Public strNoQ As String
Public NumberofQuestions As Integer
Dim StudentName As String
Dim InputBoxAnswer As String
Public Sub Workbook_Open()
Application.Visible = False 'Hide Excel
Load Questions
Load NumberCorrect
NumberofQuestions = Worksheets("Sheet1").Range("K3").Value
For intNoQ = 1 To NumberofQuestions
strNoQ = VBA.CStr(intNoQ)
If intNoQ = 1 Then
Questions.Controls("QuestionNumberBox").Value = "Question#" & strNoQ
Questions.Controls("QuestionBox").Value = Worksheets("Sheet2").Range("B1").Value
End If
Questions.Controls("CorrectBox" & strNoQ).Visible = True
Questions.Controls("CorrectLabel" & strNoQ).Visible = True
Next intNoQ...............
View 9 Replies
View Related
Apr 9, 2014
I'm having a hard time understanding how to accomplish what seems to be a simple result.
I need to display one of two words, based on whether or not a pair of values are above or below the criteria.
FIRST:
IF H6 is greater than 5000
AND
IF AB6 is greater than 25000
Display: Double
SECOND:
IF H6 is less than 5000
AND
IF AB6 is greater than 25000
Display: Single
There is no 3rd scenario, even though logically there should be.
View 13 Replies
View Related
Apr 1, 2008
I am trying to put variables in this URL which is related to yahoo finance :
.Name="hp?s=NVDA&a=00&b=31&c=2001&d=11&e=29&f=2006&g=m&y=0"
I defined at the beginning
Dim start_date As Date
Dim end_date As Date
Dim datestring As Variant
start_date = #1/31/2001#
end_date = #11/26/2006#
and put them in datestring
I passed the datestring to a new sub which has the URL:
.Name="hp?s=NVDA&a=00&b=31&c=2001&d=11&e=29&f=2006&g=m&y=0"
So, my question is, i tried to put the (1/31/2001) and (26/11/2007) which is in the above URL which is separated in variables and the URL remain the same
View 11 Replies
View Related