+ Reply to Thread
Results 1 to 6 of 6

.Printout syntax issue

Hybrid View

rhuseman .Printout syntax issue 09-21-2011, 09:44 PM
Mordred Re: .Printout syntax issue 09-21-2011, 10:47 PM
rhuseman Re: .Printout syntax issue 09-21-2011, 10:59 PM
Mordred Re: .Printout syntax issue 09-21-2011, 11:11 PM
rhuseman Re: .Printout syntax issue 09-21-2011, 11:37 PM
Mordred Re: .Printout syntax issue 09-22-2011, 12:14 AM
  1. #1
    Registered User
    Join Date
    09-21-2011
    Location
    Illinois, USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    .Printout syntax issue

    This is a syntax/beginners question:

    Im making a system to print a single cell that will contain a barcode.

    I'm writing a basic macro that uses a for loop to print cells. I'm using the cells() command to reference the test cell so I can index it by using a number. (Instead of using range() which has a LetterNumber reference.) The problem exist when I want to print the single cell. I'm trying to use .printout with cells() and I'm having an issue. Help me correct it or find something better. The code is below and a picture.



    
    Sub print_selected_items()
    Dim cellrow As Integer
    Dim cellcollum  As Integer
    Dim cellvalue As String
    Dim cellcollumbarcode As Integer
    
    cellrow = 2 'Test cell row number
    cellcollum = 15 'Test cell collum number
    cellcollumbarcode = 17 ' Print cell collum number
    cellvalue = "" ' Test cell deciding factor
    
    For cellrow = 2 To 100 'For loop that index the cell row looking for a "true"
    
    cellvalue = Cells(cellrow, cellcollum).Value 'sets varible equal to test cell
    
    If cellvalue = "True" Then Printout.Cells(cellrow, cellcollum) copies:=1 'Prints barcode if test cell is equal to "true"
    
    Next cellrow 'indexes for loop
    End Sub




    GIVE ME YOUR THOUGHTS!!
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by rhuseman; 09-21-2011 at 10:27 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: .Printout syntax issue

    Hi rhuseman, try
    Sub print_selected_items()
        Dim cellrow As Integer
        Dim cellcollum As Integer
        Dim cellvalue As String
        Dim cellcollumbarcode As Integer
    
        cellrow = 2    'Test cell row number
        cellcollum = 15    'Test cell collum number
        cellcollumbarcode = 17    ' Print cell collum number
        cellvalue = ""    ' Test cell deciding factor
        For cellrow = 2 To 100    'For loop that index the cell row looking for a "true"
            cellvalue = Cells(cellrow, cellcollum).Value    'sets varible equal to test cell
            If cellvalue = "True" Then
                ActiveSheet.PageSetup.PrintArea = Cells(cellrow, cellcollum)
                With Worksheets
                    .PrintOut , , 1
                End With
            End If
        Next cellrow    'indexes for loop
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    09-21-2011
    Location
    Illinois, USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: .Printout syntax issue

    mordred,
    First of all thanks for reading this and replying. Im getting the code you submitted to not debug. Below i highlighted where its is throwing a "run time error "1004" reference not valid". Your plan of attack seems very close. I attached the workbook if thats helpful.

    ActiveSheet.PageSetup.PrintArea = Cells(cellrow, cellcollum)

    Sub print_selected_items()
        Dim cellrow As Integer
        Dim cellcollum As Integer
        Dim cellvalue As String
        Dim cellcollumbarcode As Integer
    
        cellrow = 2    'Test cell row number
        cellcollum = 15    'Test cell collum number
        cellcollumbarcode = 17    ' Print cell collum number
        cellvalue = ""    ' Test cell deciding factor
        For cellrow = 2 To 100    'For loop that index the cell row looking for a "true"
            cellvalue = Cells(cellrow, cellcollum).Value    'sets varible equal to test cell
            If cellvalue = "True" Then
                ActiveSheet.PageSetup.PrintArea = Cells(cellrow, cellcollum)
                With Worksheets
                    .PrintOut , , 1
                End With
            End If
        Next cellrow    'indexes for loop
    End Sub

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: .Printout syntax issue

    Replace your code with
    Sub print_selected_items()
        Dim cellrow As Integer
        Dim cellcollum As Integer
        Dim cellvalue As String
        Dim cellcollumbarcode As Integer
        Dim Ws1 As Worksheet
        Set Ws1 = Worksheets("OPENPOS")
        cellrow = 2    'Test cell row number
        cellcollum = 15    'Test cell collum number
        cellcollumbarcode = 17    ' Print cell collum number
        cellvalue = ""    ' Test cell deciding factor
        For cellrow = 2 To 100    'For loop that index the cell row looking for a "true"
            cellvalue = Ws1.Cells(cellrow, cellcollum).Value    'sets varible equal to test cell
            If cellvalue = "True" Then
                Ws1.PageSetup.PrintArea = Worksheets("OPENPOS").Cells(cellrow, cellcollum).Address
                With Ws1
                    .PrintOut , , 1
                End With
            End If
        Next cellrow    'indexes for loop
    End Sub
    and let me know how that goes.

  5. #5
    Registered User
    Join Date
    09-21-2011
    Location
    Illinois, USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: .Printout syntax issue

    Mordred,

    The code worked. The only issue now is that it is prompt the user a window warning them that only one cell has been selected for printing and wants to know if if this is an accident. Is their a way to automatically bypass this?

    Thank you very much!

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: .Printout syntax issue

    Hi rhuseman, I didn't test this but the following should work
    Sub print_selected_items()
        Dim cellrow As Integer
        Dim cellcollum As Integer
        Dim cellvalue As String
        Dim cellcollumbarcode As Integer
        Dim Ws1 As Worksheet
        Set Ws1 = Worksheets("OPENPOS")
        cellrow = 2    'Test cell row number
        cellcollum = 15    'Test cell collum number
        cellcollumbarcode = 17    ' Print cell collum number
        cellvalue = ""    ' Test cell deciding factor
        For cellrow = 2 To 100    'For loop that index the cell row looking for a "true"
            cellvalue = Ws1.Cells(cellrow, cellcollum).Value    'sets varible equal to test cell
            If cellvalue = "True" Then
                Application.DisplayAlerts = False
                Ws1.PageSetup.PrintArea = Worksheets("OPENPOS").Cells(cellrow, cellcollum).Address
                With Ws1
                    .PrintOut , , 1
                End With
                Application.DisplayAlerts = True
            End If
        Next cellrow    'indexes for loop
    End Sub
    You'll notice that I set Display Alerts to False before the print statement and then set them to True after. That should take care of the alerts.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1