Thread: Macro question
View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default Macro question

Hopefully, this is close to what you want. There are a couple of points
where I didn't know what to offset by, they are marked

Sub CreateData()
Dim oCell As Range

'''' First find
Set oCell = GetCell("1")

' This range needs to be offset (0,11)
oCell.Offset(0, 11).FormulaR1C1 = "=(RC[-11]*R10C2)-R10C2"
' M39:M201 needs to be offset. (M39 would be 11 boxes to the
' right. M201 would 11 boxes to the right and 200 down)
oCell.Offset(0, 11).AutoFill Destination:= _
oCell.Offset(0, 11).Resize(200), Type:=xlFillDefault

' Don't need offset
Range("C39:C201").Copy


'''' Second find
Set oCell = GetCell("1")

' offset (0,12)
oCell.Offset(0, 12).Paste


'''' Third find
Set oCell = GetCell("2")

' I have no idea how this would work.
' My data has 2 trials in column A
' The problem is I need to seperate them to get two
' seperate graphs
' In column A, my data is 1,1,1,1....etc. then trial 2
' starts 2,2,2,2,2,2...etc.
' How would you offset it so that the first number 2 is
' selected everytime. Then from there offset (0,12)
oCell.Offset(0, 12).Copy


'''' Fourth find
Set oCell = GetCell("2")

' offset (0,9)
oCell.Offset(0, 9).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False


'''' Fifth find
Set oCell = GetCell("2")

' again offset with the first number 2
'????????????? offset by what <<<<<<<<<<<<<<<<<<<<<<<
Range("C100:C208").Copy


'''' Sixth find
Set oCell = GetCell("1")

' offset (0,13)
oCell.Offset(0, 13).Paste


