+ Reply to Thread
Results 1 to 13 of 13

Offset Reference

  1. #1
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Offset Reference

    Hi,

    Simple question.

    Say my ActiveCell is "D5", and I want to pull the value of same row but column B. Now I know I could do 'ActiveCell.Offset(0, -2).Value'. But how do I reference column B regardless of where my ActiveCell is??

    For instance I will be in "D5", then maybe "S6", and so on. I always want to get the value of the same row as ActiveCell but in Column B.

    Any ideas??

    Thanks,
    Matt

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    You might be able to use this kind of reference:

    Range("B" & ActiveCell.Row).value

    Does that help?

    Ron

  3. #3
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Ron,

    Thanks a ton!! That was perfect!

    So not to over stay my welcome, but how do I tell a module statement not to run IF XYZ() macro runs?? Does that make sense?

    Thanks a million!
    Matt

  4. #4
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    Without knowing all of the details, you could approach it this way:

    Create a "flag" variable like:
    dim blnXYZ_Ran as boolean

    and initialize it to FALSE.

    After XYZ is run, set it to TRUE

    Then you can just check the blnXYZ_Ran value before running the conditional code.

    if blnXYZ_Ran =false then
    ...your code here....
    end if


    Does that help?

    Ron
    Last edited by Ron Coderre; 04-26-2005 at 03:58 PM.

  5. #5
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Ron, sounds good to me.

    But I am not sure how to work it into my program, below you will see my code...maybe you can make a suggestion?


    Dim vOldVal 'Must be at top of module



    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim blnXYZ_Ran As Boolean

    Dim bBold As Boolean
    If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
    bBold = Target.HasFormula
    With Sheet2
    '.Cells(2, 1) = Format(ThisWorkbook.BuiltinDocumentProperties("Last Save Time"), _
    '"short date")
    .Cells(1, 2) = "CELL CHANGED"
    .Cells(65536, 2).End(xlUp)(2, 1) = Target.Address
    .Cells(1, 3) = "OLD VALUE"
    .Cells(65536, 3).End(xlUp)(2, 1) = vOldVal
    With .Cells(1, 4)
    .Value = "NEW VALUE"
    .ClearComments
    .AddComment.Text Text:= _
    "Matt Kleinke:" & Chr(10) & "" & Chr(10) & _
    "Bold values are the results of formulas"
    End With
    With .Cells(65536, 4).End(xlUp)(2, 1)
    .Value = Target
    .Font.Bold = bBold
    End With
    .Cells(1, 1) = "ENTERED DP"
    .Cells(65536, 1).End(xlUp)(2, 1) = "No"
    .Cells(1, 5) = "TIME OF CHANGE"
    .Cells(65536, 5).End(xlUp)(2, 1) = Time
    .Cells(1, 6) = "DATE OF CHANGE"
    .Cells(65536, 6).End(xlUp)(2, 1) = Date
    .Cells(1, 7) = "CHANGED BY"
    .Cells(65536, 7).End(xlUp)(2, 1) = Application.UserName
    .Cells.Columns.AutoFit
    .Cells(1, 8) = "Customer"
    .Cells(65536, 8).End(xlUp)(2, 1) = Range("C" & ActiveCell.Row).Value
    .Cells(1, 9) = "Location"
    .Cells(65536, 9).End(xlUp)(2, 1) = Range("D" & ActiveCell.Row).Value
    .Cells(1, 10) = "Nick Name"
    .Cells(65536, 10).End(xlUp)(2, 1) = Range("E" & ActiveCell.Row).Value
    .Cells(1, 11) = "Fab Location"
    .Cells(65536, 11).End(xlUp)(2, 1) = Range("K" & ActiveCell.Row).Value
    End With




    vOldVal = vbNullString
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    vOldVal = Target
    End Sub

  6. #6
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    You'd need to define the blnXYZ_Ran flag at the top of a module this way (not within a procedure):

    Option Explicit
    Public blnXYZ_Ran as Boolean


    That way it's scope will be global and it's value will be persistent.

    I hope that helps.

    Ron

  7. #7
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Ron,

    Unfortunately I am not much of a programer, so I am running into errors.

    Before I can even get to the portion of ending module code from running, I have to figure out how to assign a value to a cell (like "B", ActiveCell.Row) if one doesn't exist.

    Any ideas on that??

    Thanks so much for your help!
    Matt

  8. #8
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    Well, here's a little bit of code that just sets the value of the cell in column B of the active cell's row to 1000:

    Option Explicit

    Sub setMyNumber()
    Range("B" & ActiveCell.Row).Value = 1000
    End Sub


    There are many ways to approach setting cell values. I hope you've got a VBA book to help you along with your project.

    Good luck and have fun learning.

    Ron

  9. #9
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    Hi, Matt

    I took a shot at cleaning up your code a little bit. Check out the changes and let me know if you have any questions. Notice that there's no need in your application for the END UP activity. It's easier to just check how many rows the log table is using and just use the next one down. Also note the Option Explicit at the top of the module. That is imperative to prevent "variable creep" (unwanted, unexpected, build-on-the-fly variables)

    I hope this helps.

    Regards,
    Ron

    Option Explicit

    Dim blnXYZ_Ran As Boolean
    Dim vOldVal 'Must be at top of module


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim bBold As Boolean
    Dim intRowRef As Integer

    If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
    bBold = Target.HasFormula

    With ThisWorkbook.Worksheets("Sheet2")
    'If no column headings exist, set them up
    If .[A1] = "" Then
    .Cells(1, 1) = "ENTERED DP"
    .Cells(1, 2) = "CELL CHANGED"
    .Cells(1, 3) = "OLD VALUE"
    With .Cells(1, 4)
    .Value = "NEW VALUE"
    .ClearComments
    .AddComment.Text Text:= _
    "Matt Kleinke:" & Chr(10) & "" & Chr(10) & _
    "Bold values are the results of formulas"
    End With
    .Cells(1, 5) = "TIME OF CHANGE"
    .Cells(1, 6) = "DATE OF CHANGE"
    .Cells(1, 7) = "CHANGED BY"
    .Cells(1, 8) = "Customer"
    .Cells(1, 9) = "Location"
    .Cells(1, 10) = "Nick Name"
    .Cells(1, 11) = "Fab Location"

    'Set the row reference for the first line of details
    intRowRef = 2
    Else
    intRowRef = .[A1].CurrentRegion.Rows.Count + 1
    End If

    'Log the Sheet change activity
    .Cells(intRowRef, 1).Value = "No"
    .Cells(intRowRef, 2).Value = Target.Address
    .Cells(intRowRef, 3).Value = vOldVal
    With .Cells(intRowRef, 4)
    .Value = Target
    .Font.Bold = bBold
    End With
    .Cells(intRowRef, 5).Value = Time
    .Cells(intRowRef, 6).Value = Date
    .Cells(intRowRef, 7).Value = Application.UserName
    .[A1].CurrentRegion.EntireColumn.AutoFit

    .Cells(intRowRef, 8).Value = Range("C" & ActiveCell.Row).Value
    .Cells(intRowRef, 9).Value = Range("D" & ActiveCell.Row).Value
    .Cells(intRowRef, 10).Value = Range("E" & ActiveCell.Row).Value
    .Cells(intRowRef, 11).Value = Range("K" & ActiveCell.Row).Value
    End With


    vOldVal = vbNullString
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    vOldVal = Target
    End Sub
    Last edited by Ron Coderre; 04-26-2005 at 08:14 PM.

  10. #10
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Ron, that works perfectly!!

    Thank you so much!!

    Your approach really makes good sense!

    But I still can't seem to get the code to stop when I execute XZY macro. The macro is initiated by a command button on sheet 1.

    You're a great help Ron...I can't thank you enough!

    Cheers,
    Matt

  11. #11
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    1)Add a new VBA Module
    2)Enter the following:
    Option Explicit
    Public blnXYZ_Ran As Boolean


    3)Initialize that variable at some point in your code (or in an Open event):
    blnXYZ_Ran = FALSE

    4)The last step in the XYZ code should be this line:
    blnXYZ_Ran = TRUE

    Now you just need to check if blnXYZ_Ran =FALSE before executing any conditional code:

    ....other code
    if blnXYZ_Ran = FALSE Then
    ....run your code
    end if
    ....other code


    Does that help?

    Ron

  12. #12
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Hey Ron,

    I am slowly making great progress with this code. Here is the problem.

    Now that I get the code to stop when I execute the macro, I can't get it to turn back on...Since the code is in the Sheet module and not a standalone module it doesn't have an execute.

    Basically, I just don't want my code to track my changes when I execute my macro....

    I tried your above solution, but maybe I am not using it right.

    Thanks for your continued help!!

    Matt

  13. #13
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    Well....Since I think you're running your XYZ code to stop the tracking, maybe you need an XYZ_Off macro that just sets the blnXYZ_Ran variable back to FALSE to re-engage tracking:

    Sub XYZ_Off()
    blnXYZ_Ran = FALSE
    End Sub


    Is that something you could use?

    Ron

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1