Run Code only if Header contains multiple values in different cells

silentwolf

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

it has been a while since I have been here so I hope all of you are well!

In my worksheet first Row are about 10 columns filled with header data.
I like to create a condition so the code only runs if those cells have certain values.
For example
A1 (IBAN)
B1 (Auszugsnummer)
C1 (Buchungsdatum)
D1 (Valudadatum)
E1 (Umsatzzeit)
and so on..

So what could be the easiest or the best way of creating a conditon so the Code only runs if all 10 Cells in the header row meat this condition if not run a different code or exit out of it.

I found some help on here where the first rows are populated in a string Array.

VBA Code:
Sub datacollect()
Dim heading() As String
Dim i As Integer
Dim x As Variant

i = -1
For Each x In rows(1).Cells
    If x.value = "" Then Exit For
    i = i + 1
    ReDim Preserve heading(i) As String
    heading(i) = x.value
Next x
End Sub

Something like that.. but how can I put that all together to create a safeguard so the Code wont run if those header cells are not given or different pupulated.

Hope this is understandable and someone could give me a littel help with it.

Many Thanks

Albert
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi Albert

Could you not just have aa helper cell with formula in, formula checks the conditions, then code runs if formule is true etc?

Or if not liking formula route.

maybe something like, but maybe too long winded.
VBA Code:
Sub albert()
tot = 0
    If Range("a1") = "IBAN" Then tot = tot + 1
    If Range("b1") = "Auszugsnummer" Then tot = tot + 1
    If Range("c1") = "" Then tot = tot + 1
    If Range("d1") = "" Then tot = tot + 1
    If Range("e1") = "" Then tot = tot + 1
    If Range("f1") = "" Then tot = tot + 1
    If Range("g1") = "" Then tot = tot + 1
    If Range("h1") = "" Then tot = tot + 1
    If Range("i1") = "" Then tot = tot + 1
    If Range("j1") = "" Then tot = tot + 1
    If tot = 10 Then runmycode
End Sub
 
Upvote 0
Hi Albert

Could you not just have aa helper cell with formula in, formula checks the conditions, then code runs if formule is true etc?

Or if not liking formula route.

maybe something like, but maybe too long winded.
VBA Code:
Sub albert()
tot = 0
    If Range("a1") = "IBAN" Then tot = tot + 1
    If Range("b1") = "Auszugsnummer" Then tot = tot + 1
    If Range("c1") = "" Then tot = tot + 1
    If Range("d1") = "" Then tot = tot + 1
    If Range("e1") = "" Then tot = tot + 1
    If Range("f1") = "" Then tot = tot + 1
    If Range("g1") = "" Then tot = tot + 1
    If Range("h1") = "" Then tot = tot + 1
    If Range("i1") = "" Then tot = tot + 1
    If Range("j1") = "" Then tot = tot + 1
    If tot = 10 Then runmycode
End Sub
Hi SQUIDD,

thanks for your reply!
Unfortunatelly I like to have it all automated so a Formula is not the way I would like to go.
However could you not just somehow create an array and then compare it with the first row of the worksheet somehow?

I am not that good in arrays but that what I was thinking and been searching for I guess..

But many thanks for your code and input!!

Cheers Albert
 
Upvote 0
Hi
Try this method

VBA Code:
Sub test()
    Dim a, b
    Dim i&
    a = Application.Transpose(Cells(1, 1).Resize(, 10))
    b = [{"IBAN","Auszugsnummer","Buchungsdatum","Valudadatum","Umsatzzeit"}] '<< fill as required
    For i = 1 To UBound(b)
        If Not IsNumeric(Application.Match(b(i), a, 0)) Then
            Exit Sub 'Or do something
        End If
    Next
End Sub
 
Upvote 0
Hi Albert

If i understand you correctly?

VBA Code:
Sub albert_2()
    my_arr = ""
    mystr = ",IBAN,Auszugsnummer,Buchungsdatum,Valudadatum,Umsatzzeit" 'hardcode list,change to suit
    'make the header array
        For a = 1 To 10
            my_arr = my_arr & "," & Cells(1, a)
        Next a
    If my_arr = mystr Then runmycode
End Sub
 
Last edited:
Upvote 0
Hi Albert

If i understand you correctly?

VBA Code:
Sub albert_2()
    my_arr = ""
    mystr = "IBAN,Auszugsnummer,Buchungsdatum,Valudadatum,Umsatzzeit," 'hardcode list,change to suit
    'make the header array
        For a = 1 To 10
            my_arr = my_arr & "," & Cells(1, a)
        Next a
    If my_arr = mystr Then runmycode
End Sub
Hi Squidd,

thank you for your code I just have to work it out to run it correctly but many thanks!
I will get back to you if I get it to run :)

Cheers
Albert
 
Upvote 0
Would something like this work for you? I have just done it for the 5 columns you listed so you would need to expand it if you have 10 columns.

VBA Code:
Sub Run_If()
  If Join(Application.Index(Range("A1:E1").Value, 1, 0), "|") = "IBAN|Auszugsnummer|Buchungsdatum|Valudadatum|Umsatzzeit" Then
    'Run the code here
  End If
End Sub
 
Upvote 0
Hi Squidd,

thank you for your code I just have to work it out to run it correctly but many thanks!
I will get back to you if I get it to run :)

Cheers
Albert
No Problem Albert.

I did edit it to put a , at the start of mystr.

What calls this code to check the headers? button? sheet change?
 
Upvote 0
Would something like this work for you? I have just done it for the 5 columns you listed so you would need to expand it if you have 10 columns.

VBA Code:
Sub Run_If()
  If Join(Application.Index(Range("A1:E1").Value, 1, 0), "|") = "IBAN|Auszugsnummer|Buchungsdatum|Valudadatum|Umsatzzeit" Then
    'Run the code here
  End If
End Sub
Hi Peter,
thanks to you for the code!
Well unfortunatelly it never gets into the debug.print statement
VBA Code:
Sub Run_If()
  If Join(Application.index(Range("A1:I1").value, 1, 0), "|") = "IBAN|Auszugsnummer|Buchungsdatum|Valudadatum|Umsatzzeit|Zahlungsreferenz|Waehrung|Betrag|Buchungstext|Umsatztext" Then
    'Run the code here
    Debug.Print "Alles gut"
  End If
End Sub

What am I missing?

Cheers
Albert
 
Upvote 0
Well unfortunatelly it never gets into the debug.print statement
Do the headers have to be in the order you gave in post #1?
silentwolf.xlsm
ABCDE
1IBANAuszugsnummerBuchungsdatumValudadatumUmsatzzeit
Sheet1


Or could they be in any order like this?
silentwolf.xlsm
ABCDE
1BuchungsdatumIBANUmsatzzeitValudadatumAuszugsnummer
Sheet1 (2)


.. or something else altogether?
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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