Define changing column range

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
I am trying to define a range of cells that may change. I have a worksheet that imports data from another sheet, and the number of columns in one part may change depending on how many months of data are available. What I need to do is define the number of columns for use in the code. Here is what I have so far, but it isn't working. The code needs to look through the values in Range N1:BJ1 for a cell that starts with "CY", then pass the column value just to the left of this cell to a range. There are also other columns that need to be defined, but they are always 2 columns and 25 columns to the right of the cell starting with "CY." Any thoughts? and thanks in advance.

Code:
Dim LastMonthY1 As Double
Dim FirstMonthY2 As Double
Dim LastMonthY2 As Double

For Each Cell In Worksheets("Data").Range("N1:BJ1")

If Left(Cell.Value, 2) = "CY" Then

LastMonthY1 = ActiveCell.Offset(0, -1).Column
FirstMonthY2 = ActiveCell.Offset(0, 2).Column
LastMonthY2 = ActiveCell.Offset(0, 25).Column


End If

Next Cell


Worksheets("Data").Activate

Set allData = Worksheets("Data").Range("N3:" & LastMonthY1 & Range("A65536").End(xlUp).Row)
Set allData1 = Worksheets("Data").Range(FirstMonthY2 & "3:" & LastMonthY2 & Range("A65536").End(xlUp).Row)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

The column property returns a number, not a letter, ie, 1 for column A, 2 for B, and so on.

So try something like this
Code:
With Worksheets("Data")
        Set alldata = .Range(.Cells(3, "N"), .Cells(.Range("A65536").End(xlUp).Row, LastMonthY1))
End With

HTH

M.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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