AW: Excel - VBA - Spalten eifügen - Felder abfragen
18.11.2020 11:44:57
Nepumuk
Hallo Beata,
teste mal:
Option Explicit
Public Sub Transformieren()
Dim lngItem1 As Long, lngItem2 As Long
Dim lngColumn As Long, lngCounter As Long
Dim strFirstNumber As String
Dim ablnFound(1 To 7) As Boolean
Dim objCell As Range
Application.ScreenUpdating = False
lngItem1 = Cells(3, 1).End(xlToRight).Column
lngItem2 = Cells(3, Columns.Count).End(xlToLeft).Column
strFirstNumber = Cells(3, lngItem1).Text
Call Rows(2).Copy(Destination:=Rows(1))
Call Cells(1, lngItem1).UnMerge
Call Range(Cells(3, lngItem1), Cells(3, lngItem2)).Copy(Destination:=Cells(1, lngItem1))
Call Rows("2:3").Delete
Call Range(Cells(1, 1), Cells(1, Columns.Count).End(xlToLeft)).BorderAround(LineStyle:=xlContinuous)
For lngColumn = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
Select Case UCase$(Cells(1, lngColumn).Text)
Case "FIRMA"
If Not ablnFound(1) Then
Cells(1, lngColumn).Value = "L_Name_1"
ablnFound(1) = True
Else
Cells(1, lngColumn).Value = "R_Name_1"
End If
Case "ORGANISATIONSEINHEIT / EMPFÄNGER"
If Not ablnFound(2) Then
Cells(1, lngColumn).Value = "L_Name_2"
ablnFound(2) = True
Else
Cells(1, lngColumn).Value = "R_Name_2"
End If
Case "ANSPRECHPARTNER"
If Not ablnFound(3) Then
Cells(1, lngColumn).Value = "L_Name_2"
ablnFound(3) = True
Else
Cells(1, lngColumn).Value = "R_Name_2"
End If
Case "ANSCHRIFT"
If Not ablnFound(4) Then
Cells(1, lngColumn).Value = "L_Straße"
ablnFound(4) = True
Else
Cells(1, lngColumn).Value = "R_Straße"
End If
Case "PLZ"
If Not ablnFound(5) Then
Cells(1, lngColumn).Value = "L_PLZ"
ablnFound(5) = True
Else
Cells(1, lngColumn).Value = "R_PLZ"
End If
Case "ORT"
If Not ablnFound(6) Then
Cells(1, lngColumn).Value = "L_Ort"
ablnFound(6) = True
Else
Cells(1, lngColumn).Value = "R_Ort"
End If
Case "AP-NR."
If Not ablnFound(7) Then
Cells(1, lngColumn).Value = "L_Ref."
ablnFound(7) = True
Else
Cells(1, lngColumn).Value = "R_Ref."
End If
End Select
Next
Call Range(Cells(1, lngItem1), Cells(1, lngItem2)).EntireColumn.Cut
Call Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Insert(Shift:=xlShiftToRight)
Set objCell = Rows(1).Find(What:=strFirstNumber, After:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole)
lngColumn = objCell.Column
Set objCell = Nothing
Do
lngCounter = lngCounter + 1
Call Columns(lngColumn + 1).Insert(Shift:=xlShiftToRight)
Call Range(Cells(2, lngColumn), Cells(Rows.Count, lngColumn).End(xlUp)).Copy(Destination:=Cells(2, lngColumn + 1))
Range(Cells(2, lngColumn), Cells(Rows.Count, lngColumn).End(xlUp)).Value = Cells(1, lngColumn).Value
Cells(1, lngColumn).Value = "ArtikelNummer" & CStr(lngCounter)
Cells(1, lngColumn + 1).Value = "ArtikelAnzahl" & CStr(lngCounter)
lngColumn = lngColumn + 2
Loop Until IsEmpty(Cells(1, lngColumn).Value)
Application.ScreenUpdating = True
End Sub
Gruß
Nepumuk