Redim Preserve?

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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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


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

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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