Error in IF Statement

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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
 
Upvote 0
The test is case sensitive, so check what exactly Computername holds.
 
Upvote 0
Solution
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
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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