¿Cómo agrego una celda con un valor de fórmula?

I'm trying to work a little macro that will format some data so that I can make a price sheet. When I put the information in excel, it works fine, but some of the notes make it onto the next line. This is supposed to take the cell contents and append the cell above it with them.

Sub Macro1()
    For i = 8 To 100000
        If Left(ActiveSheet.Cells(i + 1, 1).Value, 2) = "AC" Or ActiveSheet.Cells(i + 1, 1).Value  = vbNullString Then
        Else
            If Left(ActiveSheet.Cells(i + 1, 1).Value, 4) = "Page" Then
                ActiveSheet.Rows(i + 1).Delete shift:=xlUp
            Else
                Dim tempString As Variant
                tempString = ActiveSheet.Cells(i + 1, 1).Value
                ActiveSheet.Cells(i, 1).Value = ActiveSheet.Cells(i, 1).Value + " " + tempString
                ActiveSheet.Rows(i + 1).Delete shift:=xlUp
                i = i - 1
            End If
        End If
    Next
End Sub

It works just fine until I run into a cell value:

=- 4-7

(This isn't really a formula, it just looks like one) which gives me a type mismatch error. Should I be using another way to specify cell contents? Also, there's probably a better way to specify the range, I really just want to go all the way down column A. Any help appreciated.

preguntado el 09 de marzo de 12 a las 23:03

I can't replicate your error? Can you post a sample somewhere? Mods I would make: 1) I would delete the "Page" cells with an autofilter at the start of the code 2) Set the range in column A looking up from the last cell rather than hard-coding 100000 3) Delete bottom up means you can remove your i=i-1 fix -

+1) All 3 good points! Even I would recommend that. Here is the 4th Point: Use "&" instead of "+" when you concatenate strings in Excel -

Thank you! This is great help, and I'm happy to say that the sub is working fine! -

1 Respuestas

Sólo tiene que utilizar .Formula en lugar de .Value

This will return a string for a constant where it is a constant, or a string for the "formula" that excel thinks that it is, as appropriate.

respondido 10 mar '12, 06:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.