JugglerJAF
Active Member
- Joined
- Feb 17, 2002
- Messages
- 297
- Office Version
- 365
- Platform
- Windows
I have a file that I want to use to generate a complex IF statement string that I paste into VBA to check the layout of a data file received from clients. My sheet has 3 columns:
If UCase([A1]) = "ALPHA" And UCase([B1]) = "BRAVO" And UCase([C1]) = "CHARLIE" And UCase([D1]) = "DELTA" Then
So far, so good, but because I'm potentially checking dozens of column headers, there comes a point where the VBA Editor can;t handle a single line of text over a certain length, so I want to split the string across several lines. In this example, I'd split after B to give:
If UCase([A1])="ALPHA" And UCase([B1])="BRAVO" And _
UCase([C1])="CHARLIE" And UCase([D1])="DELTA" Then
But the issue I'm having is that when I enter in a line break after the "And _", and paste it to the VBA Editor, the whole string is enclosed in quotes, and each entry checked for is surrounded by 2 double quotes:
"If UCase([A1])=""ALPHA"" And UCase([B1])=""BRAVO"" And _
UCase([C1])=""CHARLIE"" And UCase([D1])=""DELTA"" Then"
Is there a way to code it so that a line break is entered without treating the entire things as a quote enclosed string?
Current code is:
- A1: Col (contains column letters: A, B, C, D, etc.)
- B1: Header (the expected values: alpha, bravo, charlie, delta etc.
- C1: NewLine: True/False as to whether the code should start on a new line
If UCase([A1]) = "ALPHA" And UCase([B1]) = "BRAVO" And UCase([C1]) = "CHARLIE" And UCase([D1]) = "DELTA" Then
So far, so good, but because I'm potentially checking dozens of column headers, there comes a point where the VBA Editor can;t handle a single line of text over a certain length, so I want to split the string across several lines. In this example, I'd split after B to give:
If UCase([A1])="ALPHA" And UCase([B1])="BRAVO" And _
UCase([C1])="CHARLIE" And UCase([D1])="DELTA" Then
But the issue I'm having is that when I enter in a line break after the "And _", and paste it to the VBA Editor, the whole string is enclosed in quotes, and each entry checked for is surrounded by 2 double quotes:
"If UCase([A1])=""ALPHA"" And UCase([B1])=""BRAVO"" And _
UCase([C1])=""CHARLIE"" And UCase([D1])=""DELTA"" Then"
Is there a way to code it so that a line break is entered without treating the entire things as a quote enclosed string?
Current code is:
VBA Code:
Sub generate_check_string()
check_str = "If "
Range("B2").Select
If ActiveCell.Offset(1, 0).Value = "" Then
check_str = check_str & "UCase([" & ActiveCell.Offset(0, -1).Value & 1 & "])="""
check_str = check_str & UCase(ActiveCell.Value) & """ Then"
Else
Range(ActiveCell, ActiveCell.End(xlDown)).Select
For Each c In Selection
check_str = check_str & "UCase([" & c.Offset(0, -1).Value & 1 & "])="""
check_str = check_str & UCase(c.Value) & """"
If c.Offset(1, 0).Value = "" Then
check_str = check_str & " Then"
Else
If c.Offset(0, 1).Value = True Then
check_str = check_str & " And " 'single line: works
'..or...'
'check_str = check_str & " And _" & vbCr 'split line: fails
Else
check_str = check_str & " And "
End If
End If
Next c
End If
'
Range("G2").Select
ActiveCell.Value = check_str
Range("G2").WrapText = True
End Sub