Excel - Multiple Offsets

Asked By Jon M.
09-Feb-10 04:26 PM
Hi all,
I have a worksheet with the following code listed below.  The idea is that
once a cell in a particular row is populated with an 11 digit claim# it will
jump to the first cell 2 columns over at the top of the particular table of
claim#'s.  I hope that makes sense.  Anyways the first Function works fine,
when I put an 11 digit claim# in row 11 and press enter the correct cell is
selected at the top of the next column I want to enter in.

However when I try to do the same in Rows 23, 35, and 47 nothing happens.
My code is the same for these functions so I cannot figure out why the other
rows will not function the same.  As always any help is always appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 11 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
Else
Exit Sub
End If
End If
End Sub

Private Sub Worksheet_Change_2(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 23 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
Else
Exit Sub
End If
End If
End Sub

Private Sub Worksheet_Change_3(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 35 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
Else
Exit Sub
End If
End If
End Sub

Private Sub Worksheet_Change_4(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 47 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
Else
Exit Sub
End If
End If
End Sub
--
Jon M.
Office
(1)
ActiveCell.Offset
(1)
Worksheet
(1)
VBA
(1)
Target.Offset
(1)
Target.Count
(1)
Target.Value
(1)
IsEmpty
(1)
  JLGWhiz replied to Jon M.
09-Feb-10 04:51 PM
You are only allowed one Worksheet_Change procedure per sheet.  However, you
can organize the code to react to multiple conditions, as shown below.  Give
it a try and post back if there is a problem.



Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 11 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
ElseIf Target.Row = 23 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
ElseIf Target.Row = 35 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If         `
End If
End If
End Sub
  OssieMac replied to Jon M.
09-Feb-10 05:13 PM
Hi Jon,

You cannot create additional events as you have done. You are restricted to
the ones you can create by selecting Worksheet from the dropdown at the top
of the VBA editor and then the events allowable are the ones you can see at
the dropdown to the right top. You have to then code based on identifying the
target as below.

Note I have included the test for length of the string with the IsEmpty test.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub

If IsEmpty(Target.Value) Or _
Len(Target) <> 11 Then Exit Sub

Select Case Target.Row
Case 11
Target.Offset(-10, 2).Select
Case 23
Target.Offset(-10, 2).Select
Case 35
Target.Offset(-10, 2).Select
Case 47
Target.Offset(-10, 2).Select
End Select
End Sub


--
Regards,

OssieMac
  joel replied to Jon M.
09-Feb-10 04:51 PM
You can have only one change function.  try this code it is very simple


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If not IsEmpty(Target.Value) Then
If Len(Target) = 11 Then
Select Target.Row
Case 11:
Range("B2").Select
Case 23:
Range("B12").Select
Case 35:
Range("B24").Select
end select
end if
end if
end if
exit sub


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=177855

[url="http://www.thecodecage.com"]Microsoft Office Help[/url]
  JLGWhiz replied to Jon M.
09-Feb-10 05:17 PM
Missed part of of.  Use this one:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If IsEmpty(Target.Value) Then Exit Sub
If Target.Row = 11 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
ElseIf Target.Row = 23 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
ElseIf Target.Row = 35 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
ElseIf Target.Row = 47 Then
If Len(Target) = 11 Then
ActiveCell.Offset(-10, 2).Select
End If
End If
End Sub
  OssieMac replied to Jon M.
09-Feb-10 05:20 PM
Hi again Jon,

Having another look at the code I am not sure you need the following line.
If Target.Count > 1 Then Exit Sub

Also because the offset is the same for each Case you can code it like the
following.


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub

If IsEmpty(Target.Value) Or _
Len(Target) <> 11 Then Exit Sub

Select Case Target.Row
Case 11, 23, 35, 47
Target.Offset(-10, 2).Select
End Select
End Sub

--
Regards,

OssieMac
  Jon M. replied to OssieMac
12-Feb-10 12:54 PM
A Lifesaver, that is what you are.  Thanks.
--
Jon M.
Create New Account
help
time error when this macro gets to y = ActiveCell: Any help would be appreciated. 40 ActiveCell.Offset(1, 4).Select Do Until x > 0 ActiveCell = 0 ActiveCell.Offset(-1, 0).Select y = ActiveCell.Select Do Until ActiveCell.EntireRow.Hidden = False If ActiveCell.EntireRow Hidden = True Then ActiveCell.Offset(-1, 0).Select End If Loop y = ActiveCell If -x < y Then ActiveCell = y + x Loop Excel Programming Discussions ActiveCell.EntireRow.Hidden (1) ActiveCell.FormulaR1C1 (1) AutoFilter (1) PyendLAYERS (1) Office (1) Sheets (1) Excel (1) Error (1) Try: y = ActiveCell.Value The ActiveCell is already a row number less than 1 or greater than the number of rows on a worksheet (65, 536) because one of your conditions are not being met. There are better ways
VB routine, I'm serching for a dash in a StockNumber. I Tried NrSlut = Find("-", ActiveCell.Offset(Line, -6), 1) But that certainly did not work. The VB Help suggests to implement Peter Hesselager-Olesen SBS-IT Aps Excel Programming Discussions Application.WorksheetFunction.VLookup (1) WorksheetFunction (1) Office (1) Series (1) ActiveCell.Offset (1) Named range (1) Worksheet (1) Workbook (1) Try the Instr function in VB I think you want instr. Something like this. . . NrSlut = instr(1, ActiveCell.Offset(Line, -6), "-") Which will return the position where the dash is found. - - HTH. . . Jim Thomlinson
find all records that contain Public Sub AddNamepro(ByVal Promotion As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As sheet for every day. on the sheets are the people that have come into the office. some receive promotional prices i am trying to find all entries so i can track If ActiveCell <> "" Then Do Until ActiveCell = "" If ActiveCell = MyVal Then varValues(11) = ActiveCell varValues(1) = ActiveCell.Offset(0, -10) varValues(2) = ActiveCell.Offset(0, -9) varValues(3) = ActiveCell.Offset(0, -8) varValues(4) = ActiveCell.Offset(0, -7) varValues(5) = ActiveCell.Offset(0, -6) varValues
Fast code in 2003 = agonizingly slow code in 2007 Excel Using Office XP with Office 2007. I have a severe slow down in my code when using any For Each Cell" or "For x = 1 to 10000". . . This slow down seems to be only in Office 2007. In 2003, my code runs like a machine gun; in 2007 it's more how to make it run faster? Thanks much for your assistance / guidance. Excel Programming Discussions Office XP (1) PivotTable (1) PowerPoint (1) Office 2007 (1) Office 2003 (1) Worksheets (1) Excel 2003 (1) Excel 2007 (1) Always nice to post your not have been clear in my original post. I KNOW how to code, and in Office 2003 my code was optimized and ran superbly. It looks like I'm already using most of Mr. Pearson's suggestions and have been for years. . . The problem is in Office 2007 the same code runs extremely slowly. . .THAT is the problem: SPEED. Please post back
Need Help Modifying Code Please Excel I have a worksheet LOADED w / info; I have created a UserForm with a textbox, commadButton and a listbox i = 0 To 6 Ary(0) = i Next i Ary(0) = ActiveCell.Value Ary(1) = ActiveCell.Offset(0, 1).Value Ary(2) = ActiveCell.Offset(0, 2).Value Ary(3) = ActiveCell.Offset(0, 3).Value Ary(4) = ActiveCell.Offset(0, 4).Value Ary(5) = ActiveCell.Offset(0, 5).Value ListBox1.Column() = Ary Else