Define letter as number, include as part of string for loop - vb.net

If SecretWordLength = 5 Then
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
End If
This is the current code for making the dash underneath a letter visible for my hangman game based on the length of the secret word. How can I change this into a FOR loop so that I don't have to repeat this code for every label individually?
I was thinking of using a FOR loop in this way:
For i = 3 To 7
Labeli.Visible = True
Next
But it does not work as it recognizes the i as the letter itself, not the number I want it to represent. Help please?

Related

Comparing EPPlus Style Fill background Color with System.Drawing.Color gives error

I am using EPPlus library to read and modify contents on an excel file.
The background colour on rows gets assigned initially when the excel file is processed first. New records will be added manually. Then when new records are added to this existing file, they won't have a background colour set. I wish to skip the already processed rows based on the background colour. But I get this error when trying to do a colour comparison.
Error 5 Value of type 'OfficeOpenXml.Style.ExcelColor' cannot be converted to 'System.Drawing.Color'.
If Not myrow.Style.Fill.BackgroundColor = Color.LightSlateGray AndAlso myrow.Style.Font.Bold = True Then
HasExcelRowBeenAlreadyProcessed = False
Else
HasExcelRowBeenAlreadyProcessed = True
End If
I also tried this but somehow it is not working. Please, can someone assist?
Dim oCellRGBVal = System.Drawing.ColorTranslator.FromHtml(myrow.Style.Fill.BackgroundColor.LookupColor.ToString).ToArgb
HasExcelRowBeenAlreadyProcessed = False
If Not oCellRGBVal = Color.LightSlateGray.ToArgb AndAlso myrow.Style.Font.Bold = True Then
HasExcelRowBeenAlreadyProcessed = False
Else
HasExcelRowBeenAlreadyProcessed = True
End If
Instead of trying to convert the background color, I would instead convert the initial color to a string.
Dim colorAsString As String = Color.LightSlateGray.ToArgb.ToString("X2")
If Not myrow.Style.Fill.BackgroundColor.Rgb = colorAsString AndAlso myrow.Style.Font.Bold = True Then
HasExcelRowBeenAlreadyProcessed = False
Else
HasExcelRowBeenAlreadyProcessed = True
End If
This should work. Also, this let you store colorAsString so you don't need to calculate it everytime.

Algorithm for finding end of a list (SAP GUI)

I'm writing a script that adds elements to a list in a SAP GUI screen. Now, it seems that when using SAP GUI, nothing "exists" unless it is actually on screen, so the first step involves finding the end of the list.
I accomplished this by scrolling though each element, and checking if it was blank.
Do While Not blank
If session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-MATNR[2,0]").Text = "" Then blank = True
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = i
i = i + 1
Loop
However, for very large existing lists, this takes a long time. I'm trying to figure out a way to find the end more quickly. Some truths/limitations I know:
I'm assuming I have no knowledge of the list length.
I cannot command the verticalScrollbar.position too far beyond the end of
the list. For ex. if the list contains 62 elements, .verticalScrollbar.Position = 100 will not work.
In the case of the above example, SAP does NOT throw an error. Nothing happens at all, and then next line of code executes.
All references to elements are with respect to their position on the screen. Ex, if I scroll down 5 positions, the 6th element of the overall list would actually indexed as 1.
On the other hand, verticalScrollbar.Position is absolute
I'm thinking of doing the following (in very psuedocode):
i = 0
do while scrolled = true
scrolled = false
a = GUIlist[0]
verticalScrollbar.Position = i + 1000
b = GUIlist[0]
'check to see the first element shown has changed
if a <> b then
scrolled = true
i = i + 1000
end if
loop
do while scrolled = true
scrolled = false
a = GUIlist[0]
verticalScrollbar.Position = i + 500
b = GUIlist[0]
if a <> b then
scrolled = true
i = i + 500
end if
loop
...and so on until I'm iterating i by one.
Is there a generally accepted better way of doing this kind of 'search'?
Any input is appreciated.
Thanks
My suggestion:
session.findById("wnd[0]").sendVKey 83
myPosition = session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position
do
if session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-MATNR[2,0]").Text = "" then exit do
myPosition = myPosition + 1
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = myPosition
loop
msgbox myPosition
Regards,
ScriptMan
Just to go to the end
max_scrollbar = session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Maximum ' Get the maximum scrollbar value
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = max_scrollbar ' Go to the end

VB Validation keeps looping

i have been assigned to write a program which saves data about films.We are required to use windows forms in vb.Im trying to validate the data entry for the text box.The text entered must be numbers and must be 4 characters long.The problem im trying to solve is that once it has looked at the value i want the program to allow the user to re-enter a value into the text box but instead it keeps looking at the text box with nothing in then says that it is not in the correct format , without letting the user enter a correct value.here is the code , just for the validation so far:
Dim PassValidate As Boolean = False
ReadFilmData()
NOFilms = NOFilms + 1
ReDim Preserve Film(NOFilms)
'Validate before it is saved to the file'
Do
If IsNumeric(Me.txtFilmID.Text) = True Then
If Me.txtFilmID.TextLength = 0 Then
'Presence check'
MsgBox("You are required to provide a value for the film ID")
ElseIf Me.txtFilmID.TextLength > 4 Then
'Length Check'
MsgBox("The film ID must be 4 numbers long")
ElseIf Me.txtFilmID.TextLength = 4 Then
PassValidate = True
End If
End If
If IsNumeric(Me.txtFilmID.Text) = False Then
MsgBox("The film ID must be a set of numbers ")
End If
If PassValidate = False Then
txtFilmID.Text = ""
MsgBox("Re-enter the value for Film ID")
Call SaveFilmData()
End If
If PassValidate = True Then
Film(NOFilms).FilmID = txtFilmID.Text
End If
Loop Until PassValidate = True
you should check out the validation events in WinForms. They are there to avoid the very problem you are describing.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Try removing the Do Loop, there does not seem to be a need for the loop.

