Print resultValue from VBA script

georgeeye

New Member
Joined
Dec 12, 2023
Messages
4
I have the following script working to lookup a value from column F and display it's matching value from column I in a message box, but ulyimately I need to send the resultValue to the default windows printer, and I'm not having much luck getting printing to work.

Here's the script:

VBA Code:
Sub LookupAndPrintToDefaultPrinter()
    Dim searchValue As Variant
    Dim ws As Worksheet
    Dim searchRange As Range
    Dim foundCell As Range
    Dim resultValue As Variant
    
    ' Set the worksheet where you want to perform the search
    Set ws = ThisWorkbook.Sheets("Main") ' Change "Sheet1" to your sheet's name
    
    ' Define the search range (column A in this example)
    Set searchRange = ws.Columns("F") ' Change "A" to your desired column
    
    ' Ask user for input
    searchValue = InputBox("Enter value to search:", "Lookup Value")
    
    ' Check if the user entered a value
    If searchValue <> "" Then
        ' Look for the value in the search range
        Set foundCell = searchRange.Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)
        
        ' If the value is found
        If Not foundCell Is Nothing Then
            ' Get the value from a cell on the same row (column B in this example)
            resultValue = ws.Cells(foundCell.Row, "I").Value ' Change "B" to your desired column
            
            ' Print the value to the default printer
              MsgBox resultValue ' Print the value in the Immediate window (for demonstration purposes)
            ' To print to the default printer, you can use appropriate code here.
            ' For example, you might use the PrintOut method for the resultValue or specific content.
            
            ' Please replace the Debug.Print line above with the appropriate print command for your specific printer configuration.
            ' ActiveWindow.resultValue.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        Else
            MsgBox "Value not found.", vbExclamation, "Result"
        End If
    Else
        MsgBox "No value entered.", vbExclamation, "Result"
    End If
End Sub

I though this line would work, but I can't get it to work for the life of me:

ActiveWindow.resultValue.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
An example.
VBA Code:
Sub PrintExample()
    Dim searchValue As Variant, resultValue As Variant
    Dim ws As Worksheet
    Dim searchRange As Range, foundCell As Range
   
    Set ws = ActiveSheet
    Set searchRange = Range("A1:E50")

    'Create some data
    searchRange.Value = "AAA"

    'Create something to find
    searchValue = "Red Rock"
    Range("B10").Value = searchValue

    ' Look for the value in the search range
    Set foundCell = searchRange.Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)

    ' If the value is found
    If Not foundCell Is Nothing Then
        ' Get the value from a cell on the same row (column B in this example)
        resultValue = foundCell.Value
            
        ' Print the value to the default printer
        Select Case MsgBox("Result = " & resultValue & vbCrLf & vbCrLf _
            & "Print Result?", vbYesNo Or vbQuestion, "Found in cell " & foundCell.Address(0, 0))
            Case vbYes
                foundCell.PrintOut
            Case vbNo

        End Select
    Else
        MsgBox "Search Value '" & searchValue & "' not found.", vbExclamation, "Result"
    End If
End Sub
 
Upvote 0
An example.
VBA Code:
Sub PrintExample()
    Dim searchValue As Variant, resultValue As Variant
    Dim ws As Worksheet
    Dim searchRange As Range, foundCell As Range
  
    Set ws = ActiveSheet
    Set searchRange = Range("A1:E50")

    'Create some data
    searchRange.Value = "AAA"

    'Create something to find
    searchValue = "Red Rock"
    Range("B10").Value = searchValue

    ' Look for the value in the search range
    Set foundCell = searchRange.Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)

    ' If the value is found
    If Not foundCell Is Nothing Then
        ' Get the value from a cell on the same row (column B in this example)
        resultValue = foundCell.Value
           
        ' Print the value to the default printer
        Select Case MsgBox("Result = " & resultValue & vbCrLf & vbCrLf _
            & "Print Result?", vbYesNo Or vbQuestion, "Found in cell " & foundCell.Address(0, 0))
            Case vbYes
                foundCell.PrintOut
            Case vbNo

        End Select
    Else
        MsgBox "Search Value '" & searchValue & "' not found.", vbExclamation, "Result"
    End If
End Sub
Thanks! This is closer than I was before!

Here's the version I have now:

VBA Code:
Sub LookupAndPrintToDefaultPrinter()
    Dim searchValue As Variant
    Dim ws As Worksheet
    Dim searchRange As Range
    Dim foundCell As Range
    Dim resultPrint As Range
    Dim resultValue As Variant
    
    ' Set the worksheet where you want to perform the search
    Set ws = ThisWorkbook.Sheets("Main") ' Change "Sheet1" to your sheet's name
    
    ' Define the search range (column A in this example)
    Set searchRange = ws.Columns("F") ' Change "A" to your desired column
    
    ' Ask user for input
    searchValue = InputBox("Enter value to search:", "Lookup Value")
    
    ' Check if the user entered a value
    If searchValue <> "" Then
        ' Look for the value in the search range
        Set foundCell = searchRange.Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)
        
        ' If the value is found
        If Not foundCell Is Nothing Then
            ' Get the value from a cell on the same row (column B in this example)
            resultValue = ws.Cells(foundCell.Row, "I").Value ' Change "B" to your desired column
            
                      
            ' Print the value to the default printer
            '  MsgBox resultValue ' Print the value in the Immediate window (for demonstration purposes)
            ' To print to the default printer, you can use appropriate code here.
            Select Case MsgBox("Result = " & resultValue & vbCrLf & vbCrLf _
            & "Print Result?", vbYesNo Or vbQuestion, "Found in cell " & resultValue)
            Case vbYes
                resultValue.PrintOut
            Case vbNo

        End Select
            ' For example, you might use the PrintOut method for the resultValue or specific content.
            
            ' Please replace the Debug.Print line above with the appropriate print command for your specific printer configuration.
            ' ActiveWindow.resultValue.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        Else
            MsgBox "Value not found.", vbExclamation, "Result"
        End If
    Else
        MsgBox "No value entered.", vbExclamation, "Result"
    End If
End Sub

But I get an object required error from this line:

resultValue.PrintOut

Any ideas on how I print the value of a Variant Dim?
 
Upvote 0
Any ideas on how I print the value of a Variant Dim?

By putting that value in a cell or range of cells, and printing the range. Keep in mind that .Printout is a range property.

Example:
VBA Code:
Dim resultValue As Variant

resultValue = 2 + 2
ActiveSheet.Range("A1").Value = resultValue
ActiveSheet.Range("A1").PrintOut
 
Upvote 1
Solution
By putting that value in a cell or range of cells, and printing the range. Keep in mind that .Printout is a range property.

Example:
VBA Code:
Dim resultValue As Variant

resultValue = 2 + 2
ActiveSheet.Range("A1").Value = resultValue
ActiveSheet.Range("A1").PrintOut
That works perfectly! Thank you so much! One last question; the resultValue I'm printing is in a barcode font, but it prints out plain text. Do I need to specify the font I want it to print in?
 
Upvote 0
That works perfectly! Thank you so much! One last question; the resultValue I'm printing is in a barcode font, but it prints out plain text. Do I need to specify the font I want it to print in?
Nevermind, I figured it out, just needed to set the font on the range I was storing resultValue in.

Thnaks again for your help!
 
Upvote 0

Forum statistics

Threads
1,224,810
Messages
6,181,079
Members
453,021
Latest member
Justyna P

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