- 25 Abr 2018 às 20:36
#32423
Bom dia, Boa tarde ou Boa noite, de acordo com a hora a que visualizem isto.
Estou a tentar colocar uma fórmula numa célula de de uma folha (planilha) de Excel através do VBA, mas sempre obtenho erro. e não consigo descobrir o defeito do programa. Aqui está o que fiz até agora:
Dim wbTarget As Workbook 'workbook where the data is to be copied
Dim wbThis As Workbook 'workbook from where the data is to be pasted
Dim strName As String 'name of the source sheet/ target workbook
Dim caminho As String 'path of books
Dim linha As Integer 'linha a tratar
Dim coluna As Integer
Dim coluna_a As Integer
Dim mes As String
Dim mes_a As String
Dim FormulaFinal As String
Dim resto_nome As String
Dim contribuinte As String
Dim contribuintef As Double
Private Sub Workbook_Open()
Range(“A3100”).Select
If Range(“A3100”).Value = “SIM” Then Exit Sub
servico = Range(“A3101”).Value
Set wbThis = ActiveWorkbook
wbThis.Sheets(“GESTÃO < 2998 Executados”).Select
strName = ActiveSheet.Name
mes = Month(Date)
If mes < 10 Then mes = “0” & mes
mes_a = (Month(Date)) - 1
If mes_a < 10 Then mes_a = “0” & mes_a
Application.DisplayAlerts = False
caminho = Application.ActiveWorkbook.Path
Set wbThis = ActiveWorkbook
'ActiveWorkbook.ExclusiveAccess
resto_nome = "CARTEIRA DE GESTAO " & servico & “_” & mes_a & “.xlsm”
'############################################
FormulaFinal = “=ÍNDICE(’” & caminho & “[” & resto_nome & “]” & strName
FormulaFinal = FormulaFinal & “’!$A$1:$BZ$3100;CORRESP($B2;’” & caminho & “[” & resto_nome & “]” & strName
FormulaFinal = FormulaFinal & “’!$B:$B;0);CORRESP(”“HISTÓRICO”";’"
FormulaFinal = FormulaFinal & caminho & “[” & resto_nome & “]” & strName & “’!$1:$1;0))”
mensagem = MsgBox("A fórmula é " & FormulaFinal, vbOKOnly)
coluna = Application.Match(“HISTÓRICO”, Rows(1), 0)
linha = 2
Sheets(strName).Select
Sheets(strName).Activate
Range("AQ2").Select
Range("AQ2").Activate
ActiveSheet.Cells(linha, coluna).FormulaR1C1 = FormulaFinal
End sub
Alguém me consegue ajudar?
Obrigado antecipadamente
Manbat
Estou a tentar colocar uma fórmula numa célula de de uma folha (planilha) de Excel através do VBA, mas sempre obtenho erro. e não consigo descobrir o defeito do programa. Aqui está o que fiz até agora:
Dim wbTarget As Workbook 'workbook where the data is to be copied
Dim wbThis As Workbook 'workbook from where the data is to be pasted
Dim strName As String 'name of the source sheet/ target workbook
Dim caminho As String 'path of books
Dim linha As Integer 'linha a tratar
Dim coluna As Integer
Dim coluna_a As Integer
Dim mes As String
Dim mes_a As String
Dim FormulaFinal As String
Dim resto_nome As String
Dim contribuinte As String
Dim contribuintef As Double
Private Sub Workbook_Open()
Range(“A3100”).Select
If Range(“A3100”).Value = “SIM” Then Exit Sub
servico = Range(“A3101”).Value
Set wbThis = ActiveWorkbook
wbThis.Sheets(“GESTÃO < 2998 Executados”).Select
strName = ActiveSheet.Name
mes = Month(Date)
If mes < 10 Then mes = “0” & mes
mes_a = (Month(Date)) - 1
If mes_a < 10 Then mes_a = “0” & mes_a
Application.DisplayAlerts = False
caminho = Application.ActiveWorkbook.Path
Set wbThis = ActiveWorkbook
'ActiveWorkbook.ExclusiveAccess
resto_nome = "CARTEIRA DE GESTAO " & servico & “_” & mes_a & “.xlsm”
'############################################
FormulaFinal = “=ÍNDICE(’” & caminho & “[” & resto_nome & “]” & strName
FormulaFinal = FormulaFinal & “’!$A$1:$BZ$3100;CORRESP($B2;’” & caminho & “[” & resto_nome & “]” & strName
FormulaFinal = FormulaFinal & “’!$B:$B;0);CORRESP(”“HISTÓRICO”";’"
FormulaFinal = FormulaFinal & caminho & “[” & resto_nome & “]” & strName & “’!$1:$1;0))”
mensagem = MsgBox("A fórmula é " & FormulaFinal, vbOKOnly)
coluna = Application.Match(“HISTÓRICO”, Rows(1), 0)
linha = 2
Sheets(strName).Select
Sheets(strName).Activate
Range("AQ2").Select
Range("AQ2").Activate
ActiveSheet.Cells(linha, coluna).FormulaR1C1 = FormulaFinal
End sub
Alguém me consegue ajudar?
Obrigado antecipadamente
Manbat