Public Variable Producing Error

Feb 10, 2007

I want to define a varible named MonthEnd that I will use in more than one project. In a normal example the variable would look like this:

Dim MonthEnd As String
MonthEnd = Format(Sheet1.Range("C3"), "MMYY")

The problem is that I will be using this more than one time so I figured I could define this a Public constant like

Public Const MontEnd As String = Format(Sheet1.Range("C3"), "MMYY")

View 3 Replies


ADVERTISEMENT

Keep Getting Error With Public Variable

Jul 8, 2008

I have a number of userforms that all reference the same worksheet. I wanted to create a public variable for that worksheet so I don't have to keep referencing it in each userform/commandbuttons etc. So I inserted a module and placed this declaration.

Option Explicit
Public MySh As Worksheet
Set MySh = ActiveWorkbook.Sheets("sheetName")
And I referenced it in a UserForm commandbutton

Private Sub CommandButton2_Click()
MySh.Range("i4") = Me.ComboBox1
MySh.Range("i5") = Me.TextBox1
End Sub

View 9 Replies View Related

Loop That Keeps Producing Next Without For Error

Mar 24, 2014

I know it has something to do with the way I am trying to complete the loop.

I have tried next i, I have tried if's instead of the do until but can't get it to work.

I want it to cycle through the x and if a value is found then paste into the corresponding i row. If the x value is blank then exit the loop.

View 14 Replies View Related

Set Public Variable

Feb 12, 2008

Setting the Public Statement does not work. These are my codes:

Option Explicit
Public Ankis_makro As Boolean
Sub Makro2_Ankis_veckor()
On Error Resume Next
Application. ScreenUpdating = False
Sheets("Listan").Activate
Selection. AutoFilter Field:=1
Selection.AutoFilter Field:=2
Selection.AutoFilter Field:=3
Selection.AutoFilter Field:=4
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=6
Selection.AutoFilter Field:=7
Selection.AutoFilter Field:=8
Selection.AutoFilter Field:=9
Range(Range("J2"), Range("I65536").End(xlUp).Offset(0, 1)).FormulaR1C1 = "=TEXT(RC[-9],""ลลลล"")" ...................

What ever I do I can't get the Ankis_makro set to True.

View 2 Replies View Related

Macro Can't See Public Variable?

May 26, 2014

I have declared one variable outside of Sub. And in a Sub I gave it a value. Then I run a macro, which should take that value and print it to the cell. However, it is printing a blank cell, not even "0" (I run a macro and anything in that cell is removed).

[Code].....

and then my macro:

[Code]......

How to make so macro see my variable declared outside of this macro?

View 8 Replies View Related

Global Vs. Public Variable

Oct 28, 2009

explain the difference between declaring a variable as global or as public. Aren't they both available to the entire project including forms?

View 9 Replies View Related

Public Variable Not Being Retained

Feb 22, 2010

I've got several proccedures all inside a single module where I declare 7 variables as Public before the start of the first proccedure.

My structure is something like this:

Public v1 as Variant
Public v2 as Variant
.
.
.
Public v7 as Variant

Sub Proc1
Call Proc2 (defines v2 as name of a file opened by a user; inside Proc1)
Call Proc3 (inside Proc1)
Call Proc4 (inside Proc3)
Call Proc5 (inside Proc5)
End Sub

Sub Proc6
Here I use v2
call Proc7 (inside Proc6)
End Sub

Variable v2 is uniquely named and is only used to hold a file's name so that I can call upon that file in various procedures. However, after Proc1 ends, Proc 1 retains the variable names of some of my Public variables, but not all, including v2, which I need!

I've tried going through this all step-by-step, no joy/nothing apparently obvious. I've also turned on Tools-Options-General-Notify Before State Loss but this isn't generating anything either.

View 9 Replies View Related

Scope Of Public Variable

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

Public Variable Not Work

Feb 17, 2007

I read the Excel VBA Variables Scope and Lifetime info, but I still don't understand b/c of the statement "It's value is retained unless the Workbook closes or the End Statement is used." Which "End Statement"?

I want to fill an array in a form module, then later use those values in a different form module. If I declare the public variable in a standard module, I know any of the modules can access it, but will it retain its value after form1 is no longer running?

If not, do I need to involve a class somehow? I know nothing about classes except that their variables retain their values as long as any of the members is running.

View 9 Replies View Related

Userform Not Passing Through 1 Particular Public Variable?

Aug 20, 2014

