Última linha preenchida de uma determinada coluna

Boa noite pessoal!

Fiz uma pesquisa aqui no fórum antes de postar mas não achei algo que se encaixe no que eu queria. Gostaria de ajuda para criar uma Sub para determinar a última célula preenchida de uma determinada coluna. Os exemplos que eu encontrei aqui determina a última preenchida da planilha toda. No arquivo em anexo gostaria que determinasse a última célula preenchida na coluna B, por isso deixei com menos registros nesta coluna. Agradeço qualquer ajuda.
Arquivo_teste.ods (10.6 KB)

Ola @LeandroRodrigues, a finalidade de achar a última, é para inserir dados na próxima vazia ?

Não, é para usar em uma outra função que achei aqui mesmo no fórum. Mas eu preciso determinar a última preenchida de uma determinada coluna.

doc = ThisComponent
sheet = doc.CurrentController.ActiveSheet
range = sheet.getCellRangeByName("B:B").queryContentCells(127)
ra = range(0).RangeAddress
		
'Ultima usada
cell = sheet.getCellByPosition(1, ra.EndRow)
MsgBox cell.AbsoluteName

'Siguiente libre
cell = sheet.getCellByPosition(1, ra.EndRow + 1)
MsgBox cell.AbsoluteName
3 Likes

Da forma abaixo também dá certo.

Function LastLinhaCliente
	Dim nLinhaCliente As Long
	oPlan2 = ThisComponent.Sheets(2)
	nLinhaCliente = LastLinhaCliente
	nLinhaCliente = fServico2.CallFunction("COUNTA" , array(oPlan2.getCellRangeByName("B:B")))
	LastLinhaCliente = nLinhaCliente 
End Function

fServico2 eu defini no início do documento, aqui funcionou pois o arquivo que você tem não está totalmente atualizado.

Capturar

Continua mesrmo erro…

sub UltimaUsada 'source Mauricio Baeza
 sheet = ThisComponent.CurrentController.ActiveSheet            '' PARA PLANILHA ATIVA
'sheet = ThisComponent.getSheets.getByName( "Planilha")         '' PARA PLANILHA EXPECIFICA
range = sheet.getCellRangeByName("B:B").queryContentCells(127)  '' B = NOME COLUNA
ra = range(0).RangeAddress
'Ultima usada
cell = sheet.getCellByPosition(1, ra.EndRow)
MsgBox cell.AbsoluteName
MsgBox "$B$" & ra.EndRow + 1
MsgBox ra.EndRow + 1
End sub
' PARA USAR COMO SUBMACRO 
sub UltimaUsada2 ( xPlan$, xCol$ ) 'source Mauricio Baeza
'' exemplo de uso: UltimaUsada2  "Planilha", "B"
sheet = ThisComponent.getSheets.getByName( xPlan )  '' PARA PLANILHA EXPECIFICA
range = sheet.getCellRangeByName( xCol &":"& xCol ).queryContentCells(127)
ra = range(0).RangeAddress
'Ultima usada
cell = sheet.getCellByPosition(1, ra.EndRow)
MsgBox cell.AbsoluteName
MsgBox "$" & xCol & "$" & ra.EndRow + 1
MsgBox ra.EndRow + 1
End Sub
Sub ExemploDeUso
	UltimaUsada2 "Planilha1", "B"
End Sub
1 Like

Faltou adicionar a linha que defini fServico2

fServico2 = CreateUnoService("com.sun.star.sheet.FunctionAccess")

Segue arquivo teste
Arquivo_teste.ods (15.9 KB)

1 Like

Geralmente faço assim:

Const FCTACC = “com.sun.star.sheet.FunctionAccess”
'-----------------------------------------------------------------------------------------------------------
Dim fServico
Dim wb As Object
Dim ws As Object
Dim rng As String
Dim nLinha As Long

Sub BaseDados
wb = ThisComponent
ws = wb.Sheets.getByName (“Dados”)
rng = “B:B”

nLinha = uLinha ( ws , rng )
MsgBox nLinha
End Sub

Function uLinha ( ws , rng )
fServico = createUnoService ( FCTACC )
uLinha = fServico.CallFunction(“COUNTA” , array( ws.getCellRangeByName ( rng )))
End Function

1 Like