Help to Combine VBA-code / run-time error 9 subscript out of range

KlausW

Active Member
Joined
Sep 9, 2020
Messages
386
Office Version
  1. 2016
Platform
  1. Windows
Hi

I use this VBA code to combine different sheets into one sheet, the name of this sheet is in cell D1.

The different sheets are dynamic in the number of rows from time to time.

In the sheet "Stamdata" from Cell A2 an down, there are the names of all the sheets to be collected.

In the "Stamdata” sheet, cell D2 contains the letters of the columns to be combined.

I get the following error message when I run the VBA code.

run-time error 9 subscript out of range

Some who can help.

Any help will be appreciated.

Best regards

Klaus W



VBA Code:
Sub Rektangelafrundedehjørner2_Klik()

Dim wksHent As Range

Dim wksSaml As Worksheet

Dim sidsterk As Long

Dim kopiomr As Range

Dim kolonner As Variant

kolonner = Split(Names("HentKolonner").RefersToRange.Value2, ":")

Set wksSaml = Worksheets(Names("SamlearkNavn").RefersToRange.Value2)

wksSaml.Cells.ClearContents

For Each wksHent In Names("OpsamlingFra").RefersToRange.Cells

sidsterk = Worksheets(wksHent.Value2).Range("A" & Worksheets(wksHent.Value2).Rows.Count).End(xlUp).Row

'kopi dataomkr

Set kopiomr = Worksheets(wksHent.Value2).Range(kolonner(0) & "2:" & kolonner(1) & sidsterk)

wksSaml.Range("A" & wksSaml.Rows.Count).End(xlUp).Offset(1, 0).Resize(kopiomr.Rows.Count, kopiomr.Columns.Count).Value2 = kopiomr.Value2

'kopi overskrift

wksSaml.Range("A2").EntireRow.Value2 = Worksheets(wksHent.Value2).Range("A2").EntireRow.Value2

Next

End Sub
 

Attachments

  • 2023-05-21 (3).png
    2023-05-21 (3).png
    61.6 KB · Views: 7

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.
You are referencing something that does not exist, likely a worksheet name that does not exist in the worksheets collection for that workbook. Your code loops though a name collection and builds worksheet names and defines range cell ranges based on that information . Usually it means that one of your worksheet names is not valid. Use the debugger to track down which one.
 
Upvote 0
Your code is cycling through the cells defined by the Named Range "OpsamlingFra".
This is the range A2:A4. A4 is blank so when it tries to use the value of A4 as a sheet name you get the subscript out of range error.

Screenshot 2023-05-22 223119.png
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,302
Members
449,218
Latest member
Excel Master

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