I am new to coding and programing and would appreciate any suggestions you can give me in creating my first real code.
What I want it to do is ask the user what the Ticker Name is of the stock and once that is input, a message box should display the current price of the stock.
I know the formula to extract the current stock price.
For Example: to find the current value of Netflix all you have to do in a spreadsheet is put "NFLX" in cell(A1) and in cell(A2) put the following formula:
=NUMBERVALUE(WEBSERVICE("http://finance.yahoo.com/d/quotes.csv?s="&A1&"&f=l1"))
Yesterday while learning how to define variables in VBA I was trying to experiment with this concept and came up with the following code which obviously doesn't work:-
Sub Declaring_Variables()
Dim TickerName As String
TickerName = InputBox("Ticker Symbol")
Dim CurrentPrice As Integer
CurrentPrice = Formula ="=NUMBERVALUE(WEBSERVICE("finance.yahoo.com/d/quotes.csv?s="&TickerName&"&f=l1"))
MsgBox (CurrentPrice)
End Sub
I am sure the problem is with the way I have declared the formula for CurrentPrice. I would really appreciate if you can teach me how to use normal formulas which we use on spreadsheet in VBA. I think this will be a good example for me to get better as it covers a very important concept.
Thank you for your time.
Regards,
InWoods
Give this a try:
Option Explicit
Sub Declaring_Variables()
Dim TickerName As String
TickerName = InputBox("Ticker Symbol")
Dim CurrentPrice As String
CurrentPrice = Application.WebService("finance.yahoo.com/d/quotes.csv?s=" & TickerName & "&f=l1")
MsgBox "Text Format: " & CurrentPrice & Chr(10) & _
"Number Format: " & Application.NumberValue(CurrentPrice)
End Sub
Related
I have a lot of dates in column D. I need to find the student with the earliest date, and show the following information in a messagebox:
Sub Finddate()
Dim Mn As Integer
Mn = Application.Match(Application.Min(Range("D2:D18288")), Range("D2:D18288"), 0)
MsgBox ("For the student with the earliest date (" & Range("D" & Mn) & ") the following information applies: " & Range("k" & Mn) & ", " & Range("L" & Mn) & " and " & Range("M" & Mn))
End Sub
However when i run the Macro it shows the wrong date. The earliest date in the sheet is 31-08-1996, but it says the earliest date is 01-02-2010 and if i write =min(D2:D18288) in Excel it finds the right date. But i need it to work in VBA as well. And if i change min to max it also finds the wrong date. But if i instead write:
Mn = Application.Match(Application.Max(Range("D2:D18288")), Range("D2:D18288"))
It shows the right date but i need to find the min date not the max date and when i change max to min I get a type mismatch error. I really don’t know what is wrong really hope someone can help me!
Your indexing is off by 1 ................because the data starts out a D2 rather than D1, Mn points to the cell just above the minimum.
When something like this happens, try to replicate the result, using a small sample. E.g. this one, hoping to return Peter6 for the smallest info:
Option Explicit
Public Sub TestMe()
Dim dateRanges As Range
Set dateRanges = Range("D1:D11")
Dim mn As Variant
With Application
mn = .Match(.Min(dateRanges), dateRanges, 0)
End With
MsgBox Range("E" & mn).Value2
End Sub
Once it works, try to fix it with your big example.
You will probably notice that mn should not be Integer as far as Integer is up to 32767 and this parsed to a date is 16-September-1989, which is long time ago. In your case it is not an error, because you are not referencing mn directly to a date, but it may happen at a later.
I am trying to pull all data entries that are within a userform selected month and year. I can get the code to run fine when I hard code the year but I want the year to come off of a text box. I converted the Textbox value to an integer using Cint() and dim'd it to "Year" in my if statement. I can get it to work if I write Cdate("3/1/2016"), but I want see if there is a way to run it like: Cdate("3/1/Year"). I tried it this way and get a typematch error on the Cdate Im pretty new to VBA so excuse my stupidity.
Ignore the "Month" variable I was just using that to put a stop on the code and step it through to see if it would enter my if statement.
Thanks in advance.
My Code
Private Sub OKBtn_Click()
Dim Sales As Range
Dim Year As Integer
Dim Month As Integer
Dim i As Integer
Year = CInt(YearText.Value)
Set Sales = Worksheets("Sales").Range("A4")
i = 0
If Sales.Offset(i, 1).Value >= CDate("3/1/2016") And Sales.Offset(i, 1).Value <= CDate(" 3/31/2016 ") Then
Month = 1
End If
In order for the CDate to work, you need to seperate the stings inside the brackets to 2 parts
1.The constant, in your case "3/1/".
2.And the variable, CInt(YearText.Value).
Option Explicit
Private Sub OKBtn_Click()
Dim DDate As Date
DDate = CDate("3/1/" & CInt(YearText.Value))
' for debug only
MsgBox "Date entered is :" & DDate
End Sub
I'm trying to make a table that has subtotals every few columns. My vba code brings and sorts data from another sheet into sections and now i'm trying to write the code to have the subtotal formulas put in. Here is what i have:
Sub Macro21()
Dim FI(1 To 3) As Variant
FI(1) = "Fixed Income"
FI(2) = 10
FI(3) = 21
Sheets("Sheet1").Cells(FI(2), 3).FormulaR1C1 = "=SUBTOTAL(9,R[1]C:R[FI(3)-FI(2)]C)"
End Sub
FI(2) and FI(3) are the beginning and ending rows for this section. I use them in other parts of the macro and they are updated as new items are put under a category.
When I run this it give me an error. Any ideas?
I think you need to build the formula as a string, not make it refer your Variant array. How about:
Sheets("Sheet1").Cells(FI(2), 3).FormulaR1C1 = _
"=SUBTOTAL(9,R[1]C:R[" _
& CStr(FI(3)-FI(2)) _
& "]C)"
This assuming the resulting string is what you'd like to calculate...
I am a complete novice in VBA and I'm in way over my head I think but the research necessitates it. I followed a great online tutorial series, which unfortunately didn't help me in solving 1 big problem: Data input.
My goal is to scrape patent data from google patents. To do so, it's pretty convenient that Google patents website is uniquely identified by the patent number. Thus what I want to achieve is the following:
Extract the patent number from a list in excel
Use that number to access the specific webpage
Extract application and publication year of patent, as well as patent number (as check)
Store all in a single excel sheet
Now, I can make 2,3, and 4 work but it's the loop that allows me to extract the patent numbers from excel and put them into my code that I am missing.
Here is the current code:
Private Sub CommandButton4_Click()
Dim obMF As Object
Dim patent As String
Dim grant_date As String
Dim app_date As String
Dim patent_number As String
patent_number = insert.Text ' insert.Text refers to a textbox in my interface
Call gotopat(patent_number, patent, app_date, grant_date)
found.Text = patent
grantdate.Text = grant_date
appdate.Text = app_date
output_row = 1 'Set the output row as 1 (this is where the title is)
Do
DoEvents
output_row = output_row + 1 'Increase output row with 1
Loop Until Sheets("bkcit").Range("B" & output_row) = ""
'Continue loop until that cell ~ Range is blank.
'Once a blank is found, we can put new data in there
'Store data into Worksheet "bkcit"
Sheets("bkcit").Range("B" & output_row) = patent
Sheets("bkcit").Range("C" & output_row) = grant_date
Sheets("bkcit").Range("D" & output_row) = app_date
In this code, found.Text, grantdate.Text, and appdate.Text are sourced from the scraping function which works perfectly. The important things about that function are:
Function gotopat(patent_number As String, patent As String, app_date As String, grant_date As String)
' A Bunch of other stuff
obMF.Navigate ("http://www.google.com/patents/US" & patent_number & "?")
'All the scraping code'
So, I want to replace the patent_number = insert.Text by a loop that looks in my excel sheet bkcit, column A and basically loops through all the unique patent numbers. I tried
input_row = 1
Do
DoEvents
input_row = input_row + 1
Range("C" & input_row) = patent_number
Loop Until Sheets("bkcit").Range("A" & input_row) = ""
But this seems to delete the first patent number in cell A2 and nothing more.
I'm thinking I'm pretty close to a working solution but your help would be fantastic, as always!
Thanks in advance
Simon
If I understand correctly, you have a column of patent numbers like this:
And you want to loop through each number and do something to it. Try this:
Sub loopPatents()
Dim patentNumber As Range
Dim patentRange As Range
Set patentRange = Worksheets(1).Range("A2:A10")
For Each patentNumber In patentRange
MsgBox ("Patent number: " & patentNumber)
'Do your stuff with the patent number here
Next patentNumber
End Sub
Im still very much a novice so be nice if I'm asking a silly question but this is my issue at the moment. I'm starting to create a database in excel for a large archive collection using a user form for the data inputting. The simple bit is that to help identify each item in the collection I want to give each item its own ID. The harder bit is that I want the ID to also include a prefix that makes it clear what sort of item it is. For example the follow prefixes;
Description | Prefix
-----------------------
Newsletter | NEW
Minutes | Min
Photograph | Pho
and to add to the challenge is to have the ID to be sequential under its own prefix (they by not be grouped together though) so the list of IDs could end up like this;
NEW1
NEW2
PHO1
PHO2
PHO3
MIN1
NEW3
MIN2
IF you can suggest anything to help me achieve this i would be most grateful. Thanks in advance.
This assumes a form with a Listbox that has the three choices, a Button to generate the next record ID and a Textbox to display it.
It assumes that the existing IDs are in Column A of the ActiveSheet when the form is activated:
Private Sub UserForm_Activate()
With Me.ListBox1
.AddItem "PHO"
.AddItem "NEW"
.AddItem "MIN"
End With
End Sub
Private Sub CommandButton1_Click()
Dim ws As Excel.Worksheet
Dim Category As String
Set ws = ActiveSheet
Category = Me.ListBox1.Value
Me.TextBox1 = Me.ListBox1 & Application.Evaluate("=MAX(IFERROR(SUBSTITUTE('" & ws.Name & "'!A1:A10," & """" & Category & """" & ","""")*1,0))") + 1
End Sub
It uses Application.Evaluate, which evaluates an array formula as if it was entered into a cell. If the formula was entered into a cell and there were values in A1:A10, and you'd selected "NEW" in the listbox, it would look like this:
And here's the result in the form:
In a database, it is really best to make your ID code be completely free of meaning. That is, do not include the "item type" as a part of your unique ID. It should be a field of its own. The ID code should be nothing more than a unique identifier.