使用工作表名称作为变量,但具有刷新功能

时间:2018-04-06 16:17:52

标签: excel vba excel-vba excel-formula formulas

0。!

我在这里寻求帮助来修复,我认为,这是一个简单的问题。

在工作表中,我指的是工作表中名为“Consolidado”的一些单元格,但是,由于某些代码,工作表(“Consolidado”)被删除并再次使用新数据创建,因为它,我每次运行代码时,在另一个工作表的公式中得到“#REF”错误。有没有办法“修复”,强制它在公式的参考中是“Consolidado”?

这是公式。

#REF应该是"Consolidado"

PROC(2;1/--(#REF!$J$2:$J$30<>"");#REF!$J$2:$J$30)

再次欢迎任何帮助,谢谢!

====== EDIT ==========

我用来“刷新”工作表“Consolidado”的宏如下:

每次按下“Consolidar”按钮时,此宏都会创建一个新摘要。但是,在创建新工作表之前,它会销毁旧工作表以确保数据是新的。

Sub Consolidar_Abas()
    Dim Sh As Worksheet
    Dim Newsh As Worksheet
    Dim myCell As Range
    Dim ColNum As Integer
    Dim RwNum As Long
    Dim Basebook As Workbook

    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'Delete the sheet "Consolidado" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ThisWorkbook.Worksheets("Consolidado").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "Consolidado"
    Set Basebook = ThisWorkbook
    Set Newsh = Basebook.Worksheets.Add
    Newsh.Name = "Consolidado"

    'Add headers
    Newsh.Range("A1:AH1").Value = Array("Consolidado", "Carteira", "Segmento", "QTD Estagiário", "QTD CLT", "QTD Coordenador", "QTD Supervisor", "QTD BKO", "Prêmio & Comissões", "Receita Bruta Prevista", "Imposto", "Receita Líquida (-Imposto)", "Pessoal (OPs Carteira)", "Holding Carteira (Sup+Coord+BKO)", "Postagem & Impressos", "SMS", "Telefonia", "Internet Dedicada", "Softwares Dedicados", "Custo Extra", "Internet", "Softwares & Ferramentas", "Custo Total de Produção", "Lucro / Perda Prod. - Líquido", "Margem com Rec. Líquida", "Adm Holding", "Desp. Terceiros / Produção", "Tecnologia", "Manutenção", "Admistração", "Custo Empresarial Total", "Custo Total Real Final", "Lucro / Perda Final", "Margem com Rec. Líquida")

    'The links to the first sheet will start in row 2
    RwNum = 1

    For Each Sh In Basebook.Worksheets
        If Sh.Name <> Newsh.Name And Sh.Visible And Sh.Name <> "Menu" And Sh.Name <> "Infos" And Sh.Name <> "Master" Then
            ColNum = 1
            RwNum = RwNum + 1

    'Copy the sheet name in the A column
    Newsh.Cells(RwNum, 1).Value = Sh.Name
    For Each myCell In Sh.Range("A2:H2,J2:L2,A7:M7,A12:F12,H12,J12:K12")  '<--Change the range
            ColNum = ColNum + 1
            Newsh.Cells(RwNum, ColNum).Formula = _
            "='" & Sh.Name & "'!" & myCell.Address(False, False)
            Next myCell

        End If
    Next Sh

    Newsh.UsedRange.Columns.AutoFit

    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With


End Sub

在另一张名为“菜单”的工作表中,我提供了工作簿,其中有规则以及其他关于如何操作工作簿和类似内容的文本,在“信息”中也是如此,只是一堆其他工具。

在“菜单”上,用户可以创建一个新表单,在填写表单后,将填充表单“Consolidado”,每次按下“Criar Nova Aba”按钮,都会创建一个新表单(I只需从要填写的工作表“Master”创建一个副本。

在他按照自己的意愿完成了许多工作表的创建后,他按下“Consolidar”按钮,删除旧的并创建一个新的“Consolidar”工作表。

我想我用这个涵盖了工作簿的功能。我会尝试制作一个空白版本,不幸的是,这是工作中的,并且有敏感的数据。

编辑2

我现在需要的是细胞,各自的公式:

表:“菜单”;细胞:AY4

=PROC(2;1/--(Consolidado!$J$2:$J$30<>"");Consolidado!$J$2:$J$30)

表:“菜单”;细胞:AY5

=PROC(2;1/--(Consolidado!$AF$2:$AF$30<>"");Consolidado!$AF$2:$AF$30)

表:“菜单”;细胞:AY6

=(PROC(2;1/--(Consolidado!$X$2:$X$30<>"");Consolidado!$X$2:$X$30))/(PROC(2;1/--(Consolidado!$L$2:$L$30<>"");Consolidado!$L$2:$L$30))

表:“菜单”;细胞:AY7

=(PROC(2;1/--(Consolidado!$AG$2:$AG$30<>"");Consolidado!$AG$2:$AG$30))/(PROC(2;1/--(Consolidado!$J$2:$J$30<>"");Consolidado!$J$2:$J$30))

还有别的,请问。再次感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我刚试过一些似乎有用的东西......

Sheets("").Columns("A").Replace What:="#REF!", Replacement:="Consolidado!", SearchOrder:=xlByColumns, MatchCase:=True

确保更改列并添加工作表参考。

答案 1 :(得分:0)

您要查找的工作表是否具有一组唯一的列标题? 打开电子表格进行处理后,您可以遍历表格查找Consolidado

    'Find if sheet exists
    vFound = "N"
    For ix = 1 To Sheets.Count
        If "Consolidado" = ActiveWorkbook.Sheets(ix).Name Then
            vFound = "Y"
        End If
    Next

如果工作表不存在,您可以使用相同的想法来查找标识列标题,只需确保您有正确的工作表:

    'Check column names of sheets to find desired sheet (note vFound is still "N")
    For ix = 1 To Sheets.Count
        ActiveWorkbook.Sheets(ix).Activate
        If "Name" = Range("A1:A1").Value And "URL" = Range("B1:B1").Value And "Date" = Range("C1:C1").Value And "Logon" = Range("D1:D1").Value Then
            vTotFound = "Y"
            vSheetName = ActiveWorkbook.Sheets(ix).Name
            Exit For
        End If
    Next

您可以使用vSheetName而不是Consolidado。