Error in IF Statement

Haree

Board Regular
Joined
Sep 22, 2019
Messages
85
Office Version
  1. 2016
Hi all, I have a code which identifies from which system the code is running and changes the default printer, but for some reason the if part isnt working. Can anyone please help me with this regard.

I was able to identify the computers name with the help of environ function. Mr @RoryA helped me out in this regard .

the code is as follows
VBA Code:
Private Sub CommandButton1_Click()
Dim Computername As String
Set mynetwork = CreateObject("WScript.network")
Computername = Environ$("computername")
If Computername <> "Desktop-165t6df" Then
    mynetwork.setdefaultprinter "\\Desktop-165t6df\THERMAL Receipt #1"
    ActiveSheet.Range("A3:B52").Select
    Selection.PrintOut Copies:=1
    mynetwork.setdefaultprinter "\\Desktop-165t6df\HP LaserJet 1020"
Else
    mynetwork.setdefaultprinter "THERMAL Receipt #1"
    ActiveSheet.Range("A3:B52").Select
    Selection.PrintOut Copies:=1
    mynetwork.setdefaultprinter "HP LaserJet 1020"
End If
End Sub

Thank you in advance
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Haree

Board Regular
Joined
Sep 22, 2019
Messages
85
Office Version
  1. 2016
If Computername <> "Desktop-165t6df" Then

i am working from the above mentioned computer, but it does not go to the else part it just returns no printer found named "Desktop-165t6df\THERMAL Receipt #1
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,573
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
The test is case sensitive, so check what exactly Computername holds.
 
Solution

Haree

Board Regular
Joined
Sep 22, 2019
Messages
85
Office Version
  1. 2016
Thank you @RoryA , sorry for being lame. i did not guess the case sensitive. it works fine now.
Thank you once gain for your time
 

Forum statistics

Threads
1,144,684
Messages
5,725,760
Members
422,639
Latest member
i have a question

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
Top