I have a userform that is called within a sub in module. I declared a public string, "divisonb", in the module. When the userform's ok button is clicked, I define the public string through a "select case" method.

At the end of the private sub for the ok button click I have this:

[Code].....

a message box comes up with the correct string for divisonb. After the sub ends and it returns to the module I have the following:

[Code] ....

When this message box pops up, it is blank. Somehow, divisonb was redifined as blank within that 2 lines of code. All my other public strings are returned to the module with their correct values.

View 3 Replies View Related

Dynamic Array As Public Variable

Mar 6, 2010

How can I make a dynamic array public?
I have to Dim it so it stays valid only inside the sub.

Public MyVar()

Sub test1()

Dim MyVar()
ReDim MyVar(1 To 4)
For x = 1 To 4
MyVar(x) = "ffffff"
Next x
End Sub

Sub test2()

For x = 1 To 4
Range("A" & x) = MyVar(x)
Next x
End Sub
Error I get is 'MyVar(x) = Subscript out of range'

View 9 Replies View Related

Public Variable Declaration Not Being Recognized

Jul 27, 2007

Hopefully this isn't too vague, but almost every project I've worked on so far has not recognized Public variables in all modules. I've constantly been searching for exceptions to this rule that could be contributing to this issue but haven't been successful.

I have read a million times that declaring a variable as Public makes it visible to all modules and preserves the value throughout. I've also read that if you edit code or reset your project, the values may be lost. I also know that if you try to use a variable before you give it a value (or for object variables set it = to something), it doesn't have a value yet. But all this applied, I'm still not getting Public variables to be consistently recognized or stay with the value I want them to have throughout the lifetime of my project (while the workbook is open)

My specific current issue (one of many so far) is that I have 4 command buttons within a worksheet object. Each triggers a different group of procedures, some of these are user forms. In the first button, I create two worksheets one to contain the current fiscal period "Options" the other for "Summary" of all the totals etc... (all financial data). In this first userform, triggered by the button, I allow the user to name these worksheets: like Summer2007Options or Summer2007Summary and I declare these publically as object variables OptSheet and SumSheet. These variable names and sheets are recognized for the next 2 buttons but now that I'm on the fourth, I have object variable not set errors when I try to refer to this variable.

View 9 Replies View Related

Public Variable Loosing Reference

Mar 14, 2008

I've got the following code, setting up and initializing a Public variable for a sheet, but I've found that it's has no value after the Auto_Open is run,
though it works fine in the procedure Auto_Open calls.

I can get around it by reSetting it in the module I need it in,
but that kind of defeats the purpose of having a Public variable:

code in Module1

Public parSheet As Worksheet

Sub Auto_Open()

Set parSheet = Worksheets("Params")
Call Initialization.BuySellInit

End Sub

View 8 Replies View Related

Losing Public Variable When Submitting Userform

Jan 29, 2009

I have an Excel 07 spreadsheet containing multiple tabs, modules, and userforms.
In Module1, I have a public variable declared as boolean - will call it X. When X is selected from a combo box in Userform 1, X is set to TRUE. However I've noticed that when the user enters Userform 2, X is somehow set to FALSE.

I can't set it back to TRUE at that point because more often than not, it SHOULD be FALSE, thereby sending the macro down a different path. Any idea how I can retain the "TRUE" value for X. I've tried changing the Public Variables to Global, but am still having the problem.

View 2 Replies View Related

Workaround For Public Variable Bug: Creates Different Cell Colors Based Upon The Cell Contents

Oct 15, 2008

I am in the process of reformatting an excel workbook to act as a review tool for different factors in a process. Part of my redesign includes the use of coding that creates different cell colors based upon the cell contents. The new workbooks will be used to handle existing data for this year. I have developed a process macro to open an existing workbook and copy and transfer the original data worksheet into the newly formatted workbook. The data gets transferred to the new worksheet and the resulting workbook is renamed and saved, Heres'' the rub... the newly saved workbook is missing all of the coding for the worksheets... apparently this is a MS bug.

Has anyone figured a workaround for this. One thought I had is to open both( new and old )workbooks and rather than move/copy , i would transfer the data using cell references.

View 5 Replies View Related

Call Public Object From Public Sub?

Aug 14, 2012

I am trying to bring up a form after clicking the "ok" button on another form. Both forms are defined in my public sub, so basically, the module creates both forms, calls the first form, and hides the second form. Then the first form hides itself and shows the second form. However, when second form is defined using "dim", the first form can't find it to show it, and when I make the second form "public" in my public sub, I get the error that it is an invalid attribute or function.

Here is the code from my sub that applies to this error:

Code:
Public Sub PutInEngine()
Dim InputForm As New FrmInputs
InputForm.Show

[Code].....

View 3 Replies View Related

"wrong Data Type Error" Public Lookup Function

May 4, 2007

I have attached a file called Test2 which has a public function called DBLookup. The function looks in the attached access database simulating a Vlookup just like in Excel. The problem I am having is when that data appears on sheet1. I am getting #VALUE in the cell and the erroe states "a used value in the formula is of the wrong data type".

my

Dim adoCN As ADODB.Connection
Dim strSQL As String

Const DatabasePath As String = "C:Test2.mdb"

'Function argument descriptions
'LookupFieldName - the field you wish to search
'LookupValue - the value in LookupFieldName you're searching for
'ReturnField - the matching field containing the value you wish to return...

View 5 Replies View Related

2007 Macro: Run-time Error 91:Object Variable Or With Block Variable Not Set

Feb 20, 2009

I'm fairly new to macro's and VBA, by searching on the internet i've copied and pasted some code together into a macro.
But it ends in a Run-time error 91...

The macro opens a target .xls file in a selected folder, performs copy - paste actions from masterfile to targetfile.
Than it filters data in the targetfile sheet1 and copy's the results to the various other sheets; saves and closes the targetfile.
The next target file in the folder is opened and the actions are repeated in this second target file.
For the first target file this works smoothly; but for the second one (of a total of around 100) it does not copy the filter results to the other sheets in this workbook.
The error message i get is: "Run-time error 91:Object variable or with block variable not set."
When i hit debug it highlights the line "ActiveSheet.Next.Select" which, at least in the first file, seems ok.

View 9 Replies View Related

Excel 2007 :: Object Variable Or With Block Variable Not Set Error

Feb 28, 2013

I Wrote a code which as intended to open each excel file in a folder and copy the data containing in it into a new sheet.

But While running the code the first excel file gets open, and an error message "Run Time Error 91-Object Variable Or With block Variable not set Error"

How to set the file which got opened from the folder to wbk variable.

Code:
Sub dataintoonesheet()

Dim i As Integer
Dim jk As Integer
Dim j As Integer

Dim rowstart As Integer
rowstart = 3

[Code] .......

View 9 Replies View Related

Macro Error Message (object Variable Or With Block Variable Not Set)

Jul 7, 2009

When i try to run the code below i get the error message - object variable or with block variable not set-

Sub REFRESHXX()

'LIST
Cells(Sheets("POINTS").Range("DD801").Value, Sheets("POINTS").Range("DD800").Value).Select
Selection.AutoFilter Field:=1, Criteria1:="1"
'SET RANGE
Dim sFormula1 As String
Dim sFormula2 As String
Dim sCell1 As String
Dim sCell2 As String
Dim sSheet1 As String
Dim sSheet2 As String
Dim r As Range
Dim MyRange As Range 'for testing

With Sheets("Points")
sFormula1 = .Range("CY1").Formula
sFormula2 = .Range("CY2").Formula
End With

'FORMULA IN R1C1 STYLE
strFormula = "=IF(ISNA(VLOOKUP(RC[-1],MASTER!R4C3:R17908C7,3,FALSE)),0,VLOOKUP(RC[-1],MASTER!R4C3:R17908C7,3,FALSE))"
'ENTER FORMULA IN ALL CELL RANGES
r.FormulaR1C1 = strFormula
'REDUCE TO VALUES
Dim ar As Range 'an area is a range
For Each ar In r.Areas 'areas are discrete, contiguous ranges of cells
ar.Value = ar.Value
Next ar

'UNLIST
Cells(Sheets("POINTS").Range("DD801").Value, Sheets("POINTS").Range("DD800").Value).Select
Selection.AutoFilter Field:=1

End Sub

View 9 Replies View Related

Runtime Error 91 Object Variable Or With Block Variable Not Set

Mar 24, 2014

I am getting error in Set MyRange

[Code] .....

View 3 Replies View Related

Object Variable Or With Block Variable Not Set Error With Macro

Mar 12, 2009

I attempted to modify "macro_1a1ay" to look into the "comments" sheet (column a) for a specific text string. If that text string is found, I have it delete the entire row, then re-sort the page and return to the calling page. It works well as long as it finds something. When it does not find the string (i.e. like now when the page is blank) it gives me the run-time error mentioned above.

View 7 Replies View Related

Run Time Error 91 Object Variable Or With Block Variable Not Set

Oct 7, 2009

I am having a lot of trouble finding out why I am getting error. I believe the error is because it can't find the number. In cells C115:C314 i have the numbers 1 to 200 in order. when someone types in 1 to 9 in the text box it works, but on 10 and over i get the error ???? here is the code I have

shCalculator.Range("C115:C314").Select
Selection.Find(what:=CInt(txtPackageID), After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Select
x = ActiveCell.Row
shCalculator.Range("ProposedMeter").Value = Cells(x, 7).Value
shCalculator.Range("Package").Value = Cells(x, 12).Value
shCalculator.Range("ProposedMeterAmount").Value = Cells(x, 30).Value
shCalculator.Range("Term").Value = Cells(x, 62).Value
shCalculator.Range("Discount").Value = Cells(x, 67).Value
shCalculator.Range("Equipment").Value = Cells(x, 72).Value

View 3 Replies View Related

Two Macros - Object Variable Or With Block Variable Not Set Error

Jul 11, 2014

I have two macros that run fine separately. I want to join them, but when I do I get an Object variable or With block variable not set error.

Here's the code

Code:

Sub border_highlight_insert()
' Change heavy border to new column, change tint - keyed to year A6
With ActiveSheet
Dim i As Integer

[Code]....

View 2 Replies View Related

Runtime Error 91, Object Variable Or With Block Variable Not Set

Feb 6, 2009

This works fine in Excel 07 but when ran in 03 it doesn't work and I get that error message.

Sub mcrRefresh2()
Sheets("WeeklyData").Range("A1").ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets("WeeklyData").Range("aa1").ListObject.QueryTable.Refresh BackgroundQuery:=False
Worksheets("WeeklyPivot").PivotTables("PivotTable1").PivotCache.Refresh

End Sub

View 9 Replies View Related

Debugging Error: Object Variable Blocked Variable Not Set

Nov 9, 2009

I am getting an error at this line:

Set rng5 = Range(.Cells(celle2.Row, celle4.Column), .Cells(celle2.Row, celle4.Column))

Sub UpdateNumbers()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim celle1 As Range
Dim celle2 As Range
Dim celle3 As Range
Dim celle4 As Range
Dim celle5 As Range
Dim flag1 As Long
Dim flag2 As Long................

View 9 Replies View Related

Run Time Error 91, Object Variable Or With Block Variable Not Set

Jan 14, 2007

Function Find_Range(Find_Item As Variant, _
Search_Range As Range, _
Optional LookIn As Variant, _
Optional LookAt As Variant, _
Optional MatchCase As Boolean) As Range
Dim c As Range
If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
If IsMissing(MatchCase) Then MatchCase = False
With Search_Range
Set c = .Find( _
What:=Find_Item, _
LookIn:=LookIn, _
LookAt:=LookAt, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=MatchCase, _ ........................

Using the message box I see that sheet 1 opens but then I receive an error message Run Time Error 91, Object Variable or With Block Variable not set. I tried declaring and using set on "project" but got nowhere. I also need to have a message indicating project not found. Once this part is solved I will loop all of my other workbooks

View 5 Replies View Related

Copy And Paste Special Values But Getting Error 'Compile Error - Expected Function Or Variable'

Feb 7, 2007

I am trying to run create a simple macro that copies and paste special values - something I have done 100's of times but for some reason I keep getting an error message - even though I recorded the macro and didnt write it by hand - see below:

Sub Macro6()
Cells.Select
selection.Copy
selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

For which I get 'Compile Error - Expected Function or Variable'

View 6 Replies View Related

Error - Object Variable Or With Block Variable Not Set

Jun 20, 2007

I've got a simple Macro which manipulates data in various worksheets. Problem is that I get the error: "object variable or with block variable not set" in the area of the code that I have put in bold below:

All I'm trying to do in this part is find the first occurrence of the #n/a value ....

View 12 Replies View Related

Error: Object Variable Or With Block Variable Not Set

Mar 3, 2009

I've set up a userform (frmCoC) to read and write info to the "contractor database". Within that userform is a listview control that reads off a second, "induction database".

When the user selects and clicks on one of the displayed entry in listview, another form (frmInd) opens to edit data on the induction database.

There's a command button along side the original form (frmCoC) to create a new entry in the induction database. The click event for the button is as follows:

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved