I have found many blocks of code that containVBA.Date, for example;
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Then Exit Sub
Target.Offset(0, 1).Value = Format(VBA.Date, “MM/DD/YYYY”)
End Sub
Or
Sub YearSheets()
Dim i As Integer
i = 0
Do
Sheets.Add(After:=Sheets(Sheets.Count)).Name = Format(VBA.Date + i, “MM-DD-YYYY”)
i = i + 1
Loop Until i = 365
End Sub
I have tried these steps:
I have pushed F1 for VBA's help but it shows "Keyword Not Found".
Searched my VBA books but no one shows something because they take such a piece of information as unimportant or too easy (I guess).
I have tried to Google it --> You can imagine what happens with a combination of such common words.
I have pushed F2 in the VBE Window and opened the Object Browser
i. I searched for VBA and I got the library ... not very bright
ii. I searched for the Date Property and I found it to be preceded by the DateTime Module and going more in seniority from the VBA Library.
Question-born-from-question: Is it possible to be the VBA library here VBA.Date?
Frustrating-thing-that-happens: Once I type VBE IntelliSense shows Date Property, so it's hard-coded somewhere in my machine.
What really is VBA.Date - an object, a library that can be entered as as object?
Date() and Time() are Properties of the DateTime Class. Found in Excel VBA. They are of DataType Variant that contain the current system date when Date is used and current system time when Time is used.
They do not need to be preceeded by the VBA or the DateTime in order to be used they can simply be used with the terms Date, and Time.
Date, and if the calendar is Gregorian, Date$ behavior is unchanged by the Calendar property setting. If the calendar is Hijri, Date$ returns a 10-character string of the form mm-dd-yyyy, where mm (01-12), dd (01-30) and yyyy (1400-1523) are the Hijri month, day and year. The equivalent Gregorian range is Jan 1, 1980 through Dec 31, 2099.
In the future the best way to get information on specific function, properties, methods, classes and other members of the VBA Language you can use the Object Browser.
Marked one is the object browser and when clicked on will open this window (it can also be accessed with pressing F2) In this window enter what you are searching for
One you get results scroll through and look for a more specific item:
Now there are 2 points in the above image:
1) At the bottom of the screen it gives basic detail about this item, in this case
it states that it belongs to the `DateTime` Class, and is a property of that
class, and that it is a Variant.
2) Also in the picture above I have right clicked on the item and selected Help from
the context menu, this will bring up even more details about this item.
As you can see here Microsoft has built in support for this item and gives you details on what it is, what data type it would use and what it returns. Also, how to use and some common notes for when you use it!
VBA.Date and VBA.Time are functions that return the current date and the current time, respectively.
In your editor Tools / References you'll see 'Visual Basic For Applications' is selected. I have absoutely no proof but my idle conjecture is that this is where your VBA.Date (and VBA, everything) lies. Furthermore your F2 investigations show you the truth - It's in the VBA module there. Note you can use VBA.Date or just Date and they are the same thing.
In short it's hard coded in that DLL file referred to in Tools/References.
Related
I know this is probably simple, but I have spent 2 hours Googling for the answer and I can't find it. I am using the Office Web Components 2003 (v11) Spreadsheet Control on an Excel 2007 userform. I am using the BeforeContextMenu event to create a custom context (right-click) menu. I'm using the code sample from the Spreadsheet Control's help file to create the menu. The code is:
Sub Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel)
Dim cmContextMenu(4)
Dim cmClearSubMenu(2)
cmClearSubMenu(0) = Array("&All", "ClearAll")
cmClearSubMenu(1) = Array("&Formats", "ClearFormats")
cmClearSubMenu(2) = Array("&Values", "ClearValues")
cmContextMenu(0) = Array("Cu&t", "owc2")
cmContextMenu(1) = Array("&Copy", "owc3")
cmContextMenu(2) = Array("&Paste", "owc4")
cmContextMenu(3) = Empty
cmContextMenu(4) = Array("Clea&r", cmClearSubMenu)
Menu.Value = cmContextMenu
End Sub
The custom menu appears perfectly when the spreadsheet is right-clicked. What I can't figure out is how to capture the menu item that I select. Can someone please point me to VBA code that will enable me to capture and use the selection?
Thanks In Advance -
After more Googling I found the answer to this problem in the Help for the OCW11 Spreadsheet Control. The BeforeContextMenu event lists the code shown in my original post. I could see that the menu items were defined with statements like:
cmContextMenu(0) = Array("Cu&t", "owc2")
The "Cu&t" is the menu item label, and the "owc2" is the constant (I assume) that is supposed to execute the Cut command (commands are known as "OCCommands" in OWC11). The problem is that the "owc2" constant wasn't firing the Cut command. I checked the "Programming Information" section of the Help, underneath "Enumerations," in the entry titled "OCCommandID" for the OCCommand ID constants. That's exactly where they are located. I looked up the Cut constant, which is 1001, and replaced the "owc2" with 1001, and the Cut command worked perfectly! I did the same thing for the "&Copy" and "&Paste" commands, with 1002 and 1003, respectively, and they worked as well.
My problem may be that I didn't set up the proper references to the OWC constants. Or it could be that the constants changed and weren't updated correctly in the Help documentation. Whichever may be the culprit, the above method fixed the problem.
Is there a way i can check if a US number is a cell phone or landline?
I have found several sites online, but they are paid per number. Can i use code to do the check?
Thanks
This is less of an Excel question and more of a general question about phone numbers. If there's some way (I don't know of one) that you can freely check a phone number against some registry, then we could use some code to do this in Excel - sure. However, I'm not sure how to check whether a line is mobile or land line.
I did find this SO thread, but it looks like the free options are limited. I did find this site just Googling around, but I have no idea how to check if that's accurate or not. There's this site which also charges, but could help you.
Again, if you (or someone) does find a way to check phone numbers, you could probably automate it with VB, but getting to that point is the tricky part.
(Note: the webpages I linked above I just found Googling around).
I have worked on an admittedly crude way to do what you've requested, using the free service phonevalidator.com. I cannot attest to the accuracy of this service, but I tried for several cell numbers I know and several landlines as well and it was accurate for them.
This option uses VBA to go to the website, automatically enter a number, and scrape the phone type off of the results page. I have made the scraping a function (probably could be optimized better, but it works).
In a standard code module, start with this function:
Public Function GetPhoneNumType(phoneNum As String) As String
Dim ie As Object
Dim objResult As Object
'Initialize IE
Set ie = CreateObject("InternetExplorer.Application")
'Go to the phone validator
ie.Navigate "http://www.phonevalidator.com/index.aspx"
'Wait for the page to load
Do While ie.ReadyState <> 4
DoEvents
Loop
'Enter the phone number
ie.document.getElementByID("ContentPlaceHolder1_txtPhone").Value = phoneNum
'Click the search button
ie.document.getElementByID("ContentPlaceHolder1_SearchButton").Click
'Wait until javascript executes and displays the PhoneTypeLabel
On Error Resume Next
Do Until Not objResult Is Nothing
Set objResult = ie.document.getElementByID("ContentPlaceHolder1_PhoneTypeLabel")
Loop
On Error GoTo 0
'Return the phone type result, either LANDLINE or CELL PHONE
GetPhoneNumType = objResult.innerText
End Function
This takes in a phoneNum as a string and returns the result either as LANDLINE or CELL PHONE (I'm not sure if there are any other return types).
For purposes of this example, let's assume that I entered two phone numbers (these can be with or without dashes, but should have no other symbols and no leading 1, etc.) in cells A1 and A2, I can call this function like so:
Public Sub TestPhoneNum()
Dim rngNumbers As Range, c As Range
Set rngNumbers = Sheets("Sheet1").Range("A1:A2")
For Each c In rngNumbers
c.Offset(0, 1).Value = GetPhoneNumType(c.Value)
Next c
MsgBox "Complete!"
End Sub
This will put the phone number type in the column adjacent (to the right) of each cell that contains a phone number. You can adjust A1:A2 to be whatever range has the phone numbers. Whichever range you enter, this subroutine will always place the phone number type in the right adjacent cell.
I added the message box because this code is slow, and that notified me when the code was done executing.
Hopefully this gets you at least started.
I am an old man trying to compare dates from two different files in Excel.
My code is:
Dim i As Integer
For i = 1 To 7
IF Data_for_Processing.xls (“SOLARLOG”). Cells (i,”A”).Value = Day_Conversion_chart.xls (Sheet1).Cells (i+2, “B”) Then
Cells(7+I, “B”)=”Equal”
Else: Cells(7+i, “B”) = “NotEQ”
End If
Next i
Will anyone help?
First of all, I would recommend following #simoco 's advice - Reading the documentation will provide the tools for solving future problems and give you the basics. As well as that, I would recommend using vba help. And another very useful tool for trying commands could be recording macros and analyzing them later on the editor.
So, first you need the code to be inside a macro. It will look like this (I chose the name TestMacro):
Sub TestMacro()
'Code here.
End sub
You should take into account that when your macro is running, it does so from the sheet you are working in, so any partial references to cells, etc. will refer to the book you are in and the sheet you are in when you run the macro. It is possible to select another sheet or book, and if you do so manually or on the code, references will be applied on that selected book or sheet.
What I call partial references here are those that read simply "ThisCell" instead of "ThisBook.ThisSheet.ThisCell". I would say that using partial references, though, is appropriate in a vast majority of cases.
The way your code is organized, be careful to run it from the workbook where you want the data to be in the end, let's say, in your 'main' workbook, so that the final values will be written there..
Another comment: whenever you want to use another file, this file must be open (as far as I know), while you are using it. In your code, you don't open any file, so the macro will not work if you don't open ALL referenced workbooks manually prior to running the macro.
When you want to reference something inside something, you mostly use ".". Please read the documentation - You will get a better idea of how this works. For this example:
Book.Sheet.Cell.Value is the structure you are using.
A book can be referenced as Workbooks("Name.xls") if it is open.
A sheet or worksheet can be referenced as Sheets("Name") or Worksheets("Name"), and also with numbers, like Sheets(1) or Worksheets(1). The differences can be seen on vba editor help. I chose Sheets(...) for your example.
Be careful with the symbols. I guess this was probably my problem, but I have to mention it just in case: When I copied your code, instead of double quotes (""), I got something different, that Excel would not recognize. If for any reason you are using different symbols, Excel might not understand.
A cell can be referenced in various ways too. The Range object is used extensively, and if you want to use "A1", "C44", etc., it's probably better to go for it. I like using Cells(,) as you did, when possible. As far as I know, this works nice with numbers (Cells(1,2), for example), which may be very convenient too. I kept this on your code, changing "A" and "B" and writing 1 and 2, respectively.
With all these changes incorporated:
Comments:
'NOTICE THAT ALL WORKBOOKS MUST BE OPEN.
'["A"] changed to [1]
'[Sheet1] changed to [1]
'["B"] changed to [2]
'Data_for_Processing.xls(“SOLARLOG”).Cells(i, 1).Value
'becomes Workbooks("Data_for_Processing.xls").Sheets(“SOLARLOG”).Cells(i,1).Value
'Day_Conversion_chart.xls(1).Cells(i + 2, 2).Value
'becomes Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i+2,2).Value
'["B"] changed to [2]
And one possible solution:
Sub TestMacro()
Dim i As Integer
For i = 1 To 7
If Workbooks("Data_for_Processing.xls").Sheets("SOLARLOG").Cells(i, 1).Value _
= Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i + 2, 2).Value Then
Cells(7 + i, 2) = "Equal"
Else: Cells(7 + i, 2) = "NotEQ"
End If
Next i
End Sub
This worked on my Excel example - I hope it is of some help.
Regards.
Sub TEST()
If cells(i, "R").Value <> "UK" Then
cells(i, "R").Interior.ColorIndex = 3
End If
End Sub
If I run this program it throws application defined error \
I am new to Excel (beginner)
How to correct this error!!!
Thanks In advance
I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.
This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!
First you have to define i variable
for example: Dim i as variant
I have no experience with vba and would much appreciate your assistance with the implementation of a pop-up calendar user form. This link will direct you to the source of the calendar that I am trying to introduce into my workbook:
http://www.ozgrid.com/forum/showthread.php?t=142603
I have copied the class module, the user form, and the module into my workbook. When I launch the macro, this opens up a user form where i can select the date wished on a calendar.
What I miss is to be able to send the date selected in the calendar to a specific cell in my workbook.
I would much appreciate if somebody could guide me through how to write a few lines of code that would send the date selected in the user form to a specific cell in my workbook!
Again, I am very new to this so let me know if anything is unclear in my explanation. I have spent a lot of time on this so any support is highly appreciated! It probably only takes a few moments for you but would mean a lot to me!
Try this post. It semes to give a better guide to work with datepicker control. However it shows coding to make an add-in.
Hence most basic approach for you would be to,
Add a Form
Add datepicker control
code from there
per this article.
But do remember calendar control in Excel/Access can sometimes disappear due this reason mentioned in my post.
If you are planning to use date picker control, here is the code to pass the value from form to anywhere you want ;)
Private Sub myDtPicker_Change()
Dim dtDateSelected as Date
dtDateSelected = myDtPicker.Value
'-- do anything else
End Sub
The class writes the selected Date into a Textbox. After you selected the Date, you can use the value of the textbox to set the value of the Cell.
Private Sub UserForm_Initialize()
Set clsCal = New clsCalendar 'Initialize the Class'
Set clsCal.Form(Me.TextBoxDate) = Me 'Tells the class to write the Selected date'
' into the textbox "Me.TextBoxDate"'
End Sub
So in that example, whenever you select a date, the class will automaticaly store the selected Date in the Textbox.
After you selected the date, you can use the following code to add the value to a cell:
Range("A1").value=TextBoxDate.Value