VBA Excel Double If

I have various dropdown menus in the document and need to create a double conditional statement.
So I am trying to do the following:
If Cell B14 = Option 1 Then unhide Cells B16:B17
If Cells B17 = Yes Then hide Cells B19:B53
If Cells B17 = No Then show Cells B19:B34
So if someone picks Option 1 from the dropdown menu then there is another drop down menu that appear and if they select Yes from the second on it hide the required cells and if they pick No it unhides the required cells.
Options 2 & 3 in the first dropdown menu do not need to show the second drop down box.
I have it all working apart from the double if.
Current code is:
If Target.Address = "$B$14" Then
If Range("B14") = "Option 1: Travel Home" Then
ActiveSheet.Rows("16:35").EntireRow.Hidden = False
ActiveSheet.Rows("36:55").EntireRow.Hidden = True
ElseIf Range("B14") = "Option 2: Travel to next city" Then
ActiveSheet.Rows("15").EntireRow.Hidden = False
ActiveSheet.Rows("16:17").EntireRow.Hidden = True
ActiveSheet.Rows("19:35").EntireRow.Hidden = True
ActiveSheet.Rows("36").EntireRow.Hidden = False
ActiveSheet.Rows("37:55").EntireRow.Hidden = True
ElseIf Range("B14") = "Option 3: Make own arrangements" Then
ActiveSheet.Rows("15:36").EntireRow.Hidden = True
ActiveSheet.Rows("39:55").EntireRow.Hidden = False
End If
End If
Option 2 and 3 are working OK just option 1 isn't working to unhide/hide whats necessary and also then need to do the second dropdown option which the alters what is shown.
I tested the below code and it appears to be behaving as you desire. I did make some seemingly significant changes to the structure, but I did so in hopes of making it both easier to read and maintain going forward. I think my edits are self-explanatory.
I also took the liberty to assume this is a Worksheet_Change event even though you did not explicity state as such.
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case Is = "$B$14"
Select Case Right(Left(Target.Value, 8), 1)
Case Is = 1 'Option 1
Me.Rows("16:17").EntireRow.Hidden = False
Case Is = 2 'Option 2
Me.Rows("15").EntireRow.Hidden = False
Me.Rows("16:17").EntireRow.Hidden = True
Me.Rows("19:35").EntireRow.Hidden = True
Me.Rows("36").EntireRow.Hidden = False
Me.Rows("37:55").EntireRow.Hidden = True
Case Is = 3 'Option 3
Me.Rows("15:36").EntireRow.Hidden = True
Me.Rows("39:55").EntireRow.Hidden = False
Case Else
Me.Rows("15:55").EntireRow.Hidden = True
End Select
Case Is = "$B$17"
Me.Range("B19:B35").EntireRow.Hidden = Not (Target.Value = "No")
End Select
End Sub
Here's the code to make sure all rows are hidden when the workbook is opened.
Private Sub Workbook_Open()
With Me.Worksheets("SheetName") ' change as needed
.Rows("1:3").EntireRow.Hidden = True
.Rows("15:55").EntireRow.Hidden = True
End With
End Sub

How to hide columns based on name in Excel 2010?

I am trying to hide columns based on name using VBA inside Excel 2010. Each of my columns have a product version and some results below it. The product version does repeat throughout the spreadsheet since I have it categorized by OS. Thus, I'm hiding multiple columns based on selection, like a filter would do. If I could hide based on the name and not the column letter (A,B,C,...), then adding columns in between in the future would prevent more code changes on the location of those columns.
What I'm currently doing right now is fixed to the column letter. This limits me in the sense that I cannot add columns in between without having to change the code (column letter). Example:
`If productver_2dot5.Value = True Then
Columns("E").Hidden = False
Columns("M").Hidden = False
Columns("AC").Hidden = False
Columns("AT").Hidden = False
Columns("BD").Hidden = False
Columns("BR").Hidden = False
Else
Columns("E").Hidden = True
Columns("M").Hidden = True
Columns("AC").Hidden = True
Columns("AT").Hidden = True
Columns("BD").Hidden = True
Columns("BR").Hidden = True
End If`
What I would like to do is to hide any columns that contains the name 'Product Ver 2" (example) in one of its cells.
Sub HideBlahs()
Dim col As Range
For Each col In ActiveSheet.UsedRange.Columns
If Application.CountIf(col, "blah") > 0 Then
col.EntireColumn.Hidden = True
End If
Next col
End Sub
FYI your posted code reduces to:
Range("E1,M1,AC1,AT1,BD1,BR1").EntireColumn.Hidden = Not productver_2dot5.Value