Input range and use it in VBA

zarkoff

New Member
Joined
Feb 25, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

(sorry for my english).

I need to consolidate data from several sheets for which the range I have to look at is different by sheets.

So I created a table in which I put the name of the sheet and the range I need to look at:

SheetsRange
BOBA4:E2000,AS4:BD2000
JONA4:E2000,F4:Q2000
EDIA4:E2000,BF4:BQ2000

I wanted to use a for... next, but I get an error.

VBA Code:
Sub test()

Set xl = Excel.Application
Set macr = xl.ActiveWorkbook
Set def = macr.Sheets("Range")
    
For a = 2 To 3

    sh_name = def.Cells(a, 1)
    sh_range = def.Cells(a, 2)

    Sheets(sh_name).Range(sh_range).Select
    Selection.Copy

'(... other tasks)

Next

End Sub

I get an error message '1004': Select method of Range class failed, at step " Sheets(sh_name).Range(sh_range).Select"

Do you have an idea how to solve my problem (or an alternative way) ?

Thank you.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You can't use "Select" on a range in a none active sheet. the sheet you're referencing has to be the active one in order to do that. You can skip that step though:

VBA Code:
Sub test()
    Dim a&, sh_name$, sh_range$, def As Worksheet
    
    Set def = Sheets("Range")
    For a = 2 To 3
        sh_name = def.Cells(a, 1)
        sh_range = def.Cells(a, 2)
        Sheets(sh_name).Range(sh_range).Copy
    '(... other tasks)
    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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