Erro Sub ou Function undefined ao tentar concatenar várias strings em uma célula no VB

0

Eu estava tentando fazer uma ferramenta rápida hoje no trabalho para automatizar algumas tarefas no trabalho, mas corri para o problema de obter o erro Sun ou Function. Eu sou um novato absoluto em VB e não está familiarizado com a sintaxe e ou linguagem específica. Podes ajudar-me, por favor? Detalhes sobre o que estou tentando fazer: Basicamente, eu tenho vários grupos (apenas 4 células em cada grupo) que estou tentando copiar em uma única célula. Assim:

Grupo1: cell1 cell2 cell3 cell4

Grupo 2: . . . Cada um desses grupos precisa ser copiado em células separadas.

Aqui está o código:

Public Sub GlobalConcatenation()
    Dim sourcerange As Range
    Dim gbegin As Integer
    Dim gend As Integer
    gbegin = 2
    gend = 5
    sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbegin), Cells(2, gend))
    Dim i As Integer
    For i = 2 To 50
        callConcatinateAllCellValuesInRange (sourcerange)

        sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbegin + 4), Cells(2, gend + 4))
End Sub
Function ConcatinateAllCellValuesInRange(sourcerange As Excel.Range) As String
    Dim finalValue As String

    Dim cell As Excel.Range

    For Each cell In sourcerange.Cells
        finalValue = finalValue + CStr(cell.Value)
    Next cell

    ConcatinateAllCellValuesInRange = finalValue
End Function

EDIT: Eu sinto que eu também preciso especificar que o problema aparece na função GlobalConcatenation () e sempre que recebo o erro, "sourcerange" está sendo destacado.

EDIT: atualizou o código - erro de ortografia fixo

    
por GKED 27.04.2012 / 07:14

2 respostas

0

Isso se deve ao erro de ortografia nesta linha em gbeging : -

Errado: -

sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbeging), Cells(2, gend))

sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbeging + 4), Cells(2, gend + 4))

Um correto: -

sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbegin), Cells(2, gend))

sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbegin + 4), Cells(2, gend + 4))
    
por 27.04.2012 / 08:02
0

A causa mais provável do seu erro são as referências não qualificadas às células nas linhas, como

sourcerange = Sheets("raw_LSToolData").Range(Cells(2, gbegin), Cells(2, gend))

Cells(2, gbegin) refere-se a uma célula na planilha ativa. Se isso não for raw_LSToolData , ocorrerá um erro

O código correto é

sourcerange = Sheets("raw_LSToolData").Range(Sheets("raw_LSToolData").Cells(2, gbegin), Sheets("raw_LSToolData").Cells(2, gend))

A melhor maneira é

with Sheets("raw_LSToolData")
    sourcerange = .Range(.Cells(2, gbegin), .Cells(2, gend))
end with

Observe o . antes de Range e Cells - isso faz referência ao objeto da cláusula with

    
por 27.04.2012 / 14:39

Tags