Define a name in VBA

B

bijan

Hi All,
I need a VBA code to select a non-active sheet(TEST) and then detect
non-blank area to define a name(GI) to it.
Thanks in advance
Bijan
 
D

Daniel.C

HI.
Try :

Sub test()
Dim LCol As Integer, RCol As Integer, TRow As Long, BRow As Long
Sheets("TEST").Select
LCol = Cells.Find("*").Column
RCol = Cells.Find("*", [IV1], , , xlByColumns, xlPrevious).Column
TRow = Cells.Find("*").Row
BRow = Cells.Find("*", [A65000], , , xlByRows, xlPrevious).Row
ActiveWorkbook.Names.Add "GI", _
RefersTo:=ActiveSheet.Range(Cells(TRow, LCol), Cells(BRow, RCol))
End Sub

HTH
Daniel
 
B

bijan

Hi daniel,
Thanks for your answer, your code is working but with wrong range, it Named
range"B2:E23" but the sheet test was started from "A1:E23"
Thanks
Bijan
Daniel.C said:
HI.
Try :

Sub test()
Dim LCol As Integer, RCol As Integer, TRow As Long, BRow As Long
Sheets("TEST").Select
LCol = Cells.Find("*").Column
RCol = Cells.Find("*", [IV1], , , xlByColumns, xlPrevious).Column
TRow = Cells.Find("*").Row
BRow = Cells.Find("*", [A65000], , , xlByRows, xlPrevious).Row
ActiveWorkbook.Names.Add "GI", _
RefersTo:=ActiveSheet.Range(Cells(TRow, LCol), Cells(BRow, RCol))
End Sub

HTH
Daniel
Hi All,
I need a VBA code to select a non-active sheet(TEST) and then detect
non-blank area to define a name(GI) to it.
Thanks in advance
Bijan
 
D

Daniel.C

Sorry :

Sub test()
Dim LCol As Integer, RCol As Integer, TRow As Long, BRow As Long
Sheets("TEST").Select
LCol = Cells.Find("*", , , , xlByColumns).Column
RCol = Cells.Find("*", [IV1], , , xlByColumns, xlPrevious).Column
TRow = Cells.Find("*", , , , xlByRows).Row
BRow = Cells.Find("*", [A65000], , , xlByRows, xlPrevious).Row
ActiveWorkbook.Names.Add "GI", _
RefersTo:=ActiveSheet.Range(Cells(TRow, LCol), Cells(BRow, RCol))
End Sub

Daniel
Hi daniel,
Thanks for your answer, your code is working but with wrong range, it Named
range"B2:E23" but the sheet test was started from "A1:E23"
Thanks
Bijan
Daniel.C said:
HI.
Try :

Sub test()
Dim LCol As Integer, RCol As Integer, TRow As Long, BRow As Long
Sheets("TEST").Select
LCol = Cells.Find("*").Column
RCol = Cells.Find("*", [IV1], , , xlByColumns, xlPrevious).Column
TRow = Cells.Find("*").Row
BRow = Cells.Find("*", [A65000], , , xlByRows, xlPrevious).Row
ActiveWorkbook.Names.Add "GI", _
RefersTo:=ActiveSheet.Range(Cells(TRow, LCol), Cells(BRow, RCol))
End Sub

HTH
Daniel
Hi All,
I need a VBA code to select a non-active sheet(TEST) and then detect
non-blank area to define a name(GI) to it.
Thanks in advance
Bijan
 
B

Bob Phillips

I interpreted non-blank area as usedrange, not as the area with no blank
cells within it. The area isn't blank, individual cells may well be, but not
the whole.
 
S

Shane Devenshire

Hi,

You don't need to activate the sheet to name a range on it, so your code
could be 1 line:

Sheets("Sheet1").UsedRange.Name = "Data"

If this helps, please click the Yes button

cheers,
Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Delete blank worksheets 1
Date Format 5
copy and paste 2
formula and delete sheet 3
save textbox value 1
name manager to define another workbook 4
Excel VBA Macro to do this? 19
Text string searches 3

Top