Avatar billede vegaz Juniormester
31. marts 2015 - 11:48 Der er 1 løsning

VBA: Tilføje tekst til en tekst der bliver overført til ny workbook

Hej :)

Jeg importerer data fra en workbook til en anden. Jeg ønsker at tilføje noget tekst til den tekst som jeg overfører.

Så data fra kolonne F bliver overført til kolonne T i den nye workbook. Det starter i T13:Tlastrow.

Så mit output skulle gerne blive: {nyckel="TEKST FRA F HER";}
Jeg sidder fast her, den overfører kun dette til den sidste celle i T og ikke til alle dem jeg overfører.

Sub CopyData()
    Dim wkbCurrent As Workbook, wkbNew As Workbook
    Set wkbCurrent = ActiveWorkbook
    Dim valg, c, LastCell As Range
    Set valg = Selection
    Dim wkbPath, wkbFileName, lastrow As String
    Dim LastRowInput As Long
    Dim lrow, rwCount, lastrow2, LastRowInput2 As Long

    Application.ScreenUpdating = False

    ' If nothing is selected in column A
    If Selection.Columns(1).Column = 1 Then

        wkbPath = ActiveWorkbook.Path & "\"
        wkbFileName = Dir(wkbPath & "CIF LISTEN.xlsm")

        Set wkbNew = Workbooks.Open(wkbPath & "CIF LISTEN.xlsm")

        'Application.Run ("'C:\Users\niclas.madsen\Desktop\TEST\CIF LISTEN.xlsm'!DelLastRowData")
        LastRowInput = Cells(Rows.count, "A").End(xlDown).Row

        For Each c In valg.Cells
            lrow = wkbNew.Worksheets(1).Range("B1").Offset(wkbNew.Worksheets(1).Rows.count - 1, 0).End(xlUp).Row + 1

            lastrow2 = Range("A" & Rows.count).End(xlUp).Row
            lastrow3 = Range("T" & Rows.count).End(xlUp).Row

            wkbCurrent.ActiveSheet.Range("E" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("A" & lrow)
            wkbCurrent.ActiveSheet.Range("A" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("B" & lrow)
            wkbCurrent.ActiveSheet.Range("F" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("T" & lrow)
            ' Standard inputs
            wkbNew.Worksheets(1).Range("D13:D" & lastrow2).Value = "Ange referens och period"
            wkbNew.Worksheets(1).Range("E13:E" & lastrow2).Value = "99999002"
            wkbNew.Worksheets(1).Range("G13:G" & lastrow2).Value = "EA"
            wkbNew.Worksheets(1).Range("H13:H" & lastrow2).Value = "2"
            wkbNew.Worksheets(1).Range("M13:M" & lastrow2).Value = "SEK"
            wkbNew.Worksheets(1).Range("N13:N" & lastrow2).Value = "sv_SE"
            wkbNew.Worksheets(1).Range("P13:P" & lastrow2).Value = "TRUE"
            wkbNew.Worksheets(1).Range("Q13:Q" & lastrow2).Value = "TRUE"
            wkbNew.Worksheets(1).Range("S13:S" & lastrow2).Value = "Catalog_extensions"

            'wkbNew.Worksheets(1).Range("T" & lastrow3).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & lastrow3).Value & ";}"
        Next

' Trying to get this to work
        LastRowInput2 = wkbNew.Worksheets(1).Range("T" & wkbNew.Sheets("Sheet1").UsedRange.Rows.count + 1).End(xlUp).Row

        For i = 0 To LastRowInput2 - 13

            wkbNew.Worksheets(1).Range("T" & 13 + i).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & 13 + i).Value & ";}"
        Next i
' END HERE

        ' wkbNew.Close False
        ' Find the number of rows that is copied over
        wkbCurrent.ActiveSheet.Activate
        areaCount = Selection.Areas.count
        If areaCount <= 1 Then
            MsgBox "The selection contains " & Selection.Rows.count & " suppliers."
            ' Write it in A10 in CIF LISTEN
            wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & Selection.Rows.count & " Suppliers Added"
        Else
            i = 1
            For Each A In Selection.Areas
                'MsgBox "Area " & I & " of the selection contains " & _
                    a.Rows.count & " rows."
                i = i + 1
                rwCount = rwCount + A.Rows.count
            Next A
            MsgBox "The selection contains " & rwCount & " suppliers."
            ' Write it in A10 in CIF LISTEN
            wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & rwCount & " Suppliers Added"
        End If

        wkbNew.Worksheets(1).Activate

        Application.ScreenUpdating = True

    Else
        MsgBox "Please select cell(s) in column A", vbCritical, "Error"
        Exit Sub
    End If
End Sub
Avatar billede vegaz Juniormester
30. maj 2015 - 02:16 #1
løst
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester