Results 1 to 1 of 1

Find Pixel Position of a Cell

Threaded View

  1. #1
    Registered User
    Join Date
    07-03-2014
    Location
    East Lansing
    MS-Off Ver
    2010
    Posts
    20

    Find Pixel Position of a Cell

    I am trying to use the getpixel function to find the color of the middle pixel in each cell starting from cell AA1. However, currently when I run the code, it selects pixels starting from the top left corner of my monitor, instead of from cell AA1. Anyone know what I can do to specify the pixel position of a cell AA1? I thought that the .left .top method would work. The bolded part of the following code is what I'm talking about. Thanks for the help!

    Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    
    Sub GenerateField()
    Dim ColorInteger As Long
    Dim CurrentCell As Range
    Dim pic As Object
    Dim Rows As Integer
    Dim Columns As Integer
    Dim cnt As Integer
    
    cnt = 0
    For Each pic In ActiveSheet.Pictures
    cnt = cnt + 1
    Next pic
        
    If cnt = 0 Then
    MsgBox "Error: You must select an image first"
        
    Else
    Rows = UserForm1.TextBox1.Value
    Columns = UserForm1.TextBox2.Value
        
    For i = 0 To Rows - 1
        For j = 0 To Columns - 1
        Set CurrentCell = ActiveSheet.Cells(1, 27).Offset(i, j)
        x = CurrentCell.Left + CurrentCell.Width / 2
        y = CurrentCell.Top + CurrentCell.Height / 2
        
        hDC = GetDC(hWnd)
        ColorInteger = GetPixel(hDC, x, y)
        
        R = ColorInteger And 255
        G = ColorInteger \ 256 And 255
        B = ColorInteger \ 256 ^ 2 And 255
        
        CurrentCell.Interior.Color = RGB(R, G, B)
        Next j
    Next i
        
    For Each pic In ActiveSheet.Pictures
    pic.Delete
    Next pic
        
    CurrentCell.Select
    End If
    End Sub
    Last edited by TheSpriceEffect; 07-09-2014 at 10:55 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA to find a cell-position with text
    By olwy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2014, 06:53 AM
  2. Find Position of a Character in a Cell
    By daviieejay in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 11-30-2013, 11:08 PM
  3. [SOLVED] Find position of the first lowercase in cell
    By jopelou in forum Excel General
    Replies: 9
    Last Post: 01-09-2013, 11:33 AM
  4. Find Cell position in the column based on the value
    By kiran527 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-21-2012, 11:47 AM
  5. How to find the pixel length of a string
    By johnjohns in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-18-2011, 10:01 AM

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