Looping macro with input message box receiving runtime error 91

OMDhaf

New Member
Joined
Jul 1, 2014
Messages
4
I have written vba code to hide columns when a range of cells do not match a specific initial. This piece of the code works. I want to expand the code with a message box to ask for input (Please enter initials?). Once initials entered run the below code based on the initials entered. Then ask Do you have additional initials? Based on that answer (Yes) either run the loop again or (No) stop the macro. This is my first time attempting to write code so thanks in advance for your help.

The loop should look at the initials entered, search the first cell and either hide the column or leave it open and then move to the next cell....etc...


Sub InputMessageMacroWithDo()




' this one works well to hide columns based on AS only currently using <> =
Dim rRange As Range
Dim rCell As Range
Dim ans As String
Dim myCheck As String
Dim initials As String

Set rRange = Sheets("Sheet1").Range("C25:<wbr>CK25").Cells

ans = MsgBox("Do you have Supervisor initials to enter?", vbYesNo)
If ans = vbNo Then Exit Sub
Do
initials = InputBox(Prompt:="Enter supervisor initials", title:="Supervisor Initials")
If UCase(rCell.Value) = initials Then rCell.Columns.EntireColumn.<wbr>Hidden = False
ans = MsgBox("Copy and paste the unhidden columns into a new tab. Label the tab with the Supervisors name. Do not press OK until you have completed the copy and paste. Once completed press OK.")
myCheck = MsgBox("Do you have more Supervisor initials to enter?", vbYesNo)
If myCheck = vbNo Then Exit Sub
Loop While myCheck = vbYes


End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi OMDhaf, Welcome to MrExcel forum,
You have a couple of problems with this code.
1. rCell is not initialized so it will throw an error as soon as that line attempts to execute.
2. The myCheck message box tells the user to copy a column to a new workbook before clicking OK, which Excel will not allow to happen. While the message box is displayed, Excel is frozen.
3. The comparison of the initials from the InputBox to rCell(wherever it is on row 25) probably will be hit and miss since the code only allows the comparison to be between 'initials' and whatever is in cell C25. There is nothing in the code to advance the comparison range across the row for each 'initials'.

You might want to think about this one a little bit more and make some modifications to your logic.
 
Upvote 0
Thank you. I was able to initialize my rCell, remove completely the msgbox and use the copy and paste vba. I did get some additional help on having it read each cell in the row25 and not just the first cell. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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