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
Bookmarks