Define new table range

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
I have a code below that I've been tinkering with to create a new table for the next fiscal year. The only issue I'm having so far is that I want the table range to go down 1,500 rows versus. The size that this code creates is only for one row. Not sure how to get there. Currently, this code finds the last used cell in row one and then offsets to the right of that cell two cells to create this single column table of just only one row.

VBA Code:
Sub CreateNewFiscalYearTable()
Dim ws As Worksheet
Dim lastCol As Long
Dim myRange As Range

Dim sThisFinancialYear As String
sThisFinancialYear = "tblPO" & IIf(Month(Date) <= 9, Year(Date) + 1, Year(Date) + 1) & "List"  'REMOVE THE " +1 " AFTER "Year(Date)" FROM THIS LINE LATER AFTER TESTING, SPS, 01/10/24

Set ws = Worksheets("Drops")
Set myRange = CELLS(1, Columns.Count).End(xlToLeft).Offset(0, 2)

MsgBox myRange.Address

myRange.Select

Dim tb As Range
Dim wsht As Worksheet
Set tb = myRange
Set wsht = ActiveSheet
ws.ListObjects.Add(SourceType:=xlSrcRange, Source:=tb).Name = sThisFinancialYear    '"tblPO" & sThisFinancialYear & "List"



End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Please try this:

VBA Code:
Sub CreateNewFiscalYearTable()
  Dim ws As Worksheet
  Dim lastCol As Long
  Dim myRange As Range
  Dim sThisFinancialYear As String
  
  sThisFinancialYear = "tblPO" & IIf(Month(Date) <= 9, Year(Date) + 1, Year(Date) + 1) & "List"  'REMOVE THE " +1 " AFTER "Year(Date)" FROM THIS LINE LATER AFTER TESTING, SPS, 01/10/24
  
  Set ws = Worksheets("Drops")
  With ws
  Set myRange = .Range(.Cells(1, 1), .Cells(1, Columns.Count).End(xlToLeft).Offset(1500, 2))
  End with
  
  MsgBox myRange.Address
  
  myRange.Select
  
  Dim tb As Range
  Dim wsht As Worksheet
  Set tb = myRange
  Set wsht = ActiveSheet
  ws.ListObjects.Add(SourceType:=xlSrcRange, Source:=tb).Name = sThisFinancialYear    '"tblPO" & sThisFinancialYear & "List"



End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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