Excel VBA - после поиска диапазона, как положить в переменную - PullRequest
0 голосов
/ 06 декабря 2018

rng1 выдает ошибку, кто-нибудь знает как исправить?Я пытаюсь создать график, но мне нужно найти вторую часть диапазона для графика

Range("A1").Select

Cells.Find(What:="Sum(FRD End Analysis)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate

Set rng1 = ActiveCell.Offset(0, 1).Range("A1:E2")



'Your data range for the chart
  Set rng = ActiveSheet.Range("C1:G1,rng1")

'Create a chart
  Set cht = ActiveSheet.Shapes.AddChart

1 Ответ

0 голосов
/ 07 декабря 2018

Надеюсь, это решит проблему.Я предполагаю, что rng1 выдавал ошибку, потому что он не находил Sum (FRD End Analysis) .Я не уверен, хотя, поскольку вы не говорите, какую ошибку это выдает.

Это может быть: переменная не определена (если вы используете Option Explicit) или Переменная объекта или переменная блока не установлена ​​, поскольку FindЗаявление не нашло ничего или ряда других вещей, о которых я не подумал.

Sub Example()

    Dim wrkSht As Worksheet
    Dim rFound As Range
    Dim rng1 As Range
    Dim cht As ChartObject
    Dim DisplayRange As Range

    'The code needs to know which sheet you're talking about.
    'Not designating the sheet means it will look at the currently active sheet.
    Set wrkSht = ThisWorkbook.Worksheets("Sheet1")

    'The chart will be built to cover this range.
    Set DisplayRange = wrkSht.Range("D5:H20")

    'Try and find the cell and store it's reference in a range variable.
    'Note that it's using wrkSht variable to identify the sheet that cells are on.
    Set rFound = wrkSht.Cells.Find( _
        What:="SUM(FRD End Analysis)", _
        After:=wrkSht.Range("A1"), _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext)

    'If it hasn't been found then rFound will be nothing, otherwise it will not be nothing.
    If Not rFound Is Nothing Then

        'Here the cell 1 column to the right of rFound is considered to be cell "A1".
        'So if value is found in cell B2 then .Range("A1:E2") will reference cells C2:G3 (C2 is considered to be A1)
        Set rng1 = Union(wrkSht.Range("C1:G1"), rFound.Offset(, 1).Range("A1:E2"))

        'Create a chart container.
        Set cht = wrkSht.ChartObjects.Add _
            (DisplayRange.Left, DisplayRange.Top, DisplayRange.Width, DisplayRange.Height)

        'Build the chart within the chart container.
        With cht
            .Chart.ChartType = xlColumnClustered

            'Create the series from rng1.
            With .Chart.SeriesCollection.NewSeries
                .Name = "First Row"
                .XValues = rng1.Rows(1)
                .Values = rng1.Rows(2)
            End With

            With .Chart.SeriesCollection.NewSeries
                .Name = "Second Row"
                .XValues = rng1.Rows(1)
                .Values = rng1.Rows(3)
            End With

        End With

    End If

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...