'''' Seventh find
Set oCell = GetCell("2")

' offset with first 2
'????????????? offset by what <<<<<<<<<<<<<<<<<<<<<<<
Range("L100:O208").ClearContents

Range("A1").Select
Application.CutCopyMode = False
End Sub

Function GetCell(pValue) As Range
Dim oCell As Range
Set oCell = Cells.Find(What:="dev", _
After:=Range("A1"), _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

Set oCell = Cells.Find(What:=pValue, _
After:=oCell, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
Set GetCell = oCell
End Function


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Chris" wrote in message
...
Bob, I deleted my section of code and replaced it with yours. I still got

an
error. To be honest, it took me a half hour to get the correct syntax.

There
was an extra "." in the code. I've never coded with Visual Basic before so
you can tell I have no idea how to fix the rest of my code. I don't mind
learning new things, but for what I'm trying to accomplish it may take me

10
hours to figure this out. If you could adjust my code here I would greatly
appreciate it. Here's the whole macro. I added comments with the ' sign.
Thanks Bob.

Sub CreateData()

Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("M39").Select
' This range needs to be offset (0,11)
ActiveCell.FormulaR1C1 = "=(RC[-11]*R10C2)-R10C2"
Range("M39").Select
' Range needs to be offset (0,11)
Selection.AutoFill Destination:=Range("M39:M201"), Type:=xlFillDefault
' M39:M201 needs to be offset. (M39 would be 11 boxes to the
' right. M201 would 11 boxes to the right and 200 down)

Range("M39:M201").Select
' offset again
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("C39:C201").Select
' Don't need offset
Selection.Copy
Range("A1").Select
ActiveWindow.SmallScroll Down:=24
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("N39").Select
' offset (0,12)
ActiveSheet.Paste
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="2", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("M100:M208").Select
' I have no idea how this would work. My data has 2 trials in column A
' The problem is I need to seperate them to get two seperate graphs
' In column A, my data is 1,1,1,1....etc. then trial 2 starts
' 2,2,2,2,2,2...etc. How would you offset it so that the first number 2 is
' selected everytime. Then from there offset (0,12)
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("L39").Select
' offset (0,9)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="2", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("C100:C208").Select
' again offset with the first number 2
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("O39").Select
' offset (0,13)
ActiveSheet.Paste
Range("A1").Select
Cells.Find(What:="dev", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="2", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=

_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("L100:O208").Select
' offset with first 2
Application.CutCopyMode = False
Selection.ClearContents
Range("A1").Select
End Sub

"Bob Phillips" wrote:

Getting the last row is quite simple as long as you know where you judge

it
against. So for instance if column A holds the id, check it back against
there.

The code I gave creates a range object oCell where the found data

resides.
You just offset against this.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Chris" wrote in message
...
I think it would be a lot harder if I had to determine the last row,

so
what
I do is just go down 200 or so cells after my starting point. The

graph
doesn't include empty cells so I'm okay. I just see a lot of problems

with
this because I'm using a lot of offsetting. I'll test that code

tomorrow
at
work but I have a question though. My macro includes a lot of steps

and
offsetting. Should I go by your code to complete the rest of my macro,

or
are
there are special steps for me to to do to complete the rest of my

macro
with
an offset?

"Bob Phillips" wrote:

That will teach me to test it.

Amended code below, but how do you determine where the last row is,

M201
in
your recorded macro? I think the code might need amending to manage

that

Dim oCell As Range
Dim iRow As Long

Set oCell = Cells.Find(What:="1", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not oCell Is Nothing Then
With oCell.Offset(2, 6)
iRow = Cells(Rows.Count, oCell.Column).End(xlUp).Row
.FormulaR1C1 = "=(RC[-11]*R10C2)-R10C2"
.AutoFill Destination:=Range(Cells(.Row, .Column),

Cells(iRow,
..Column)), _
Type:=xlFillDefault
End With
End If

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Chris" wrote in message
...
It says "invalid qualifier" and points to iRow in the line right

before
the
End With.

"Bob Phillips" wrote:

No tested this, but this should do it

Dim oCell As Range
Dim iRow As Long

Set oCell = Cells.Find(What:="1", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not oCell Is Nothing Then
With oCell.Offset(2, 6)
iRow = Cells(Rows.Count, .Column).End(xlUp).Row
.FormulaR1C1 = "=(RC[-11]*R10C2)-R10C2"
.AutoFill Destination:=Cells(iRow.Column),
Type:=xlFillDefault
End With
End If


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing

direct)

"Chris" wrote in message
...
Thanks Bob. It's good news that I can get the macro to do what

I'm
looking
for except the only bad news is I've never done anything with

visual
basic
before.
I recorded the macro, so what code should I change exactly to
implement
the
offset?

Here's a section of code from the macro.

Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas,

LookAt:=
_
xlPart, SearchOrder:=xlByColumns,

SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Range("M39").Select
ActiveCell.FormulaR1C1 = "=(RC[-11]*R10C2)-R10C2"
Range("M39").Select
Selection.AutoFill Destination:=Range("M39:M201"),
Type:=xlFillDefault
Range("M39:M201").Select
Range("A1").Select

Do I just change the Range values to the formula that you

listed?
Thanks.

"Bob Phillips" wrote:

In a word, there is no way to get the recorder to do that.

The macro can do what you want though, you just have to

modify
it to
do
so.
For instance, to find a value, use Find

Set cell = a_specified_range.Find("value")

and then to offset it use

If Not cell Is Nothing Then

cell.Offset(2,6).Select

etc.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing

direct)

"Chris" wrote in message
...
Is there a way to make a macro remember my keystrokes?

When I
record a
macro
it only remembers the cell number of the box that I

clicked
and
not
how I
got
there. What I would like to do is use ctrl+F to find what

I'm
looking
for
and
then use the keyboard (2 keystrokes down and 6 keystrokes

to
the
right
for
example) to arrive at a cell. However when I arrive at a

cell,
the
macro
just uses the cell number lets say H20 and doesn't

remember
how I
got
there
from using the keyboard. What I'm trying to do is create

a
macro
which
graphs data from several different worksheets. The problem

is
the
data
from
the different worksheets isn't the size of the data from

which
the
macro
was
created. When I run the macro on different worksheets it

doesn't
graph
the
data from the same starting points or ending points. If I

can't
use a
use
a
macro to do this, is there any advice for what I'm trying

to
accomplish?