Re: Copy Column & Paste to Last Used Column
OMG! I think I've aged about 10 years!!! : D
Finally got it working though.
I figured out what was up with that error message you were getting. In my example, in Tabelle2, A1:A4 had data in it(to make sure that we didn't overwrite it). However, this meant I had failed to test it for the first 2 search results, which would get pasted into Columns A & B respectively.
If you use '.End(xlToRight)' on an empty range, or one with only 1 cell, then it jumps to the end of the row, rather than the last used cell. Therefore we need 2 special cases, 1 for if this is the 1st result (i.e. A1 is blank) and one for the 2nd result (i.e. B1 is blank).
I fixed this, and then started getting a wierd error - runtime error 9 (Subscript out of range). I managed to track this to the following line;
So I moved the sub 'Search' into the main Workbook module ("ThisWorkbook" aka "DeiseArbeitsmappe"). Prepend each call with 'ThisWorkbook.", and we're set;
[HR].[/HR]
In Tabelle1;
Private Sub CommandButton1_Click()
ThisWorkbook.Search ("SLOT_0_KARTE")
ThisWorkbook.Search ("SLOT_0_DIENST")
'...
End Sub
In DeiseArbeitsmappe;
Sub Search(ByVal strSrch As String)
Dim xFind, z As Integer
Dim rnge As Range
'MsgBox "in Suchfunktion !"
If strSrch = "" Then Exit Sub
With Sheets("Tabelle1")
Set xFind = .UsedRange.Find(What:=strSrch, After:=Range("A1"), _
LookIn:=xlValues, lookat:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False)
End With
If Not xFind Is Nothing Then
With Sheets("Tabelle2")
If .Range("A1").Value = "" Then
Set rnge = .Range("A1")
ElseIf .Range("B1").Value = "" Then
Set rnge = .Range("B1")
Else
Set rnge = .Range("A1").End(xlToRight).Cells(1, 2)
End If
xFind.EntireColumn.Copy Destination:=rnge
End With
Else
MsgBox "Suchbegriff wurde nicht gefunden !"
End If
End Sub
Display More
If this doesn't work now, I'll end up bald from pulling my hair out! : D