简体   繁体   中英

Using worksheet name as variable, but with refresh ability

O.!

I'm here for help to fix, what i think, it's a simple problem.

In a worksheet, I'm referring to some of the cells in the worksheet that is named, "Consolidado", but, because of some code, the sheet("Consolidado") gets deleted and is created again with new data, and because of it I get the "#REF" error in the formula on another worksheet, every time i run the code. Is there a way to "fix", force it to be "Consolidado" in the reference for the formula?

This is the formula.

Where #REF should be "Consolidado" .

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

ANY help is welcomed, again, thanks in adavance!

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

The macro I use to "refresh" the sheet "Consolidado" follows bellow:

This macro creates a new summary every time a hit the button "Consolidar". But, before creating the new one, it destroy the old sheet to be sure the data is new.


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

In another sheet, called "Menu" I present the workbook, there are rules there and other text on how to operate de workbook and similar stuff also, the same in "Infos", just a bunch of other tool.

On "Menu" where the user can create a new form, that, after he fills it, will populate the sheet "Consolidado", every time he hits the button "Criar Nova Aba", will create a new form (I just creates a copy from the sheet "Master") to be filled.

And after he finish the creation of many sheet as he want, he presses the "Consolidar" button, deleting the old one and creating a new "Consolidar" sheet.

I guess with this i cover the functioning of the Workbook. I'll try to make a blank version, unfortunately, it's from work and has sensitive data.

EDIT 2

What I'm needing now, on the cells, the respective formula:

Sheet: "Menu" ; Cell:AY4

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

Sheet: "Menu" ; Cell:AY5

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

Sheet: "Menu" ; Cell: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))

Sheet: "Menu" ; Cell: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))

Anything else, just ask. Thanks again for all the help!

I just tried something which appears to work...

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

Make sure to change the column and the add a sheet reference.

Does the sheet you're looking for have a unique set of column headers? Once you open the spreadsheet to process you can loop through the sheets looking for 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

If the sheet is not there you could utilize the same idea to look for identifying column headers, just enough to be sure you have the correct sheet:

    '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

You can use vSheetName instead of Consolidado.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM