Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1604to1608
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Text in Zelle schreiben mit wechselnden Variablen

Text in Zelle schreiben mit wechselnden Variablen
29.01.2018 16:18:48
Tobias
Hallo,
ich habe hier schon einige Hilfe gefunden aber leider nicht zu dem Problem was ich gerade habe.
Ich möchte in eine aufeinanderfolgende anzahl von Zellen einen Text schreiben und an diesen Text jeweil noch ein Indize anhängen.
Also so was wie:
Originaltext = Test
In die Zellen rechts daneben und darunter soll Test1, Test2, Test3 usw. geschrieben werden.
Das indize ist auch eine Variable die sich je nach Datenblock ändert.
Das habe ich bisher erstellt.
Leider habe ich absolut keine Ahnung wie ich VBA beibrigen soll den Text jeweils mit dem Inhalt der Variablen ALS TEXT! in einen andere Zelle zu scheiben.
-----------------------------------------------------------------------

Sub AllowSorting()
Dim MaxLines As Integer
Dim iSortBlocs As Integer
Dim A As Integer
Dim B As Integer
Dim iLinesMain As Integer
Dim iLines

Sub As Integer
Application.ScreenUpdating = False
MaxLines = Cells(94, 8).Value 'Set variable "MaxLines" to the value written in Row 8, line 94
For iSortBlocs = 100 To MaxLines 'Use the index "iSortBlocs" and start at 100 until the value   _
_
of "MaxLines" and do the following
If Cells(iSortBlocs, 21) > 0 Then ' Check if in this cell is any value greater than 0
A = Cells(iSortBlocs, 21).Value 'If there is a vlaue >0 then set the variable A to this  _
value
For B = 0 To A 'Use the index "B" and start at 0 until the value of "A" and do the  _
following
AktiveSheet.Cells(iSortBlocs + B, 22) = Cells(iSortBlocs, 7).Value + B + 1 ' Write in   _
_
this cell the text written in row 7 and add the value of B + 1 as Text
Next 'Add 1 to B
End If
Next 'Add 1 to iSortBlocs
Application.ScreenUpdating = True
End Sub

-----------------------------------------------------------------------
Danke für das Entfernen des Bretts vor meinem Kopf :-)

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
Sub As Integer ? owT
29.01.2018 16:36:03
Matthias
AW: Text in Zelle schreiben mit wechselnden Variablen
29.01.2018 16:54:25
Peter(silie)
Hallo,
ich verstehe nicht wirklich was du meinst.
Wenn du immer einen weiter gehst, aber alles untereinander in die gleiche Spalte schreibst,
dann überschreibst du mit sicherheit auf kurz oder lang deine bereits aufgelisteten Daten.
Es wäre mit sehr geholfen wenn du eine Beispiel Mappe reinstellen würdest!
Hier eine Mappe mit Code: https://www.herber.de/bbs/user/119374.xlsm
Hier dein Code etwas abgeändert, sollte aber das gleiche tun:
Option Explicit
Public Sub AllowSorting()
Dim maxIteration As Long, sortBlocs As Long
Dim rng As Range
Dim i As Long
If Not IsNumeric(Cells(94, 8).Value) Then Exit Sub
maxIteration = Cells(94, 8).Value
'Loop till maximum iterations are reached
For sortBlocs = 100 To maxIteration
'check if the cell value is numeric
If IsNumeric(Cells(sortBlocs, 21).Value) Then
'let i be the cell value,
'else we always have to reference an object
'that will take up memory and is simply bad practice
i = Cells(sortBlocs, 21).Value
'this could also be done at the beginning of the code
'but here we are...
With ActiveSheet
'create a range object
'we will need this so the autofill object will work
Set rng = .Range(.Cells(sortBlocs, 22), .Cells(sortBlocs, 22))
'set the value and add 2 leading zeros defined as string
rng(1, 1).Value = .Cells(sortBlocs, 7).Value & "00"
'now use the autofill object which is already onboard
'this will keep us from using a nested loop
'nested loops are nasty
rng.AutoFill Destination:=rng.Resize(i), Type:=xlFillDefault
End With
End If
Next sortBlocs
End Sub

Anzeige
AW: I am going to Evisa! :-)
29.01.2018 17:35:44
Gerd
Moin!
Sub Allowing()
Dim A As Long
Dim B As Long
For A = 100 To Cells(94, 8).Value
If IsNumeric(Cells(A, 21).Text) Then
If Cells(A, 21) > 0 Then
For B = 0 To Cells(A, 21).Value
Cells(A + B, 22) = Cells(A, 7).Value & (B + 1)
Next
End If
End If
Next
End Sub

cu Gerd
AW: I am going to Evisa! :-)
30.01.2018 07:33:25
Tobias
Moin!
Perfekt! Die Antwort von GerdL war das was ich brauchte :-)
Das "AktiveSheet" und das & mit Klammer war das was falsch war.
So funktioniert der Code jetzt einwandfrei:
Sub AllowSorting()
Dim MaxLines As Integer
Dim iSortBlocs As Integer
Dim A As Integer
Dim B As Integer
Application.ScreenUpdating = False
MaxLines = Cells(94, 8).Value 'Set variable "MaxLines" to the value written in Row 8, line 94
For iSortBlocs = 100 To MaxLines 'Use the index "iSortBlocs" and start at 100 until the value  _
of "MaxLines" and do the following
If Cells(iSortBlocs, 21) > 0 Then ' Check if in this cell is any value greater than 0
A = Cells(iSortBlocs, 21).Value 'If there is a vlaue >0 then set the variable A to this  _
value
For B = 0 To A 'Use the index "B" and start at 0 until the value of "A" and do the  _
following
Cells(iSortBlocs + B, 22) = Cells(iSortBlocs, 7).Value & (B + 1) ' Write in this cell  _
the text written in row 7 and add the value of B + 1 as Text
Next 'Add 1 to B
End If
Next 'Add 1 to iSortBlocs
Application.ScreenUpdating = True
End Sub

Anzeige

339 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige