Define Dynamic Range In Macro Code

Feb 26, 2008

I have been tring to define a dynamice range in VBA. At the moment, I use following method to Define the range,

Sub OTC_Future_Total()


Dim DynamicRange As Range

Worksheets("FinalReport").Select
Range("e9").Select
Set DynamicRange = Range(Selection, Selection.End(xlDown))
DynamicRange.Select...

View 5 Replies


ADVERTISEMENT

Define A Dynamic Column Range

Nov 3, 2008

I'm trying to define a dynamic column range call 'Cost'

If it starts in Column C, row 4 [C4]. I want the range that to go down to the Row I have defined as 'subtotal'

The user will be able to insert new rows above 'subtotal'

How can I define Column C4 so that any new row added will be including in the defined row range 'Cost'?

View 9 Replies View Related

VB Define Dynamic Ranges, Use The Range For Find Or Vlookup

Jul 9, 2008

I have an worksheet that I import a csv into, each day a new csv is added to the bottom of the previous csv data. I have some code that extracts the date and month # from the cell and places them in helper columns. The code find the last used cell in the helper column and the imported data column to find the first and last row of the new day.

This part works fine. However, I assigned a variable name to the first and last variables and would like to uses these row number to define a range in order to use the range for a vlookup or find operation. This is where I get stuck. I want to use column x and row (variable from first bit of code) to column y and row (variable from first bit of code). Then use a vlookup/find whatever works to find the text I need and get the data. Tips on looping the code would be welcome as well.

I have searched for answers to the problem, on the board and web, but have not found a solution that works for me. (at least that I could get to work!!)

Below is the code as I have it

Public Sub Enter_Date()

Dim DateA As Date
Dim DateB As Date
Dim Cnt As Integer
Dim End_of, Beg_of As Integer
Dim Count As Integer
Dim NumtoFill As Integer

View 9 Replies View Related

Set Pivot Table Dynamic Source Range Macro Code

Apr 9, 2008

Ultimate goal is to automatically update the source data for 4 pivot tables that are on 1 worksheet. The data for those 4 pivot tables are on 4 different worksheets w/in the same workbook. Consolidating into one dataset is not an option.

I'm familiar with a dynamic named range, but the 4 worksheets that contain the data are replaced daily via automated Access output which creates an error.

I know how to do this adhoc by matching the pivot table names with their respective worksheets, but there are many other documents with similar layouts where this would beneficial.

Below is an example of how I currently update 3 pivots on separate worksheets w/ the same data range which is w/in the same workbook. My proposed changes are below the current. Any ideas on how to return the workbook name as a string...Or am I going about this the completely wrong way...

View 8 Replies View Related

Define Worksheet & Range In One Code

Nov 8, 2008

I got this code I have been trying to solve but it does not work...
What I need to do is define a worksheet and a range of that worksheet.
I can only seam to work out either a worksheet or a range but not the two together.

Private Sub cmd_Cform_Click()
Dim rng As Range
Set rng = Range("A1")
With Sheets("Customer")
rng = .Range("A1")
Application.CommandBars.FindControl(ID:=860).Execute
End With
End Sub

View 9 Replies View Related

Define Sheet Level Name Range Code

Dec 3, 2007

In general defining Sheet Level Name Ranges works fine with me, most of the times I use something like shown in the code below.

However, I have discovered that apparently the successfull execution depends on the sheet-name format itself

The code below works perfectly fine with worksheet names like "234", "cas422", etc
but it fails to work with worksheet names like: "C12c", "eh34h4". It seems to me - but I am not sure of being able to generalize this - as if it works when the name contains [numbers][letters] or [letter][numbers], but it fails when it contains a mixture of both like [numbers][letters][numbers] or [letters][numbers][letters] .... in case of a failure of the code, I go into debug mode and simply rename the sheet, then everything's fine again.

Am I the only one to experience such difficulties? (I run Excel 2003 with SP3 on Windows XP) Or would a different code prevent this kind of trouble?

I think, I could write a workround, but before doing so I'd rather like to ask if I am doing something wrong here?

ActiveWorkbook.Names.Add name:=wks.name & "!myTime", RefersToR1C1:="=" & wks.name & "!R1C1:R5C8"

View 9 Replies View Related

Define Range During Macro

Apr 25, 2006

I'm trying to define a range to be copied during execution of a macro. The range to be copied must include all contiguous data from a defined starting range to the rightmost column and the bottommost row containing data (boht of which can change depending on input data). I use <End>+<RightArrow> and <End>+DownArrow> to define the range when working in Excel (not under macro control).

I used the macro recorder to define the code for the process described above, with results shown below:

Sub Macro2()
Sheets("Source_Info").Select
Range("A2:D6").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Working_Data").Select
Range("A2").Select
ActiveSheet.Paste
End Sub

When I run the macro, however, the End(xlToRight) and End(xlDown) commands do not have any effect (i.e., the rang is not made any bigger when these statements are executed).

I've used the same code in previous macros (with previous versions of Excel and Windows), and it worked fine. I'm running Excel 2003 under Windows XP Professional now.

View 5 Replies View Related

Define Named Range In VBA Macro

Dec 29, 2009

I am trying to define a number of ranges in different sheets using VBA I am using the follwoing syntax but I am getting a 1004 error:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

ThisWorkbook.names.Add Name:="Test", RefersTo:=Worksheets("Sheet1").Range("E6", Range("E6").End(xlDown))

End Sub

As long as I have sheet1 active, it works. If I have another sheet active and change smt, i get the error.

The code is located in the ThisWorkbook object.

View 6 Replies View Related

Macro: Re-Define Named Range

Nov 21, 2006

How can I redefine name of the cell? This is what I have tried and not work

intWDColumn = Range("rngWaterDepthStart").Column + 3
intWDColumn = intWDColumn + 1

strNameRange = "rawData!R7C3:R7C" & intWDColumn

ActiveWorkbook.Names.Add Name:="rngWaterDepthStart", RefersToR1C1:=strNameRange

and the cell name have define to ="rawData!R7C3:R7C7" which the "" stop the rename working I belive.? What I want is to add or reduce extra column into the name depends on macro.

View 4 Replies View Related

Automatically Define A Variable Range Using A Macro

Nov 25, 2009

1. I need to define a variable range of all the USED cells in a column (starting at row 2 as the row 1 is the header)

2. I need to do this for columns A through to P inclusively (all using the same lastrow reference as that of column A)

3. I need to call the range name by the contents of the header in that column (Row 1)

4. Some of the data in the header row (row 1) contains spaces - can we change this to an underscore for the range name only?

5. Does a range only work for the active worksheet? Because i will have multiple worksheets, all with the exact same information in row 1, but different numbers of rows (and different data as well), but i will need the range defined on each worksheet......

I've found the following code and thought it was it, but it only defines on 1 worksheet, and for some reason only defines ranges for columns 1 and 2. Also it defines the range as A1:A65535 - not the USED range.....

View 9 Replies View Related

Udf Vb Looking To Define A Dynamic Absolute Reference

Mar 13, 2007

I'm trying to create a user defined function to drop into dblookup functions that will always reference the first cell in an associated column and the first cell in the row of an associated column.

I came up with the following, however the problem is that the udf always returns the values for the active cell's, row and columns.

Function SRCref2()
Application.Volatile

myfield1 = ActiveCell.Column
Dim1 = Columns(myfield1).Range("a1").Value
SRCref2 = Dim1
End Function

Function SRCref3()
Application.Volatile

myfield2 = ActiveCell.Row
Dim2 = Rows(myfield2).Range("a1").Value
SRCref3 = Dim2

End Function

I would nest these in a formula such as "=srcsum(srcref2(), srcref3())".
Ideally I could then use this same funtion anywhere instead of having to use and worry about the accuracy of "=srcsum($a$2,$b$1)"."=srcsum($a$3,$b$1)"."=srcsum($a$4,$b$5)".etc.

It seems like there should be a current cell method or something but I can't find it and I'm not smart enough it seems to use vb to designate some kind of floating intersect as a base for a row and column reference.

View 8 Replies View Related

Add Dynamic Named Range Via Code

Nov 30, 2007

I am using follwoing piece of code for offset function. For Some reason it is not working.

