0

Trying to define the same range (A:WWW) for each worksheet that is on a list in Sheets("B").Range(C10:C42) but below shown code is not working.

Sorry for not explaining the macro's objective well enough. Please let me try again here: The objective is to name 20 ranges from 20 worksheets. Example: There are 20 sheets, named apple, orange, grape, etc. This name list is in sheet "Background" in C1:C20, let's say. So... In Apple sheet: Name columns A:WWW as "Hist_Apple" In Orange sheet: Name columns A:WWW as "Hist_Orange" In Grape sheet: Name columns A:WWW as "Hist_Grape" Etc... for 20 times. Thank you for the quick response!!

Sub Define_Range()

Dim ws As Worksheet
Dim rng As Range
Dim foundws As String

For Each ws In ThisWorkbook.Worksheets

  Set foundws = Sheets("B").Range("C10:C42").Find(ws.Name, LookAt:=xlWhole)

  If foundws Is Nothing Then

    Sheets(foundws).Select

    With ActiveWindow
        Set rng = foundws.Range("$A:$WWW")
        ActiveWorkbook.Names.Add Name:="Hist_" & foundws.Name, RefersTo:=rng
    End With

 End If

Next ws

End Sub
4
  • How exactly it doesn't work? Commented Mar 17, 2019 at 8:23
  • Edit the post and write exactly what you are trying to achieve ! Commented Mar 17, 2019 at 8:52
  • Sorry for not explaining the macro's objective well enough. Please let me try again here: The objective is to name 20 ranges from 20 worksheets. Example: There are 20 sheets, named apple, orange, grape, etc. This name list is in sheet "Background" in C1:C20, let's say. So... In Apple sheet: Name columns A:WWW as "Hist_Apple" In Orange sheet: Name columns A:WWW as "Hist_Orange" In Grape sheet: Name columns A:WWW as "Hist_Grape" Etc... for 20 times. Thank you for the quick response!!
    – SofiaEd
    Commented Mar 17, 2019 at 9:08
  • Set rng = foundws.Range("$A:$WWW"): foundws is declared as string but u use it here as sheet. -> Set rng = .Sheets(foundws).Range("$A:$WWW")
    – busybyte
    Commented Mar 17, 2019 at 15:27

1 Answer 1

1

Without seeing your data, it is unclear, but consider replacing:

If foundws Is Nothing Then

with:

If Not foundws Is Nothing Then

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .