VBA runtime error

strongman86

Board Regular
Joined
Feb 15, 2017
Messages
115
Office Version
  1. 2019
Platform
  1. Windows
Lads,

I have issue with my manually entered data on excel spreadsheet misaligning when data is refreshed from MS query e.g row is added or removed by query. I have came across this:
https://www.techsupportforum.com/forums/f57/mis-aligned-data-on-data-refresh-565373.html
I have tried to follow instructions, but when I run VBA I get run-time error '9': Subscript out of range.

In my case:
1) Unique Identifier is in column B (alphanumeric)
2) Yes I have headers
3) Extracted data worksheet name: MdProductDataList
4) Updated data worksheet name: Data
5) Excel is version 2013

Code I'm using:
Code:
Sub test()
Dim cnt, rw As Integer
Dim val As Variant


cnt = Worksheets("Data").Range("B20000").End(xlUp).Row
On Error Resume Next


For rw = cnt To 2 Step -1
val = Empty
val = Application.WorksheetFunction.VLookup(Worksheets("Data").Range("B" & rw).Value, Worksheets("MdProductDataList").Range("B:B"), 1, False)
If val = Empty Then
Worksheets("Data").Rows(rw).Delete
End If
nxt:
Next rw
End Sub

Debug points out this:

cnt = Worksheets("Data").Range("B20000").End(xlUp).Row

How can I fix this? Thanks.
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
That error on that line would indicate that the 'Data' worksheet does not exist.

Can you doublecheck you have created that worksheet before running the code?
 
Upvote 0
jwalkerday,

Yes both spreadsheets are on my desktop. Also checked spelling all ok. Note on ''Data'' do I need to point out which tad to use as I have 3 and query is on 3rd tab. On ''MdProductDataList'' only one tab.
 
Upvote 0
That sounds like your problem.


You need one spreadsheet (called workbook in code) to contain at least Data and MdProductDataList for this code to run successfully.


You can change your code to operate in separate spreadsheets (workbooks) by using the workbook object, something like this if the workbook is open-


Workbooks(“...Accounting Reports.xlsm”).Activate
Sheets(“Data”).Range("B20000").End(xlUp).Row
 
Upvote 0
jwalkerday,

I've used your suggestion to use one spreadsheet with 2 tabs called ''Data'' & ''MdProductDataList'' and macro now works, but it only works when row is removed by query. When new row is added by query it doesn't seem to do anything. Is there a code i can add for that?
Thanks.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...ow-but-not-adding-one-after-data-refresh.html

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Fluff,
I'm terribly sorry won't happen again. I'm just a poor lad seeking for answers. :help:
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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