+ Reply to Thread
Results 1 to 4 of 4

Macro: assign value of x-y postion

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2011
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    2

    Cool Macro: assign value of x-y postion

    Hi,

    I need to create a macro which will give the cell of a co-ordinate a value of 1.

    Basically I have two columns called X-Position and Y-Postion e.g.

    X-Position Y-Position
    2 16
    6 9

    So the marco will take the 2nd row 16th column in and assign that cell the value 1 then take the 6th row and 9th column in assign give the cell value 1 and so on...
    Last edited by james.carey; 05-17-2011 at 12:23 PM.

  2. #2
    Valued Forum Contributor mohd9876's Avatar
    Join Date
    05-04-2011
    Location
    Amman, Jordan
    MS-Off Ver
    Excel 2010
    Posts
    426

    Re: Macro: assign value of x-y postion

    assuming the the x and y columns are A and B:

    Dim Xpos As Range
    Dim r As Range
    Set Xpos = Range("A2", Range("A" & Rows.Count).End(xlUp))
    For Each r In Xpos
           Cells(r.Value, r.Offset(0, 1).Value) = 1
    Next r

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Macro: assign value of x-y postion

    james.carey,

    Welcome to the Excel forum.


    I assume that your coordinates are in columns A and B, and begin in row 2.

    The macro will check the coordinates, and if they are not valid, they will be highlighted RED.


    Detach/open workbook SetXY coorinates equal to 1 - james_carey - EF776351 - SDG13.xls and run macro SetXY.



    If you want to use the macro on another workbook:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    
    Option Explicit
    Sub SetXY()
    ' stanleydgromjr, 05/17/2011
    ' http://www.excelforum.com/excel-programming/776351-macro-assign-value-of-x-y-postion.html
    Dim c As Range
    Application.ScreenUpdating = False
    For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If c = "" Or c < 1 Or c > Rows.Count And c.Offset(, 1) = "" Or c.Offset(, 1) < 1 Or c.Offset(, 1) > Columns.Count Then
        c.Resize(, 2).Interior.ColorIndex = 3
      Else
        Cells(c.Value, c.Offset(, 1).Value) = 1
      End If
    Next c
    Application.ScreenUpdating = True
    End Sub

    Then run the SetXY macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Registered User
    Join Date
    05-17-2011
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    2

    Smile Re: Macro: assign value of x-y postion

    Thanks Guys!!

    You both were a big help it solved my problem perfectly, Thanks for the speedy response also.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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