[英]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. 在工作表中,我指的是工作表中一些名为“ Consolidado”的单元格,但是由于某些代码,工作表(“ Consolidado”)被删除并使用新数据再次创建,并且每次我运行代码时,都会在另一个工作表的公式中收到“ #REF”错误。 Is there a way to "fix", force it to be "Consolidado" in the reference for the formula? 有没有一种方法可以“修复”,在公式的引用中强制其为“ Consolidado”?
This is the formula. 这是公式。
Where #REF
should be "Consolidado"
. 其中#REF
应该为"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: 下面是我用来“刷新”工作表“ Consolidado”的宏:
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. 在“菜单”上,用户可以创建一个新表单,填写该表单后,将在每次单击“ Criar Nova Aba”按钮时填充“ Consolidado”工作表,将创建一个新表单(我只是创建了一个从表格“ Master”中复制)进行填写。
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. 然后,在完成所需的多个工作表的创建之后,他按“ Consolidar”按钮,删除了旧的工作表并创建了一个新的“ Consolidar”工作表。
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 编辑2
What I'm needing now, on the cells, the respective formula: 在单元格上,我现在需要的是各自的公式:
Sheet: "Menu" ; 工作表:“菜单”; Cell:AY4 单元格:AY4
=PROC(2;1/--(Consolidado!$J$2:$J$30<>"");Consolidado!$J$2:$J$30)
Sheet: "Menu" ; 工作表:“菜单”; Cell:AY5 单元格:AY5
=PROC(2;1/--(Consolidado!$AF$2:$AF$30<>"");Consolidado!$AF$2:$AF$30)
Sheet: "Menu" ; 工作表:“菜单”; Cell:AY6 单元格: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 单元格: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 打开电子表格进行处理后,您就可以遍历工作表以查找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. 您可以使用vSheetName代替Consolidado。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.