I am not an expert on VBA, all I know is based on browsing internet, but some simple codes work for me well.
I am switching pictures based on P52 value, that works perfectly, but then I want to swich different pictures based on cell value P117 and that part of the code does not really work for me. What am I missing in the code?
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Address <> "$P$52" Then Exit Sub
With ActiveSheet
Select Case Target.Value
Case "Horizontal - feet"
.Pictures("B3A").Visible = True
.Pictures("V1A").Visible = False
.Pictures("V1AF").Visible = False
Case "Vertical - simple"
.Pictures("B3A").Visible = False
.Pictures("V1A").Visible = True
.Pictures("V1AF").Visible = False
Case "Vertical - lantern"
.Pictures("B3A").Visible = False
.Pictures("V1A").Visible = False
.Pictures("V1AF").Visible = True
End Select
End With
If Target.Address <> "$P$117" Then Exit Sub
With ActiveSheet
Select Case Target.Value
Case "Right"
.Pictures("3P1").Visible = True
.Pictures("3P1M").Visible = False
Case "Left"
.Pictures("3P1").Visible = False
.Pictures("3P1M").Visible = True
End Select
End With
End Sub
Thanks for your help.
Think through the logic of your if statements causing you to exit the sub.
If the cell is P117, you will hit the first if statement which causes you to exit the sub immediately. So you will never get to your second check.
Embed the logic for each of your operations in if statements like I show here and you will be able to "do something if the cell range is P52 or P117" a bit more appropriately.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
'only do the following operation if your cell address is P52 - don't exit out
'of your entire code if it's not
If Target.Address = "$P$52" Then
With ActiveSheet
Select Case Target.Value
Case "Horizontal - feet"
.Pictures("B3A").Visible = True
.Pictures("V1A").Visible = False
.Pictures("V1AF").Visible = False
Case "Vertical - simple"
.Pictures("B3A").Visible = False
.Pictures("V1A").Visible = True
.Pictures("V1AF").Visible = False
Case "Vertical - lantern"
.Pictures("B3A").Visible = False
.Pictures("V1A").Visible = False
.Pictures("V1AF").Visible = True
End Select
End With
End If
'you skip to down here if it is NOT P52, which then lets you check again to
'see if it's P117
If Target.Address = "$P$117" Then
With ActiveSheet
Select Case Target.Value
Case "Right"
.Pictures("3P1").Visible = True
.Pictures("3P1M").Visible = False
Case "Left"
.Pictures("3P1").Visible = False
.Pictures("3P1M").Visible = True
End Select
End With
End If
End Sub
If you are going to have a lot of checks like this, you may want to create a Select case statement for Target.Address too. It's hard to say which is better for you given what you've asked here.
Related
When I run the following snippet, and enter an acceptable value, I get the desired result.
do while len(strselect) = 0 'or strselect<>"1" or strselect<>"2" or strselect<>"3"
strselect = inputbox ("Please select:" &vbcrlf&vbcrlf&_
"1. Add an entry" &vbcrlf&vbcrlf&_
"2. Remove an entry" &vbcrlf&vbcrlf&_
"3. Search for an entry" &vbcrlf, "Contact Book")
if isempty(strselect) then
wscript.quit()
elseif strselect="1" then
wscript.echo "You chose 1"
elseif strselect="2" then
wscript.echo "You chose 2"
elseif strselect="3" then
wscript.echo "You chose 3"
end if
loop
However if I try constrain the validation process further (by including the remark in the do while conditions), and run the snippet again, I get the corresponding if condition triggered, but the do loop continues, instead of exiting.
I've tried using isnumeric and cstr on the do loop strselect conditions, with no joy... What am I missing to get the darn thing to exit the loop?
You have a problem with the logic in the condition
condition 1 condition 2 condition 3 condition 4
v----------------v v------------v v------------v v............v
do while len(strselect) = 0 or strselect<>"1" or strselect<>"2" or strselect<>"3"
Depending on value inside strselect, you have
value c1 c2 c3 c4
len=0 <>"1" <>"2" <>"3" c1 or c2 or c3 or c4
-------------------------------------- --------------------
empty true true true true true
1 false false true true true
2 false true false true true
3 false true true false true
other false true true true true
In each line you have at least one condition evaluated as true, and as you are concatenating the conditions with Or operators (evaluate to true if at least one of the values is true), the full condition is evaluated as true and the code keeps looping
You only need to change the condition
Do While strselect<>"1" And strselect<>"2" And strselect<>"3"
Do While Not (strselect="1" Or strselect="2" Or strselect="3")
....
I need this to effect only the current record within the continuous form.
On the Offender Type I select "offender 1" and the code correctly sets all four fields not enabled.
However when I go record 2, in the Offender Type, I select "Offender 2" the code incorrectly sets all four fields to enabled of every record of the continuous form not just the fields for record 2.
Combo box Name: Offender_Type
Selections for Offender_Type: Offender 1; Offender 2; Offender 3
Private Sub Offender_Type_AfterUpdate()
If Me.Offender_Type.Value = "offender 1 " Then
Me.Field1.Enabled = False
Me.Field2.Enabled = False
Me.Field3.Enabled = False
Me.field4.Enabled = False
ElseIf Me.Offender_Type.Value <> "offender 1" Then
Me.Field1.Enabled = True
Me.Field2.Enabled = True
Me.Field3.Enabled = True
Me.field4.Enabled = True
Else
End If
End Sub
I have an Access 2010 database with a form that pulls up a list of all of the students that a teacher teaches. One of the fields is "Classperiod"; each teacher teaches 5 classes. I have a set of toggle buttons in an option group (Frame 103) that I'd like to use to filter the list of records by "Classperiod". So, for example, clicking on one of the toggle buttons would only show students from a particular class. Here's the code that I have for the option group in the "After Update" of the Events for the option group:
Private Sub Frame103_AfterUpdate()
Select Case Frame103
Case 1
Me.Filter = "Schedules.Classperiod = 1"
Me.FilterOn = True
Case 2
Me.Filter = "Schedules.Classperiod = 2"
Me.FilterOn = True
Case 3
Me.Filter = "Schedules.Classperiod = 3"
Me.FilterOn = True
Case 5
Me.Filter = "Schedules.Classperiod = 5"
Me.FilterOn = True
Case 6
Me.Filter = "Schedules.Classperiod = 6"
Me.FilterOn = True
Case 7
Me.Filter = "Schedules.Classperiod = 7"
Me.FilterOn = True
Case 8
Me.Filter = "Schedules.Classperiod = 8"
Me.FilterOn = True
Case Else
Me.FilterOn = False
End Select
End Sub
Schedules is the Table that Classperiod is a field for.
Right now the code isn't doing anything. Any suggestions would be welcome!
Your filter can still work if you remove your "Schedule". Just use the fieldname:
Eg.
Case 7
Me.Filter = "Classperiod = 7"
Me.FilterOn = True
The best way however, would be to restrict your record source by the data meeting your criteria:
eg:
Case ....
Me.RecordSource = "Select * FROM Schedules WHERE Classperiod =..."
Me.Requiry
....
If you want to filter based on five different class periods, make your option group with five toggle buttons, the caption of each corresponding to a different class period e.g. toggle button 1 has caption "1" and so on. Here's the code:
Private Sub Frame103_Click()
Dim Caption As String
Caption = Frame103.Controls.Item(Frame103.Value - 1).Caption
Me.SubformName.Form.Filter = "[Classperiod] = """ & Caption & """"
Me.SubformName.Form.FilterOn = True
End Sub
Basically I have the following code, which I produced on a PC desktop using Excel 2010. And it worked just fine. However, when I transferred the file to Mac and executed the getStockDataTest() subroutine using Excel 2011, it failed miserably.
I kept getting this error message
Option Explicit
Sub getStockDataTest()
getGoogleStockHistory 700
End Sub
Sub getGoogleStockHistory(gInt As Long)
'
' Macro1 Macro
'load google stock hisotry
'
'.add -> create object
'With ActiveSheet.QueryTables.Add(Connection:= _
'"URL;https://www.google.com.hk/finance/historical?q=HKG%3A000&ei=V5k-U4CjHtDakQWfQw#" _
, Destination:=Range("$A$1"))
With ThisWorkbook.Sheets("Query").QueryTables(1)
.Connection = "URL;https://www.google.com.hk/finance/historical?q=HKG%3A" & Format(gInt, "0000") & "&num=200" '&num 200 = 200 days of data
.Name = "WebQuery"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables 'xlEntirePage will get entire webpage
.WebTables = "4"
.WebFormatting = xlWebFormattingNone
.WebDisableDateRecognition = False
.Refresh BackgroundQuery:=False
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableRedirections = False
End With
Range("A1").Select
End Sub
Furthermore, I want to ask how to do Import from web in Mac Excel 2011, because I cannot find such icon. Please help me out here, as I am quite lost here.
This site says (see number 9 in the list) that PreserveFormatting doesn't exist on Mac and RefreshPeriod may be different...
Deleting these 2 statements may solve your problem.
The only recommendation I have is to remove the inline comments as it might be part of the issue. Here is a link to a similar question on how to create a web query on a Mac.
From what I can gather from a couple of Google queries, VBA within Excel 2011 Mac is nearly identical to the VBA on PC so there shouldn't be an issue. Several forums I frequent recommend this site to understand the differences that still exist.
I am very new with programming and I have come across an issue in Visual Basic that I cannot figure out. Many forums and YouTube videos later I still do not have an answer.
I am using a nested selection structure and within it is two Message boxes. I cannot figure out how to get the second dialog result to trigger the elseif statement. It just skips over it. I believe since I have one variable declared for a dialog result it is checking both of them, but in this case I don't know how to declare only the second dialog result.
Here is the code so far.
Dim dblTotal As Double = 12
Dim strResponse As DialogResult
' Dialog box asking about a coupon and $2 coupon.
If MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo) = vbYes AndAlso
MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo) = vbNo Then
lblTotal.Text = Convert.ToString(dblTotal - 4)
' Meant to be ran if statement is false. I dont Understand
' why it is skipping over and not executing.
' Is "dlgResult" reading the first one as well? How do I correct?
ElseIf strResponse = vbYes Then
lblTotal.Text = Convert.ToString(dblTotal - 2)
Else
lblTotal.Text = Convert.ToString(dblTotal)
End If
End Sub
I understand it would be easier to to code if the first message = vbNo, but I was trying to see if this way would work.
Thank you!!
Is this how you wanted it?
Dim dialog1 As DialogResult
Dim dialog2 As DialogResult
Dim dblTotal As Double = 12
dialog1 = MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo)
dialog2 = MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo)
If dialog1 = DialogResult.OK Then
dblTotal = dblTotal - 2
End If
If dialog2 = DialogResult.OK Then
dblTotal = dblTotal - 2
End If
lblTotal.Text = Convert.ToString(dblTotal - 2)