Using Cell Value to Identify "Print To" Folder with If Then ElseIf

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
701
Office Version
  1. 365
Platform
  1. Windows
I have a bit of a dilemma I've been trying to resolve since yesterday. I am needing to print some documents to PDF and store them in particular folders based on cell values. I have this code so far:
VBA Code:
    If nP1.Range("B8").Value <> """" Then
        fP = "Masked Path\" & nP1.Range("B8").Value & "\"
    ElseIf nP1.Range("B9").Value <> """" Then
        fP = "Masked Path\" & nP1.Range("B9").Value & "\"
    ElseIf nP1.Range("B10").Value <> """" Then
        fP = "Masked Path\" & nP1.Range("B10").Value & "\"
    ElseIf nP1.Range("B11").Value <> """" Then
        fP = "Masked Path\" & nP1.Range("B11").Value & "\"
    ElseIf nP1.Range("B12").Value <> """" Then
        fP = "Masked Path\" & nP1.Range("B12").Value & "\"
    Else
        fP = "Masked Path\"
    End If

First Issue = If B8 is Null, the code is skipping to the last FP line instead of moving to the next FP line.
Second Issue = In order to reduce the time it's taking to perform this process, I'm going to move the saving to the User's desktop. The issue is that some people in my company have partially migrated to OneDrive, so there are 2 potential paths to a User's desktop.

Suggestions on how to change the code to solve these issues, with the first issue being the more significant one.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
First issue: get rid of the four double quotes. An empty cell is "", not """". What the four quotes is checking for is a single double quote in cell B8.

In VBA, you have to put strings between double quotes, but to check for the cell/string value itself including a double quote, you can put 2 double quotes within the string for EACH single double quote in the string.

For example, to check for the string ("Message") within a cell, you can perform
VBA Code:
If cell.Value = """Message"""
If the cell value is (Message) without any quotes, the If will fail.

Alternatively, you can also perform
VBA Code:
If cell.Value = Chr(34) & "Message" & Chr(34)
because Chr(34) is the double quote character.

I don't have any immediate suggestion for the second issue.
 
Upvote 1
Solution
First issue: get rid of the four double quotes. An empty cell is "", not """". What the four quotes is checking for is a single double quote in cell B8.

In VBA, you have to put strings between double quotes, but to check for the cell/string value itself including a double quote, you can put 2 double quotes within the string for EACH single double quote in the string.

For example, to check for the string ("Message") within a cell, you can perform
VBA Code:
If cell.Value = """Message"""
If the cell value is (Message) without any quotes, the If will fail.

Alternatively, you can also perform
VBA Code:
If cell.Value = Chr(34) & "Message" & Chr(34)
because Chr(34) is the double quote character.

I don't have any immediate suggestion for the second issue.
Thanks for the response! The 4 quotes were the issue. I must have been thinking of "null" in a formula vs. "straight" code. Thank you again!!!
 
Upvote 0

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

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