Erro indefinido de sub ou função ao tentar concatenar várias strings em uma célula em VB

Erro indefinido de sub ou função ao tentar concatenar várias strings em uma célula em VB

Eu estava tentando fazer uma ferramenta rápida hoje no trabalho para automatizar algumas tarefas no trabalho, mas me deparei com o problema de obter erros de Sun ou Function. Sou um novato absoluto em VB e não estou familiarizado com a sintaxe e/ou especificidades da linguagem. Você poderia por favor me ajudar? Detalhes sobre o que estou tentando fazer: Basicamente, tenho vários grupos (apenas 4 células em cada grupo) que estou tentando copiar em uma única célula. Assim:

Grupo1: célula1 célula2 célula3 célula4

Grupo2: . . . 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: Sinto que 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 ortográfico corrigido

Responder1

Isso se deve a um erro ortográfico nesta linha em gbeging: -

O 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))

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))

Responder2

A causa mais provável do seu erro são as referências não qualificadas a 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 isto não for raw_LSToolDataum erro resultará

O código correto é

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

Uma maneira melhor é

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

Observe os .'antes Rangee Cells- isso faz referência ao withobjeto da cláusula

informação relacionada