Question

I am using the OFFSET Function to create a dynamic chart for the table depicted in the image below. Basically on the click of the button labeled "Copy Mean VCD Values" the code copies values from another sheet to the current sheet. If it encounters any cells with "#DIV/0!" I have it set to put "N/A" instead. But in this case I don't get a dynamic chart. If in stead of N/A I do "" it creates a dynamic chart but adds junk values "1" to the first set on the graph. I only get the desired results if I manually delete all the rows containing "N/A" below the last row containing data (See image for details).

https://lh6.googleusercontent.com/-OfjK6dSRQE8/U2JkdadjedI/AAAAAAAAABk/d8WDLuuC7Lk/w1068-h803-no/Offset+error.PNG

This is the code I am using for the Command Button "Copy Mean VCD Values":

    Private Sub CommandButton2_Click()

        r = 7
        '//j increments the column number
        '//i increments the row number
        '//r is used for taking values from alternate cells(sheet3 column K) rowwise
        For j = 2 To 14
            For i = 7 To 26
                If ThisWorkbook.Sheets(3).Range("K" & r & "").Text = "#DIV/0!" Then
                    ThisWorkbook.Sheets(2).Cells(i, j).Value = "N/A"
                    Else
                    ThisWorkbook.Sheets(2).Cells(i, j).Value = ThisWorkbook.Sheets(3).Range("K" & r & "").Value
                End If
             r = r + 2
            Next i
         Next j


     End Sub

If I add the following code it works but it deletes the entire rows before and after the table. See image : https://lh6.googleusercontent.com/-WiM8HN61zkM/U2Jz2J_JxjI/AAAAAAAAACw/z4i3hlakyAI/w1598-h442-no/offset+delete+row.PNG

    Private Sub CommandButton2_Click()

        r = 7
        '//j increments the column number
        '//i increments the row number
        '//r is used for taking values from alternate cells(sheet3 column K) rowwise
        For j = 2 To 14
            For i = 7 To 26
                If ThisWorkbook.Sheets(3).Range("K" & r & "").Text = "#DIV/0!" Then
                    ThisWorkbook.Sheets(2).Cells(i, j).Value = "N/A"
                    Else
                    ThisWorkbook.Sheets(2).Cells(i, j).Value = ThisWorkbook.Sheets(3).Range("K" & r & "").Value
                End If
             r = r + 2
            Next i
         Next j

Dim c As Range
Dim SrchRng

Set SrchRng = ActiveSheet.Range("B7:B26")
Do
    Set c = SrchRng.Find("N/A", LookIn:=xlValues)
    If Not c Is Nothing Then c.EntireRow.Value = ""
Loop While Not c Is Nothing

   End Sub

And this is OFFSET Function I am using for Column B in this case:

    =OFFSET('Data Summary Template'!$B$7,0,0, COUNTA('Data Summary Template'!$B$7:$B$26),1)
Was it helpful?

Solution

I can't exactly replicate this problem you're having... I think the initial problem must have something to do with the warning regarding invalid references. You should look in to that and figure out the cause, which is probably the cause of "extra" data in your chart.

If deleting the N/A values appears to be working, try something. Instead of:

Set SrchRng = ActiveSheet.Range("B7:B26")
Do
    Set c = SrchRng.Find("N/A", LookIn:=xlValues)
    If Not c Is Nothing Then c.EntireRow.Value = ""
Loop While Not c Is Nothing

Do this:

Dim tblCell as Range
Dim tbl as ListObject
Set tbl = ActiveSheet.ListObjects(1)
For each tblCell in tbl.DataBodyRange.Columns(2).Cells
    If tblCell.Value = "N/A" Then 
        tblCell.Resize(1, tbl.DataBodyRange.Columns.Count - 1).Value = vbNullString
    End If
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top