silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys,

I am struggling again gg../

Code:
Function MandatLoeschen(ByVal strText As String) As String
    Dim strSearch As String
    Dim varWoerter As Variant
    Dim lngCounter As Long

    
    Dim strMandat As String
    
    strSearch = "Mandatsnummer:"
    
    varWoerter = Split(strText, " ")
    
    For lngCounter = LBound(varWoerter) To UBound(varWoerter)
        If varWoerter(lngCounter) = strSearch Then
            MandatLoeschen = varWoerter(lngCounter) & " " & varWoerter(lngCounter + 1)
            
            
            MandatLoeschen = Replace(strText, MandatLoeschen, "")
            
        Else
     End If
    Next lngCounter
End Function

Following code works if I am not using it within my code..
When I run it from another code it delets the remaining records ..(
So I guess there need to be a redim preserve in it .. but in my case how does this one need to be??

Many thanks as always :)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi guys,

I am just not sure how to set this up properly..

Code:
    Mandat = Range("J2", Range("J1").End(xlDown))
    
    For i = LBound(Mandat, 1) To UBound(Mandat, 1)
        Mandat(i, 1) = MandatLoeschen(Mandat(i, 1))
    Next i
    Range("J2").Resize(UBound(Umsatztext, 1), 1) = Mandat

This is the other array how I like to run this above code so do I need to redim it on this code??

Many thanks for your help!! I am still a little struggling with arrays I am affraid :(
 
Upvote 0
What exactly is being deleted?

That function won't change anything unless you are assigning the value returned by the function to something else.
 
Upvote 0
Hi Norie,

Thanks for your input!!
It deletes all other cells in the column(J) which are not getting updatet.. So all cells where the array finds cells are remaining but all others which have no "mandatnummer" are getting deleted
So there must be the issure with the second code I have posted and there I guess need to be a redim preserve statement or am I wrong???
 
Upvote 0
Don't think it's anything to do with ReDim/Redim Preserve.

What is the function mean to do?
 
Upvote 0
Ok I try to explain it ,,,

The function split the string into seperate parts so varWoerter ... if the value in that cell is found then it replaces the value with an empty string..

So in the cell ist a text like " Mr Huber mandatsnummer: 121213 and some other text in that cell"
The function looks for "mandatsnummer" if it found it in that cell then it returns "mandatsnummer: 121213" as it looks for the mandtnumber and the value in the next varWoerter... which is 121213.
once it got it then it replaces the new found string "mandatnumber. 121213" with an empty string.. New text = "Mr Huber and some other text in that cell"

The second code runs through the column(J) and looks for all values puts it into Mandat()array and runs the function ...returns the updatet values into column(J) ... that part works ...
But all remaining cells where there was no "Mandatnumber" is gone.. So I gues it need to be redim preseve on my second Code.. So it keeps the old values

But I am not sure where to put it and I tried many different things put get an runtime error out of range..

can you just point me to the right direction??

Code:
    ReDim Preserve Mandat(UBound(Mandat) + 1)
    
    Mandat = Range("J2", Range("J1").End(xlDown))

    
    For i = LBound(Mandat, 1) To UBound(Mandat, 1)

    
        Mandat(i, 1) = MandatLoeschen(Mandat(i, 1))
    Next i
    Range("J2").Resize(UBound(Umsatztext, 1), 1) = Mandat

this is what I tried.. not working.. runtime error.

How is the correct statement for the redim? If you tell me than it might work .)
 
Last edited:
Upvote 0
Maybe this would help more..

Code:
Sub TestWith_Array()
    Dim Mandat() As Variant
    Dim i As Integer
    
    Mandat = Range("J2", Range("J1").End(xlDown))
    For i = LBound(Mandat, 1) To UBound(Mandat, 1)
        Mandat(i, 1) = MandatLoeschen(Mandat(i, 1))
    Next i
    Range("J2").Resize(UBound(Mandat, 1), 1) = Mandat

End Sub

Sorry about not sending it befor!! It was in a larger code .. and I thought it is easy to understand but I guess this is now alot better..

Many thanks guys!!!
 
Last edited:
Upvote 0
You don't need to redim preserve anything, you need to change your function(s) so that if nothing is found they return the original text.

Or change the code so that the function(s) aren't called unless they are actually needed.

Not 100% sure how you would do either as I've not seen any data.:)
 
Last edited:
Upvote 0
Oh really?

I can not send some data sorry that would be way to long for this area..)
"Some Data with Mandatsnummer: 121222121254513212 REF: 1121545454241
"No Number in this one"
"Again some Mandatsnummer: 111111555255 in some other Place where the gras is always green"
"My new home data Mandatsnummer: 1234567544 this is a nice place too"
"No need for a Mandatsnummer: 123666955 or is there one?"

This is something how it would be ... but I can not make it to work unfortunately .( crying gg
 
Upvote 0
How about a UDF?

Code:
Function SW(sInp As String, ByVal sFind As String) As String
  Dim asWd()        As String
  Dim i             As Long

  sFind = LCase(sFind)

  asWd = Split(sInp & " ")
  For i = 0 To UBound(asWd)
    If LCase(asWd(i)) = sFind Then
      SW = asWd(i) & " " & asWd(i + 1)
      Exit For
    End If
  Next i
End Function


A​
B​
C​
1​
2​
"Some Data with Mandatsnummer: 121222121254513212 REF: 1121545454241Mandatsnummer: 121222121254513212B2: =SW(A2, "Mandatsnummer:")
3​
"No Number in this one"
4​
"Again some Mandatsnummer: 111111555255 in some other Place where the gras is always green"Mandatsnummer: 111111555255
5​
"My new home data Mandatsnummer: 1234567544 this is a nice place too"Mandatsnummer: 1234567544
6​
"No need for a Mandatsnummer: 123666955 or is there one?"Mandatsnummer: 123666955
7​
Blah blah blah Mandatsnummer:Mandatsnummer:
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,180
Members
448,871
Latest member
hengshankouniuniu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top