VBA message input to complete print range

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
I would like to use a macro with a message box response to complete a print range. So let's say I want a print range of A3 to C30, but I want the C30 to change based on the input in the message box. The A3 can remain fixed as that will not likely change. I am new to vba and could not fine anything in the search. Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I know the code below is not correct but it may help convey what I want to do.

Sub print_range()
Dim response As String
response = InputBox ("enter 2nd part of range")
Worksheets("table prod").PageSetup.PrintArea = $A$3 and here I want to add the entry from the input box.
End Sub
 
Upvote 0
So something like this?

Code:
Sub print_range()
    Dim response As String
    Dim PrintAreaString As String


    response = InputBox("Enter 2nd part of range (example: $H$10)", "Enter Data")
    PrintAreaString = "$A$3:" & Trim(response)
    If response <> "" Then
        Worksheets("table prod").PageSetup.PrintArea = PrintAreaString    ' and here I want to add the entry from the input box.
    End If
End Sub
 
Upvote 0
Yes this seems to work, thanks. Can you add one more twist? I have a row number in cell A1 which represents the last row that has data and which the print range goes down to. So could this be modified so the input would only have to be the column letter? So A3 to F50 as the print range, and 50 is in cell A1. The column letter would vary depending on what I need to print.
 
Upvote 0
Code:
Sub print_range()
    Dim response As String
    Dim PrintAreaString As String


    response = InputBox("Enter column letter for 2nd part of range", "Enter Data")


    With Worksheets("table prod")
        PrintAreaString = "$A$3:$" & Trim(UCase(response)) & "$" & .Range("A1").Value
        If response <> "" Then
            .PageSetup.PrintArea = PrintAreaString
        End If
    End With
End Sub
 
Upvote 0
Works perfectly. I'm trying to add other things on my own before asking but need a little more help. To modify the code to run on the active sheet? There seems to be many ways when I searched so not sure.
Also, I want to add some printing attributes such as fit to page, landscape, etc. which I can do them myself, but if you can add one command then I can follow with others. Thanks again I really appreciate it.
 
Upvote 0
To modify the code to work on the activesheet, change


Code:
    With Worksheets("table prod")

to

Code:
With Activesheet

Keep in mind that if you don't have the row number stored in cell A1 of the activesheet, the macro will break. For the print settings I recommend using the macro recorder to record a macro changing the printing attributes you are interested in and then inspecting the code that the recorder creates as a guide to implementing them in your code.
 
Upvote 0
I made the change to "with activesheet" and the macro will not run on my other sheets in the same workbook. They all have the row number stored in A1 of the respective sheet. Even if I specify the other sheets by name in the macro it still doesn't run.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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