VBA Excel copy picture in Image ctrol to another Image ctrol in different userform

Thread Starter

atferrari

Joined Jan 6, 2004
5,029
VBA Excel
Two UserForms:

a) LayerAndFrameSettings with several Image controls. Amongst them: LED_1, LED_2, LED3, LED_4
b) Luminosity with five Image controls: Level_1, Level_2, Level_3, Level_4, Level_5

All data (single variables / arrays) is declared As Byte, enough for the values in play.

Code, based on external values is expected to copy the picture in one of the image controls at Luminosity
to one of the LED_X Image controls at LayerAndFrameSettings userform.

After extensive testing found that:

The following sentences do it properly
Rich (BB code):
    LayerAndFrameSettings.Controls("LED_4").Picture =  Luminosity.Controls("Level_" &  CStr(LayerLuminosityData(Pntr_LED))).Picture
    LayerAndFrameSettings.Controls("LED_2").Picture = Luminosity.Level_4.Picture
    LayerAndFrameSettings.Controls("Middle_square_change").Picture =  Luminosity.Controls("Level_" &  CStr(LayerLuminosityData(Pntr_LED))).Picture
    LayerAndFrameSettings.Controls("Inner_square_change").Picture = Luminosity.Controls("Level_4").Picture
    LayerAndFrameSettings.Controls("Middle_" &  "square_change").Picture = Luminosity.Controls("Level_" &  CStr(LayerLuminosityData(Pntr_LED))).Picture
    LayerAndFrameSettings.Controls("Inner_square_change").Picture = Luminosity.Controls("Level_4").Picture
    LayerAndFrameSettings.LED_3.Picture = Luminosity.Controls("Level_" & CStr(LayerLuminosityData(Pntr_LED))).Picture
BUT the following do not copy any picture at all and Excel does NOT raise any error at runtime
Rich (BB code):
    LayerAndFrameSettings.Controls("LED_" & CStr(Pntr_LED)).Picture = Luminosity.Level_2.Picture
    LayerAndFrameSettings.Controls("LED_" & CStr(Pntr_LED)).Picture =  Luminosity.Controls("Level_" &  CStr(LayerLuminosityData(Pntr_LED))).Picture
Just in case I checked in the worksheet, the values below and they display OK
Rich (BB code):
    Range("FY4").Value = "LED_" & CStr(Pntr_LED)
    Range("GB4").Value = "Level_" & CStr(LayerLuminosityData(Pntr_LED))
    Range("GD4").Value = LayerLuminosityData(Pntr_LED)
I am supposed to use a string to identify which control is receiving the picture, right?
 

panic mode

Joined Oct 10, 2011
5,295
i really don't understand why would anyone use such long lines of code. how do you even troubleshoot something like that? no wonder it is challenging to work with it, it does not even fit on screen one has to scroll or use line wrap just to see it. for example:
Rich (BB code):
LayerAndFrameSettings.Controls("Middle_" &  "square_change").Picture = Luminosity.Controls("Level_" &  CStr(LayerLuminosityData(Pntr_LED))).Picture
i strongly prefer making an arsenal of subs and functions (can be in separate module). I define and debug them once and then use them everywhere in my actual code. that makes life much easier and code is shorter, cleaner, easier to understand, troubleshoot, maintain... also it is a pleasure to test or manipulate in intermediate window.

if all you want is to set brightness of an LED, why not use something cute and short like:

Rich (BB code):
Call SetLed("LED13", 3)
 

Thread Starter

atferrari

Joined Jan 6, 2004
5,029
Hola Panic, just in case note that I changed the current names of the Image controls to "LED4", "LED5", "LED6" and so on.

In Módulo1

Rich (BB code):
Option Explicit

    Public j As Object
    Public Pntr_layer As Byte
    Public Pntr_row As Byte
    
    Public LayerLuminosityData(1 To 49) As Byte
    Public OffsetColFmActive(1 To 49) As Double
    Public OffsetRowFmActive(1 To 49) As Byte
    
    Public Ctrl_pressed As Boolean
    
    Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Const VK_CONTROL As Integer = &H11 'Ctrl


'THIS Sub WORKS FLAWLESSLY ALL THE TIME

        
Public Sub Transfer_layer_data_to_userform()
    Dim Pntr_LED As Byte
    
    For Pntr_LED = 1 To 49
        LayerLuminosityData(Pntr_LED) = ActiveCell.Offset(rowOffset:=OffsetRowFmActive(Pntr_LED), columnOffset:=OffsetColFmActive(Pntr_LED)).Value
        
        With LayerAndFrameSettings
            Select Case LayerLuminosityData(Pntr_LED)
                Case Is = 0
                    .Controls("LED" & CStr(Pntr_LED)).Picture = Luminosity.Level_0.Picture
                Case Is = 1
                    .Controls("LED" & CStr(Pntr_LED)).Picture = Luminosity.Level_1.Picture
                Case Is = 2
                    .Controls("LED" & CStr(Pntr_LED)).Picture = Luminosity.Level_2.Picture
                Case Is = 3
                    .Controls("LED" & CStr(Pntr_LED)).Picture = Luminosity.Level_3.Picture
                Case Is = 4
                    .Controls("LED" & CStr(Pntr_LED)).Picture = Luminosity.Level_4.Picture
            End Select
        End With
    Next Pntr_LED
End Sub
In LayerAndFrameSettings code module there are:

49 Calls like these below (three shown)

Rich (BB code):
Sub LED4_Click()
    Call Process_click_on_LED_image(4)
End Sub

Sub LED5_Click()
    Call Process_click_on_LED_image(5)
End Sub

Sub LED6_Click()
    Call Process_click_on_LED_image(6)
End Sub
calling this Sub (where the "offending line" is the very last).


Rich (BB code):
Public Sub Process_click_on_LED_image(Pntr_LED As Byte)
    Dim TargetCell As Object
    
    Call TestCTRLkey
    
    If Ctrl_pressed = True Then
        If LayerLuminosityData(Pntr_LED) > 0 Then
            LayerLuminosityData(Pntr_LED) = LayerLuminosityData(Pntr_LED) - 1
        End If
    Else
        If LayerLuminosityData(Pntr_LED) < 4 Then
            LayerLuminosityData(Pntr_LED) = LayerLuminosityData(Pntr_LED) + 1
        End If
    End If
    
    Set TargetCell = ActiveCell.Offset(OffsetRowFmActive(Pntr_LED), OffsetColFmActive(Pntr_LED))
    TargetCell.Value = LayerLuminosityData(Pntr_LED)
        
    Select Case TargetCell.Value
        Case Is = 0
            TargetCell.Interior.Color = RGB(202, 202, 202)
        Case Is = 1
            TargetCell.Interior.Color = RGB(67, 199, 130)
        Case Is = 2
            TargetCell.Interior.Color = RGB(110, 210, 210)
        Case Is = 3
            TargetCell.Interior.Color = RGB(179, 231, 216)
        Case Is = 4
            TargetCell.Interior.Color = RGB(170, 244, 250)
    End Select
    
    Call Update_single_LED_data_in_block(Pntr_LED)
    
    'Tres datos a ser mostrados en la hoja
    
    Range("FY4").Value = "LED" & CStr(Pntr_LED)
    Range("GB4").Value = "Level_" & CStr(LayerLuminosityData(Pntr_LED))
    Range("GD4").Value = LayerLuminosityData(Pntr_LED)
    
    LayerAndFrameSettings.Controls("LED" + CStr(Pntr_LED)).Picture = Luminosity.Controls("Level_" & CStr(LayerLuminosityData(Pntr_LED))).Picture
 End Sub
 

Thread Starter

atferrari

Joined Jan 6, 2004
5,029
I added an extra Image control (LED410) and the line in bold.
Rich (BB code):
    LayerAndFrameSettings.LED410.Picture = Luminosity.Controls("Level_" & CStr(LayerLumiData(Pntr_LED))).Picture
    LayerAndFrameSettings.Controls("LED" & CStr(Pntr_LED)).Picture = LayerAndFrameSettings.LED410.Picture
The desired picture gets copied to LED410 but not the the desired destination.
Now I can say that what fails is in the last line.
 

Thread Starter

atferrari

Joined Jan 6, 2004
5,029
no wonder it is challenging to work with it,
Your words Panic. I understand my code and along the years I can revisit old one with little difficulties.

I feel at ease with long variable names if they are descriptive. Find them helpful.

Later when code was tested I shorten them within reason (but not much).

This guy probably could not team up with you, methinks:

Rich (BB code):
Private Sub ConnectionApp_WorkbookPivotTableOpenConnection(ByVal wbOne As Workbook, Target As PivotTable)

xlCommandUnderlinesAutomatic 

xlTickLabelOrientationDownward

xlXmlImportElementsTruncated
:D
 

Thread Starter

atferrari

Joined Jan 6, 2004
5,029
the most important thing is you solved it.

i guess you can call my preferences ... unique. :D
Hola Panic,

I've been criticized for that before but as I do it consistently is not a real problem to me. Being honest, I came to suspect that your criteria is the prevailing one everywhere.

I know I solved it but still do not know why it was not working. Could it be any particular setting of those Image controls? I run through them many times but nothing seems related to my problem.

I am not experient in VBA besides this intensive last two months where I learnt a LOT (with a much BIGGER LOT still to be discovered and learnt :p).
Gracias again for your help (and the time spent in replying).
 
Top