Reference = Cells(TableRowNumber, TableColumnNumber).Address
Col = Application.WorksheetFunction. CountA(rngF.Columns(TableRowNumber, TableColumnNumber).EntireColumn)

ActiveWorkbook.Names.Add Name:=TableName, RefersToR1C1:= _
"=OFFSET(Reference,0,0,Col-3,1)"

Col value is also being calculated to zero. TableRowNumber and TableColumnNumber are the variables which store the starting location of the column.

View 2 Replies View Related

Dynamic Range In A Macro

Nov 17, 2011

I am trying wihout suceess to create a range to copy in a macro each time the macro loops. This is thus a dynamic range. My failed attempt is listed below. How is this done?

ColumnAdjust=2
For I = 1 To 282
Range ("B5").Select
Range(ActiveCellOffset(0,ColumnAdjust),ActiveCellOffset (0,ColumnAdjust+1))
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
.
.
.
ColumnAdjust= ColumnAdjust=2
Next I

View 4 Replies View Related

Dynamic Range In Macro

May 28, 2007

I am attempting to create a dynamic range within a macro.

Each week, a list of performance factors is recorded for employees. The macro is about to do an Advanced Filter to create reports based on user-provided criteria. Since there is new data every week, the range used for the Advanced Filter is dynamic. The code for the Advanced Filter refers to a range entitled “Database.” My problem is in defining “Database.”

It is currently 534 rows and 8 columns, starting at A1.

The relevant portion of the macro currently reads:

Sub Check1()

ActiveWorkbook.Names.Add Name:="Database", RefersToR1C1:= _
"=OFFSET('Loan Officer History'!$A$1,0,0, COUNTA('Loan Officer History'!$A:$A),8)"

Range(Database).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"J1:Q2"), CopyToRange:=Range("J5:Q5"), Unique:=False

End Sub

When running the macro, the debugger stops at the line which starts "ActiveWorkbook.Names" and tells me “Run-time error ‘1004’. The formula you typed contains an error.”

View 3 Replies View Related

Macro Code Create Pivot Table Based On Dynamic Source Data

Nov 28, 2006

The "Sum" sheet can change its number of rows. The pivot table is based on it. I'm having trouble with the SourceData portion of the code in my macro ....

View 9 Replies View Related

Macro To Sort Dynamic Range?

Jun 6, 2013

I have a Ranking tab that has the persons name, weeks, months that subtotal into 2 columns, MTD and YTD. I have set up with 2 Macros to sort based on two columns based on their selection of MTD or YTD.

What I did not take into consideration is if a person was added at the bottom of the list.

Is there a way to modify this to include rows that may be added?

VB:
[SIZE=4]CODE HERE[/SIZE]
VB:
[B]Macro1
[/B]Sub SortYTD()

[Code].....

View 8 Replies View Related

CountA - Dynamic Range - Macro

Sep 6, 2007

I recorded this macro based on a column with a drop down window in which data has been filtered .....

View 12 Replies View Related

Dynamic Range For A Copy Macro.

Feb 20, 2009

I need a bit of assistance in identifying the dynamic range so that my macro below will work:

View 3 Replies View Related

Macro / Set Dynamic Print Range?

May 6, 2004

I am still having some difficulty in getting a macro to select a print range on a text file imported into excel. All the data pulls into column A which is OK. I'm trying to get my macro to highlight & select the print range by going Home, End Down, Tab right Tab right and then select whatever that highlighted range is. This range will change from file to file.

My macro reads as follows:

Sub PrintToView()
'
' PrintToView Macro
' Macro recorded 5/5/2004 by
'
'
Range("A1").Select

[code]....

View 5 Replies View Related

Macro To Run On A Dynamic Selection Range

Oct 13, 2008

I am looking to run a marco for a selection range, and the selection range could vary in size.
Using the Macro recorded (whilst turning on the relative reference) the Macro runs for a defined number of cells. I would like to run certain Macro, for different ranges of cells.

View 9 Replies View Related

Dynamic Range - Moving With A Macro

May 18, 2006

I am not sure if my problem is in the dynamic range or not. I have attached a small sample file. I tried to create a dynamic range by creating a named range of my "LabelRow" with the following as the refers to: =OFFSET($C$32,0,1,0, COUNTA($32:$32))

