Declare Dim as Integer and Boolean or Long

Nayasoch

Board Regular
Joined
Sep 9, 2016
Messages
73
I have this code which doesn't work and when I complie and run It shows Error Variable not defined and highlights the CurrentChart or ChartNum.
As I am aware Dim var1, var2 can be declared but in my case it is not working if I remove

Code:
 Dim ret As Long
    Dim formHWnd As Long

It works again perfectly .

My Code which shows errors of variables not defined.
Code:
Option Explicit
Dim ChartNum As Integer




Private Sub UserForm_Initialize()


ChartNum = 1
    UpdateChart
 
  Const C_VBA6_USERFORM_CLASSNAME = "ThunderDFrame"
    
    Dim ret As Long
    Dim formHWnd As Long
    
    
    
    'Get window handle of the userform
    
    formHWnd = FindWindow(C_VBA6_USERFORM_CLASSNAME, Me.Caption)
    If formHWnd = 0 Then
        Debug.Print Err.LastDllError
    End If


    'Set userform window to 'always on top'
    
    ret = SetWindowPos(formHWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    If ret = 0 Then
        Debug.Print Err.LastDllError
    End If
    
End Sub




Private Sub UpdateChart()


    Set CurrentChart = Sheets("Graph1").ChartObjects(ChartNum).Chart
    CurrentChart.Parent.Width = 380
    CurrentChart.Parent.Height = 260


'   Save chart as GIF
    Fname = ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
    CurrentChart.Export FileName:=Fname, FilterName:="GIF"


'   Show the chart
    Image1.Picture = LoadPicture(Fname)
End Sub

Any help would be rightly appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Because you have used Option Explicit above your procedures, you must dimension all variables in those procedures. It looks like you have not dimensioned the variable CurrentChart in the last procedure.
 
Upvote 0
Well That worked, I don't know why but as I removed Option explicit ....It worked as smoothly as butter. Thanks JoeMo.
 
Upvote 0
So what might be the solution , should i remove option Explicit??
No! Do not remove it... you should always use it, especially if you are going to do any VBA coding where the results matter. Let me give you an example of the problem if Option Explicit is not declared.
Rich (BB code):
Sub Par()
  Collections = 1000
  If SomethingImportant = True Then Col1ections = Collections * 2
  '
  '  more code here that use the value in Collections
  '
End Sub
It is somewhat easy to see the problem given the font used by this forum (I "accidentally" typed a one instead of the lower case letter L), but copy/paste the above code into a VBA module and see if the problem is as obvious to spot there. Now, the problem is that without Option Explicit, the calculation after the Then statement will take place without error, but the Collections variable will not be updated if SomethingImportant is True and every calculation after this point in the code will be wrong, but you will not know that because no error occurred. With Option Explicit, on the other hand, VB will stop your code and tell you Col1ection has not been declared. You will look at it and know it must be spelled wrong because you know you declared Collection already. This will help guide you to finding the spelling error allowing you to fix the problem. This is a simple example... 0 (zero) and the letter O also look a lot alike. And, of course, you could simply make a plain typing error somewhere in your code... it may be easy to spot the typo if you knew it was there, but if no error is generated because Option Explicit was not used, you would not know to look for it. Bottom line... always use Option Explicit.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top