1

I have a problem. I need to write the macro which select the values in column E. Values of selected items should be between values in cell T2 and U2. After selection, macro should draw the chart.

I tried 3 ways:

First Approach:

Sub wykres1()  
    Dim rng As Range
    Dim cell As Range 

    Set rng = Range("E1", Range("E65536").End(xlUp))  

    For Each cell In rng  

    If cell.Value > "T2" and cell.value < "U2" Then Cell.Select    
        With Selection    
            ActiveSheet.Shapes.AddChart2    
        End With    
    Next cell    
End Sub

Wykres1 Doesn't work, because the line with if is highlighted on red.

Second Approach:

Sub wykres2()    
    Dim rng As Range    
    Dim cell As Range    

    Set rng = Range("E1", Range("E65536").End(xlUp))    

    For Each cell In rng    
        If cell.Value > ActiveSheet.Cell(2,20).Value and cell.value < ActiveSheet.Cell(2,21).Value Then Cell.Select    
        With Selection    
            ActiveSheet.Shapes.AddChart2    
        End With    
    Next cell
End Sub

Wykres2 Doesn't work, because the line with if is highlighted on red.

Third Approach:

Sub wykres3()     
    Dim rng As Range
    Dim cell As Range

    Set rng = Range("E1", Range("E65536").End(xlUp))

    For Each cell In rng
        If cell.value > -35 And cell.value < -32 Then cell.Select
        With Selection
            ActiveSheet.Shapes.AddChart2    
        End With
    Next cell
End Sub

Wykres3 freeze after run. When I remove the part with draw chart, the macro select one cell not the range with selected values. And here I put the values in macro (-35) (-32) - but I'm interested in possibility to put values from cells (T2) (U2).


As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.

Thank You for Your help.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Adorovsky
  • 28
  • 6

1 Answers1

1

Try this (Untested). avoid the use of .Select. Work with objects. You may want to see How to avoid using Select in Excel VBA

Sub wykres1()
    Dim rng As Range, cell As Range
    Dim lRow As Long, i As Long
    Dim ws As Worksheet

    '~~> Change as applicable
    Set ws = Sheet1

    With ws
        '~~> Find last row in Col E
        lRow = .Range("E" & .Rows.Count).End(xlUp).Row

        '~~> Loop though the range
        For i = 1 To lRow
            If .Range("E" & i).Value > .Range("T2").Value And _
               .Range("E" & i).Value < .Range("U2").Value Then
                With .Range("E" & i)
                    '
                    '~~> Do Something
                    '
                End With
            End If
        Next i
    End With
End Sub

As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.

You can store each range found above in one range object and then use that. See this example

Sub wykres1()
    Dim rng As Range, cell As Range
    Dim lRow As Long, i As Long
    Dim ws As Worksheet
    Dim Obj As ChartObject

    '~~> Change as applicable
    Set ws = Sheet1

    With ws
        '~~> Find last row
        lRow = .Range("E" & .Rows.Count).End(xlUp).Row

        '~~> Liip though the range
        For i = 1 To lRow
            If .Range("E" & i).Value > .Range("T2").Value And _
               .Range("E" & i).Value < .Range("U2").Value Then
                '~~> Store the cell in a range object
                If rng Is Nothing Then
                    Set rng = .Range("E" & i)
                Else
                    Set rng = Union(rng, .Range("E" & i))
                End If
            End If
        Next i

        '~~> Once you have the range, create a chart and assign range
        If Not rng Is Nothing Then
             With .ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
                .Chart.SetSourceData Source:=rng
                .Chart.ChartType = xlColumnClustered
            End With
        End If
    End With
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Hello. thank You for Your help, but when I run the macro it is a problem, because when i change from ws = sheet1 to ws = 336 (it is name of my sheet) the mismatch error is occured. Even i change the name of my sheet from 336 to sheet1 the macro is runing but nothing happens. – Adorovsky Jan 22 '19 at 09:42
  • 1
    `Sheet1` is the code name. If you want to use the sheet name then use it like `Set ws = Thisworkbook.Sheets("336")` – Siddharth Rout Jan 22 '19 at 09:43
  • 1
    Also I made a change to the above code. You may want to refresh the page to see it? – Siddharth Rout Jan 22 '19 at 09:44
  • Ok everything is good now. Thank You for Your help, You're awesome Sir !!! – Adorovsky Jan 22 '19 at 09:49