When I set my cursor on this, Excel makes an outline on the whole row 32 where there is data, which gives me some clue that it understands what I am trying to define. Next, I try to pick this range up and paste it using named ranges in a macro (CopyRange). It gives me an error like it doesn't understand the named range. I do similar macro code in many other places in this project and it works with non-dynamic ranges. Here are some other details on this project. I need a dynamic range because I insert new data into the dataset, which may be 2 quarters up to 20 quarters. This insert causes a regular range to be messed up. My ultimate goal is to graph selected rows of data with the same labels, but I need to have it starting at Q1, which is pushed to the right as new rows are added.

View 2 Replies View Related

Selecting Dynamic Range Of Rows Macro?

Jan 28, 2014

I am trying to select rows in excel using a macro and then hide the rows, but i want to be able to add rows within this selection and still be able to hide the complete selection of rows.

The macro i am currently using is shown below: This hides rows 131 - 205, but if i was to add in another row in between so the range i now want to hide is 131-206 can i get the macro to update to this new range?

[Code] ......

View 10 Replies View Related

Macro For Average Of Values - Dynamic Range Of Cells

Aug 1, 2013

I need to find average of the values , the count of the cells will be dynamic (may be 5 or even 200).

View 2 Replies View Related

Define Range() In Vba

Jan 23, 2007

I want to define a Range() in VBA. have cell1 and Cell2 in the format of rows and cols. ie. Cell1 = Row 1, Col 2. Cell2 is dynamic, can be sometimes row100, Col200, or Row23, Col 1000. May i know how to define it in VBA?

View 2 Replies View Related

Define A Time Range

Jan 21, 2010

I have two columns containg the arrival and departure hours of workers. From these columns I must define the workshift by specifiyng the time range for each shift. AZ contain the arrival hours while BA the departure.

The formula I use doesn't return the results correctly because some shifts are almost the same. i.e: if shift one starts (arrival hours) between 05:00 and ends at 12:59 and shift 1-2 starts at 08:00 and ends at 23:59 then it will go with the first shift even though the times in range belong second shift.

View 8 Replies View Related

Define Range As Variable?

Feb 23, 2013

why this won't work.

Code:

Dim DRange As String
Dim ERange As String
Dim SRange As String
EndRow = Range("A65536").End(xlUp).Row
DRange = Range("D1", "Z" & EndRow)
ERange = Range("E1", "Z" & EndRow)
SRange = DRange

View 6 Replies View Related

Using Variable To Define End Of Range

May 19, 2008

how to define the end of a range using a variable determined by a rng.Rows.Count command (or anything else)? Is this even possible? Here is what I currently have:


Set rng = Range("A1:A10000")
For i = rng.Rows.Count To 1 Step -1
If rng.Cells(i).Value = "0" Then rng.Cells(i).EntireRow.Delete
Next

I'd like to define the end of the range with more accuracy than I'm currently doing.

View 9 Replies View Related

Define An Unknown Range

Aug 13, 2006

is it possible to creaate a macro to highlight a range that starts at a1 to a? and there could be blanks amongst that range. the range will be unknown

View 9 Replies View Related

Define A Variable Range

Dec 22, 2006

I am searching for data, upwards from cell A1400. Once I find data (cell > 0), I need to count up 27 rows and then select these rows for columns A to E. Then I want to copy this block of data and paste it. I have been having difficulty with the selection of a block of data which will be in a different position depending upon the data used. The code I am using is below. The two lines that I need to replace with a Variables cell range reference are:

Range("A1268:E1242").Select
Range("A1268:E1242").Activate

Sub CalculateATR()
Sheets("Data").Range("a1400").Select
Do
If ActiveCell.Value > 0 Then
ActiveCell.Select
Exit Do
Else
ActiveCell.Offset(-1, 0).Select
End If
Loop Until ActiveCell.Value > 0

Range("A1268:E1242").Select
Range("A1268:E1242").Activate
Selection.Copy
Range("G5:K31").Select
ActiveSheet.Paste
Else
End If
End Sub

View 8 Replies View Related

Define Named Range

Feb 7, 2007

how to set up an array within a worksheet

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved