Run Time Error 1004 when running VBA

bossOnothin

New Member
Joined
Apr 3, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi, I am trying to scrape data from Yahoo Finance, and the code works fine when there is a defined URL. However, when I added a variable, it gives me error 1004. Here is my code:

VBA Code:
Sub macro()

Cells.Clear

ticker = Sheets(1).Cells(1, k)

qurl = "https://finance.yahoo.com/quote/" & ticker & ""

With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=Range("A27"))
.BackgroundQuery = True
.Refresh BackgroundQuery:=True
End With


End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the Forum!

What is k? Trying to access .Cells(1, 0) will give you a run-time error 1004.
 
Upvote 0
Welcome to the Forum!

What is k? Trying to access .Cells(1, 0) will give you a run-time error 1004.
My plan is to be able to type a ticket symbol in cell K1 and extract data from the relevant Yahoo Finance page. How else should I reference cell K1?
 
Upvote 0
You can refer to cell K1 as .Range("K1"), or .Cells(1,"K") or .Cells(1,11)

But .Cells(1,k) refers to row 1, column k where k is a variable, for which you haven't set a value hence it is empty.

I recommend you use Option Explicit with all your code. This would give you a compile error: variable not defined, letting you know that your syntax for k was not correct.
 
Upvote 0
You can refer to cell K1 as .Range("K1"), or .Cells(1,"K") or .Cells(1,11)

But .Cells(1,k) refers to row 1, column k where k is a variable, for which you haven't set a value hence it is empty.

I recommend you use Option Explicit with all your code. This would give you a compile error: variable not defined, letting you know that your syntax for k was not correct.
Thank you.

One more thing, when I type a new ticker it does not update. How do I make it so that it auto refreshes when I update K1?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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