I'm making a VBA-project and want to simplify the following if-statements, because i need to this following 11 times more, for each person.
'CTK opportunities
If result = "CTK" And ToggleButton1 = True Then Range("B4").Value = "X"
If result = "CTK" And ToggleButton2 = True Then Range("C4").Value = "X"
If result = "CTK" And ToggleButton3 = True Then Range("D4").Value = "X"
If result = "CTK" And ToggleButton4 = True Then Range("E4").Value = "X"
If result = "CTK" And ToggleButton5 = True Then Range("F4").Value = "X"
If result = "CTK" And ToggleButton6 = True Then Range("G4").Value = "X"
If result = "CTK" And ToggleButton7 = True Then Range("H4").Value = "X"
If result = "CTK" And ToggleButton8 = True Then Range("I4").Value = "X"
If result = "CTK" And ToggleButton9 = True Then Range("J4").Value = "X"
If result = "CTK" And ToggleButton10 = True Then Range("K4").Value = "X"
If result = "CTK" And ToggleButton11 = True Then Range("L4").Value = "X"
If result = "CTK" And ToggleButton12 = True Then Range("M4").Value = "X"
'Next person
you could try this:
Dim ctrl As Control
If result = "CTK" Then
For Each ctrl In Me.Controls
If InStr(ctrl.Name, "ToggleButton") > 0 Then
If ctrl.Value Then Cells(4, CInt(Replace(ctrl.Name, "ToggleButton", "")) + 1).Value = "X"
End If
Next ctrl
End If
In addition to factorizing the test on "CTK", you can also define an array for your ToggleButtons and iterate on it:
toggleButtons = Array(ToggleButton1, ToggleButton2, .... , ToggleButton12)
If(result = CTK) Then
For i = 0 to UBound(toggleButtons)
If toggleButtons[i] = True Then Cells(4, i+2).Value = "X"
Next
End If
If result = "CTK" Then
If ToggleButton1 = True Then
Range("B4").Value= "X"
ElseIf ToggleButton2 = True Then
Range("C4").Value = "X"
ElseIf...........etc
Related
I am having trouble when running a macro whereby I get an error, usually at the end, stating that it "Out of memory".
I've looked over a number of posts and followed recommendations to clear any variables and ensure that the code is "cleaner". However, the error still persists.
I have included the code below, any feedback would be greatly appreciated.
A summary of the purpose:
User clicks a button in a template (which is where the code resides)
Macro 1 cleans up the data that the user pastes into a template page.
A userform will appear after macro 1 which the user must input some data - customer name, contract numbers, etc.
Macro 2 runs when the userform button is submitted which creates a new workbook containing all the data from the template (formatted in macro1) and the data from the form.
It then formats the data for printing with a logo, etc.
Macro 1
Private Sub ContractsClean()
Application.Visible = True
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.CutCopyMode = False
Sheets("Paste").Name = "Data"
Sheets("Data").Columns("A:B").EntireColumn.Delete
Dim x As Long, lastrow As Long
lastrow = Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
'Clean the codes
For x = lastrow To 2 Step -1
If Len(Sheets("Data").Cells(x, 1)) > 9 Then
Sheets("Data").Cells(x, 1).Value = Right(Sheets("Data").Cells(x, 1).Value, 9)
End If
Next x
'Add the leading zero
Sheets("Data").Select
Range("A:A").Select
Dim Rng As Range
Dim bChanged As Boolean
Dim icol As Long
Dim sString As String
Dim iWSs As Long
iWSs = Workbooks.Count
If iWSs >= 1 Then
icol = Selection.Column
lastrow = Cells(Rows.Count, icol).End(xlUp).Row
For Each Rng In Selection
If TypeName(Rng.Value) = "Double" Then
If Left(Rng.Value, 1) = "9" Then
Rng = "'0" & Rng.Value
bChanged = True
End If
End If
If TypeName(Rng.Value) = "String" Then
If Left(Rng.Value, 3) = "09-" Then
sString = Rng.Value
sString = "'" & Replace(sString, "-", "")
Rng.Value = sString
bChanged = True
End If
End If
If Rng.Errors(xlNumberAsText).Value = True And bChanged = True Then Rng.Errors(xlNumberAsText).Ignore = True
If Rng.Row = lastrow Then Exit For
Next Rng
End If
If iWSs >= 1 Then
icol = Selection.Column
lastrow = Cells(Rows.Count, icol).End(xlUp).Row
For Each Rng In Selection
If TypeName(Rng.Value) = "Double" Then
If Left(Rng.Value, 1) = "8" Then
Rng = "'0" & Rng.Value
bChanged = True
End If
End If
If TypeName(Rng.Value) = "String" Then
If Left(Rng.Value, 3) = "08-" Then
sString = Rng.Value
sString = "'" & Replace(sString, "-", "")
Rng.Value = sString
bChanged = True
End If
End If
If Rng.Errors(xlNumberAsText).Value = True And bChanged = True Then Rng.Errors(xlNumberAsText).Ignore = True
If Rng.Row = lastrow Then Exit For
Next Rng
End If
Rng = Empty
bChanged = Empty
icol = Empty
sString = Empty
iWSs = Empty
Sheets("Data").Columns("B").EntireColumn.Delete
Sheets("Data").Range("C1").Value = "Quantity"
Sheets("Data").Columns("D").EntireColumn.Delete
Sheets("Data").Columns("E").EntireColumn.Delete
Sheets("Data").Range("E:F").NumberFormat = "dd/mm/yyyy"
Dim ws As Worksheet
Set ws = Sheets("Data")
ws.Range("A:B").Locked = False
ws.Range("C:D").Locked = True
ws.Range("E:F").Locked = False
ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:= _
True, AllowDeletingRows:=True, AllowFormattingCells:=True, AllowFiltering:=True, AllowInsertingRows:=True, Password:="Sanchez7"
'Completion message box
Dim Answer As String
Dim MyNote As String
'Place your text here
MyNote = "Cleanse complete. Do you want to create a local price agreement?"
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Action")
If Answer = vbNo Then
'Code for No button Press
Else
'Code for Yes button Press
ProposalForm.Show
End If
Answer = Empty
MyNote = Empty
lastrow = Empty
End Sub
Form Code
Private Sub CommandButton1_Click()
Dim sBuyGroup As String, sLPA As String, sRep As String, sStart As Date, sEnd As Date, sCurrency As String
Dim bExit As Boolean, sSort As String
sBuyGroup = txt_BuyGroup.Text
sLPA = txt_LPA.Text
sRep = txt_Rep.Text
sStart = txt_Start.Text
sEnd = txt_End.Text
sCurrency = ComboBox1.Value
If sBuyGroup = "" Then
bExit = True
MsgBox ("The account number cannot be blank")
End If
If sLPA = "" And bExit = False Then
bExit = True
MsgBox ("The carriage option cannot be blank")
End If
If sRep = "" And bExit = False Then
bExit = True
MsgBox ("The discount option cannot be blank")
End If
If bExit = False Then
Call CreateProposal(sBuyGroup, sLPA, sRep, sStart, sEnd, sCurrency)
Unload Me
Else
End If
sBuyGroup = Empty
sLPA = Empty
sRep = Empty
sStart = Empty
sEnd = Empty
sCurrency = Empty
End Sub
Macro 3 (which I believe is what causes the error)
Sub CreateProposal(sBuyGroup As String, sLPA As String, sRep As String, sStart As Date, sEnd As Date, sCurrency As String)
Dim picLogo As Picture
Set picLogo = Sheets("Info").Pictures("PH_LOGO")
'Copy data from original workbook
Dim exApp As Excel.Application
Set exApp = GetExcelObject()
exApp.Visible = True
Dim OGWB As Workbook
Set OGWB = ActiveWorkbook
OGWB.Sheets("Data").Range("A1:F15000").Copy
Dim wbProposal As Workbook
Set wbProposal = exApp.Workbooks.Add
wbProposal.Activate
wbProposal.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
wbProposal.Sheets("Sheet1").Name = "Raw"
exApp.CutCopyMode = False
exApp.Application.ScreenUpdating = False
'Copy the logo so we can close original workbook
wbProposal.Sheets.Add
wbProposal.Sheets("Sheet2").Name = "LPA"
OGWB.Sheets("Info").Pictures("PH_LOGO").Cut
wbProposal.Sheets("LPA").Paste
Selection.Name = "PH_LOGO"
wbProposal.Sheets("LPA").Pictures("PH_LOGO").Left = 20
wbProposal.Sheets("LPA").Pictures("PH_LOGO").Top = 13
'wbProposal.Sheets("LPA").Pictures("PH_LOGO").Width = Application.CentimetersToPoints(5.51)
'wbProposal.Sheets("LPA").Pictures("PH_LOGO").Height = Application.CentimetersToPoints(1.64)
wbProposal.Sheets("LPA").Range("A1").Select
Set picLogo = Nothing
exApp.CutCopyMode = False
wbProposal.Activate
wbProposal.Sheets("LPA").Select
'Set BG to all white
With wbProposal.Sheets("LPA").Cells.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'Add contact header
wbProposal.Sheets("LPA").Range("D2").Value = "performancehealth.co.uk"
wbProposal.Sheets("LPA").Range("D3").Value = "Main: +44 (0) 3448 730 035"
wbProposal.Sheets("LPA").Range("D4").Value = "Fax: +44 (0) 1623 557 769"
wbProposal.Sheets("LPA").Range("G2").Value = "Nunn Brook Road, Huthwaite,"
wbProposal.Sheets("LPA").Range("G3").Value = "Sutton-in-Ashfield"
wbProposal.Sheets("LPA").Range("G4").Value = "Nottinghamshire, NG17 2HU, UK"
With wbProposal.Sheets("LPA").Range("D2").Font
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0
.Bold = True
End With
With wbProposal.Sheets("LPA").Range("D3:D4,G2:G4").Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0.499984740745262
End With
wbProposal.Sheets("LPA").Range("G6").Value = "Local Price Agreement"
With wbProposal.Sheets("LPA").Range("G6").Font
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0
.Bold = True
.Size = 14
End With
'Arrange Column widths
wbProposal.Sheets("LPA").Columns("A").ColumnWidth = 2.5
wbProposal.Sheets("LPA").Columns("B").ColumnWidth = 13
wbProposal.Sheets("LPA").Range("B14").Value = "Item Code"
wbProposal.Sheets("LPA").Columns("C").ColumnWidth = 41.5
wbProposal.Sheets("LPA").Range("C14").Value = "Item Description"
wbProposal.Sheets("LPA").Columns("D:F").ColumnWidth = 12
wbProposal.Sheets("LPA").Range("D14").Value = "Sale UOM"
wbProposal.Sheets("LPA").Range("E14").Value = "Min Qty"
wbProposal.Sheets("LPA").Range("F14").Value = "Rate"
wbProposal.Sheets("LPA").Columns("G:H").ColumnWidth = 13.5
wbProposal.Sheets("LPA").Range("G14").Value = "Start Date"
wbProposal.Sheets("LPA").Range("H14").Value = "Expiry Date"
wbProposal.Sheets("LPA").Range("B14:H14").Font.Bold = True
With wbProposal.Sheets("LPA").Range("B14:H14").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -4.99893185216834E-02
.PatternTintAndShade = 0
End With
'Fill out details from form
wbProposal.Sheets("LPA").Range("B8").Value = "Buying Group:"
wbProposal.Sheets("LPA").Range("B10").Value = "Sales Rep:"
wbProposal.Sheets("LPA").Range("B11").Value = "Currency:"
wbProposal.Sheets("LPA").Range("E8").Value = "LPA Number:"
wbProposal.Sheets("LPA").Range("E10").Value = "Start Date:"
wbProposal.Sheets("LPA").Range("E11").Value = "Expiry Date:"
wbProposal.Sheets("LPA").Range("B8:B11").Font.Bold = True
wbProposal.Sheets("LPA").Range("E8:E11").Font.Bold = True
wbProposal.Sheets("LPA").Range("C8").Value = sBuyGroup
wbProposal.Sheets("LPA").Range("C10").Value = sRep
wbProposal.Sheets("LPA").Range("C11").Value = sCurrency
wbProposal.Sheets("LPA").Range("F8").Value = sLPA
wbProposal.Sheets("LPA").Range("F10").Value = sStart
wbProposal.Sheets("LPA").Range("F11").Value = sEnd
sBuyGroup = Empty
sRep = Empty
sCurrency = Empty
sLPA = Empty
sStart = Empty
sEnd = Empty
'Import the list
Dim lastrow As Long
wbProposal.Sheets("Raw").Select
lastrow = wbProposal.Sheets("Raw").Cells(Rows.Count, "A").End(xlUp).Row
wbProposal.Sheets("Raw").Columns("C").Insert Shift:=xlToRight
wbProposal.Sheets("Raw").Range("F:G").NumberFormat = "dd/mm/yyyy"
wbProposal.Sheets("Raw").Range("E" & lastrow).NumberFormat = "£#,##0.00"
wbProposal.Sheets("Raw").Range("A2:G" & lastrow).Copy
wbProposal.Sheets("LPA").Select
wbProposal.Sheets("LPA").Range("B15").PasteSpecial xlPasteValuesAndNumberFormats
exApp.CutCopyMode = False
'Setup print area
lastrow = wbProposal.Sheets("LPA").Cells(Rows.Count, "B").End(xlUp).Row
wbProposal.Sheets("LPA").Range("F15:F" & lastrow).NumberFormat = "£#,##0.00"
wbProposal.Sheets("LPA").PageSetup.PrintArea = "$A$1:$H$" & lastrow
With wbProposal.Sheets("LPA").PageSetup
.PrintTitleRows = "$14:$14"
.PrintTitleColumns = ""
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 100
End With
'Redo the picture size (changes during column amendments
'wbProposal.Sheets("LPA").Pictures("PH_LOGO").Width = Application.CentimetersToPoints(5.51)
'wbProposal.Sheets("LPA").Pictures("PH_LOGO").Height = Application.CentimetersToPoints(1.64)
lastrow = Empty
exApp.DisplayAlerts = False
exApp.CutCopyMode = False
wbProposal.Sheets("LPA").Range("A1").Select
wbProposal.Sheets("Raw").Delete
exApp.DisplayAlerts = False
exApp.ScreenUpdating = True
OGWB.Close False
Set OGWB = Nothing
Set wbProposal = Nothing
Set exApp = Nothing
Application.Quit
End Sub
'-----------------------------------------------------------------------------
' Return an intance of Excel
' First tries to open an existing instance. If it fails, it will create an instance.
' If that fails too, then we return 'Nothing'
'-----------------------------------------------------------------------------
Public Function GetExcelObject() As Object
On Error Resume Next
Dim xlo As Object
' Try to get running instance of Excel
Set xlo = GetObject("Excel.Application")
If xlo Is Nothing Then
Set xlo = CreateObject("Excel.Application")
End If
Set GetExcelObject = xlo
End Function
I have code that calculates stress at multiple points on a tank and then graphs these stresses on a Chart. I'll be viewing charts with no problem, and all of a sudden, the chart displays a red X and I need to close my program to view the stress results again.
I know there's no bug in my code. No exception is thrown. Any suggestions?
Public Sub Graph(Curves) 'Creates the Graphs
crtStressData.Series.Clear()
crtStressData.Legends.Clear()
'Dimension arrays for data points
FileClose()
Dim data As Double
Dim StressTitle As String
Dim k As Integer
Dim BorderSize As Integer = 4
'set up main graph
crtStressData.ChartAreas(0).CursorX.IsUserSelectionEnabled = True
crtStressData.ChartAreas(0).CursorY.IsUserSelectionEnabled = True
crtStressData.ChartAreas(0).AxisX.ScaleView.Zoomable = True
crtStressData.ChartAreas(0).AxisY.ScaleView.Zoomable = True
crtStressData.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
crtStressData.ChartAreas(0).AxisY.ScrollBar.IsPositionedInside = False
Dim LegendName As String = ""
StressTitle = TankName + " " + GraphType + " Stresses: " + strLoad + " Loading"
If GraphType = "Combined" Then
'Set component parameters
'Create Combined Graph
Call CreateDataPoints()
'Add title,
crtStressData.Titles(0).Text = StressTitle
For i = 0 To Curves - 1 'add legends
If i = 0 Then
LegendName = "Minimum Principal Stress"
ElseIf i = 1 Then
LegendName = "Minimum Principal Stress Location"
ElseIf i = 2 Then
LegendName = "Maximum Principal Stress"
ElseIf i = 3 Then
LegendName = "Maximum Principal Stress Location"
End If
crtStressData.Series.Add(LegendName)
''work on legend name
crtStressData.Series(i).ChartType = DataVisualization.Charting.SeriesChartType.Line 'sets as a line graph
crtStressData.Series(i).XAxisType = DataVisualization.Charting.AxisType.Primary
crtStressData.Series(i).YAxisType = DataVisualization.Charting.AxisType.Primary
crtStressData.Series(i).BorderWidth = BorderSize
For j = 0 To MaxDataPoints 'add points per legend type
'set up which data to add and define color
If i = 0 Then
' StressType(k) = "Minimum Principal Stress"
data = SMINMAXData(j)
color = Drawing.Color.Blue
ElseIf i = 1 Then
' StressType(k) = "Minimum Principal Stress Location"
data = SMINLOCData(j)
color = Drawing.Color.Green
ElseIf i = 2 Then
'StressType(k) = "Maximum Principal Stress"
data = SMAXMAXData(j)
color = Drawing.Color.DodgerBlue
ElseIf i = 3 Then
' StressType(k) = "Minimum Principal Stress Location"
data = SMAXLOCData(j)
color = Drawing.Color.Red
End If
'add data and color
crtStressData.Series(i).Points.AddXY(XPosData(j), data)
crtStressData.Series(i).Color = color
Next
k = k + 1
Next
Else ' GraphType = "Components"
crtStressData.Series.Clear()
'Set component parameters
'Create Component Graph
Call CreateDataPoints()
'Add title and axes labels
crtStressData.Titles(0).Text = StressTitle
For i = 0 To Curves - 1 'add legends
If i = 0 Then
LegendName = "Bending"
ElseIf i = 1 Then
LegendName = "Pressure (X)"
ElseIf i = 2 Then
LegendName = "Tension (X)"
ElseIf i = 3 Then
LegendName = "Total (X)"
ElseIf i = 4 Then
LegendName = "Pressure (Y)"
ElseIf i = 5 Then
LegendName = "Shear"
End If
crtStressData.Series.Add(LegendName) 'creates line
crtStressData.Series(i).ChartType = DataVisualization.Charting.SeriesChartType.Line 'sets as a line graph
crtStressData.Series(i).XAxisType = DataVisualization.Charting.AxisType.Primary
crtStressData.Series(i).YAxisType = DataVisualization.Charting.AxisType.Primary
crtStressData.Series(i).BorderWidth = BorderSize
For j = 0 To MaxDataPoints 'add points per legend
If i = 0 Then
'StressType(k) = "Bending"
data = SXBMAXData(j)
color = Drawing.Color.Blue
ElseIf i = 1 Then
' StressType(k) = "Pressure (X)"
data = SXPMAXData(j)
color = Drawing.Color.Green
ElseIf i = 2 Then
' StressType(k) = "Tension (X)"
data = SXTMAXData(j)
color = Drawing.Color.DodgerBlue
ElseIf i = 3 Then
'StressType(k) = "Total (X)"
data = SXMAXData(j)
color = Drawing.Color.Red
ElseIf i = 4 Then
' StressType(k) = "Pressure (Y)"
data = SYMAXData(j)
color = Drawing.Color.HotPink
ElseIf i = 5 Then
' StressType(k) = "Shear"
data = SSMAXData(j)
color = Drawing.Color.DarkOrange
End If
'add data and color
crtStressData.Series(i).Points.AddXY(XPosData(j), data)
crtStressData.Series(i).Color = color
Next
k = k + 1
Next
End If
'LEGEND
Dim StressLegend As Legend = New Legend()
StressLegend.IsDockedInsideChartArea = False
StressLegend.Docking = Docking.Bottom
StressLegend.Font = New Font("Gil Sans MT", 11)
StressLegend.TextWrapThreshold = 100
StressLegend.Alignment = StringAlignment.Center
crtStressData.Legends.Add(StressLegend)
crtStressData.Dock = DockStyle.None
crtStressData.Show()
End Sub
I am working on a sheet that has sections which hides/shows a number of rows based on a cell value (between 1-10). At the moment, I have a handful of nested if statements. This has made my workbook painfully slow. Is there a way to shrink this code? Thanks.
If Range("B87").Value = 10 Then
Rows("88:98").EntireRow.Hidden = False
Else
If Range("B87").Value = 9 Then
Rows("98").EntireRow.Hidden = True
Rows("88:97").EntireRow.Hidden = False
Else
If Range("B87").Value = 8 Then
Rows("97:98").EntireRow.Hidden = True
Rows("88:96").EntireRow.Hidden = False
Else
If Range("B87").Value = 7 Then
Rows("96:98").EntireRow.Hidden = True
Rows("88:95").EntireRow.Hidden = False
Else
If Range("B87").Value = 6 Then
Rows("95:98").EntireRow.Hidden = True
Rows("88:94").EntireRow.Hidden = False
Else
If Range("B87").Value = 5 Then
Rows("94:98").EntireRow.Hidden = True
Rows("88:93").EntireRow.Hidden = False
Else
If Range("B87").Value = 4 Then
Rows("93:98").EntireRow.Hidden = True
Rows("88:92").EntireRow.Hidden = False
Else
If Range("B87").Value = 3 Then
Rows("92:98").EntireRow.Hidden = True
Rows("88:91").EntireRow.Hidden = False
Else
If Range("B87").Value = 2 Then
Rows("91:98").EntireRow.Hidden = True
Rows("88:90").EntireRow.Hidden = False
Else
If Range("B87").Value = 1 Then
Rows("90:98").EntireRow.Hidden = True
Rows("88:89").EntireRow.Hidden = False
Else
If Range("B87").Value = 0 Then
Rows("88:98").EntireRow.Hidden = True
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
You have a whole lot of basically the same code. I took a look and tried to make it more arithmetical, which shortens the code. See if this works:
Sub t()
Dim myVal As String
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
mainRow = 98
myVal = Range("B87").Value
If myVal = 10 Then
Rows(mainRow - 10 & ":" & mainRow - 10 + myVal).EntireRow.Hidden = False
ElseIf myVal >= 1 And myVal <= 9 Then
tweakRow = mainRow - 10
row1 = (mainRow - (9 - myVal))
row2 = (mainRow - (10 - myVal))
Set hideRange = Rows(row1 & ":" & mainRow).EntireRow
Set showRange = Rows(tweakRow & ":" & row2).EntireRow
Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
hideRange.Hidden = True
showRange.Hidden = False
ElseIf myVal = 0 Then
Rows(mainRow - 10 & ":" & mainRow).EntireRow.Hidden = True
End If
End Sub
I might try a case statement.
Oh, or even use the ElseIf option which would reduce the amount of EndIf statements at the very least.
I think the case code looks something like this:
Select Range("B87").value
Case "1"
Case "2"
...
End Select
You don't need to use EntireRow when using Rows or 'EntireColumnwhen usingColumns`.
Rows("88:98").Hidden = True
If Range("B87").Value > 0 Then
Rows(88).Resize(1 + Range("B87").Value).Hidden = False
End If
Please help I want to know exactly what is going on in this code for a questions and answers exam tomorrow.
I don't need any help with writing the code because that would be cheating. I made a tiny few mistakes please forgive me I rectified most of theses I don't need help with the mistakes just with the comments and understanding mostly how it works.
Private Sub Command1_Click()
MSComm1.Output = "83" + Chr$(13)
End Sub
Private Sub Form_Load()
MSC1.PortOpen = True
Average_val = 0
minimum_val = 255
maximum_val = 0
Screenshotofsinewave.Left = 0
Screenshotofsinewave.Channel(0).TraceVisible = True
Screenshotofsinewave.Channel(0).MarkersVisible = True
sumofall_val = 0
Screenshotofsinewave.TitleVisible = False
Screenshotofsinewave.Top = 0
Screenshotofrectifiedsinewave.TitleVisible = False
Screenshotofrectifiedsinewave.Channel(0).TraceVisible = True
Screenshotofrectifiedsinewave.Channel(0).MarkersVisible = True
Screenshotofrectifiedsinewave.Top = 0
Screenshotofrectifiedsinewave.Left = 0
Screenshotoflevelshiftedsinewave.Top = 0
Screenshotoflevelshiftedsinewave.Left = 0
Screenshotoflevelshiftedsinewave.TitleVisible = False
Screenshotoflevelshiftedsinewave.Channel(0).TraceVisible = True
Screenshotoflevelshiftedsinewave.Channel(0).MarkersVisible = True
End Sub
Private Sub MSC1_OnComm()
Dim number_val
Dim number1_val
Dim Average_val
Dim com1_val
p = 0
q = 0
r = 0
s = 0
t = 0
Dim Xarr(50) As Single
Dim Yarr(50) As Single
Dim number2_val
Dim number3_val
Dim Snapshotofsinewave
Dim string1_out As String
Dim string1_in As String
Dim counter As Single
Dim sample_rate As Integer
Select Case MSC1.CommEvent
Case comEvReceive
minimum_val = 255
string1_in = MSC1.Input
Screenshotofsinewave.Channel(0).Clear
Screenshotofrectifiedsinewave.Channel(0).Clear
Screenshotoflevelshiftedsinewave.Channel(0).Clear
counter = 0
comm_count = comm_count + 1
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
counter = counter + 1
number_val = Val(string1_out)
Xarr(counter) = counter
Yarr(counter) = number_val
Screenshotofsinewave.Channel(0).AddXY counter, number_val
If number_val > maximum_val Then
maximum_val = number_val
MaxVoltage.Value = maximum_val
End If
If number_val < minimum_val Then
minimum_val = number_val
MinVoltage.Value = number_val
End If
sumofall_val = number_val + sumofall_val
Average_value = sumofall_val / 50
AverageVoltage.Value = Average_value
Next sample_rate
counter = 0
sumofall_val = 0
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
counter = counter + 1
number_val = Val(string1_out)
number_val = number1_val - Average_value
number_val = numer_val
If num_val1 < 0 Then
number_val = number_val * -1
End If
Xarr(counter) = counter
Yarr(counter) = number1_val
Screenshotofrectifiedsinewave.Channel(0).AddXY counter, number1_val
Next sample_rate
counter = 0
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
Count = Count + 1
number_val = Val(string1_out)
number2_val = number_val + Average_value
Xarr(Count) = counter
Yarr(Count) = number2_val
LevelShifted.Channel(0).AddXY Count, number_val2
sumofall_val = number_val + sumofall_val
Next sample_rate
counter = com1_val
Snapshotofsinewave.Value = com1_val
End Select
End Sub
Private Sub Frame4_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
Private Sub Uploaddata_Click()
If GXSwitch1.SwitchOn = True Then
led1.LampOn = True
p = 8
Else
led1.LampOn = False
p = 0
End If
If GXSwitch2.SwitchOn = True Then
led2.LampOn = True
q = 4
Else
led2.LampOn = False
q = 0
End If
If GXSwitch3.SwitchOn = True Then
led3.LampOn = True
r = 4
Else
led3.LampOn = False
r = 0
End If
If GXSwitch4.SwitchOn = True Then
led4.LampOn = True
s = 8
Else
led4.LampOn = False
s = 0
End If
t = p + q + r + s
If t = 0 Then
MSC1.Output = "0" + Chr$(13)
End If
If t = 1 Then
MSC1.Output = "1" + Chr$(13)
End If
If t = 2 Then
MSC1.Output = "2" + Chr$(13)
End If
If t = 3 Then
MSC1.Output = "3" + Chr$(13)
End If
If t = 4 Then
MSC1.Output = "4" + Chr$(13)
End If
If t = 5 Then
MSC1.Output = "5" + Chr$(13)
End If
If t = 6 Then
MSC1.Output = "6" + Chr$(13)
End If
If t = 7 Then
MSC1.Output = "7" + Chr$(13)
End If
If t = 8 Then
MSC1.Output = "8" + Chr$(13)
End If
If t = 9 Then
MSC1.Output = "9" + Chr$(13)
End If
If t = 10 Then
MSC1.Output = "10" + Chr$(13)
End If
If t = 11 Then
MSC1.Output = "11" + Chr$(13)
End If
If t = 12 Then
MSC1.Output = "12" + Chr$(13)
End If
If t = 13 Then
MSC1.Output = "13" + Chr$(13)
End If
If t = 14 Then
MSC1.Output = "14" + Chr$(13)
End If
If t = 15 Then
MSC1.Output = "15" + Chr$(13)
End If
End Sub
Depends on several factors...
Dim average_val, x As Decimal ' decimal
Dim average_val = 3.2D ' decimal if Option Infer On
Dim average_val = 3.2D ' object with boxed decimal if Option Infer Off
Dim average_val ' Object if Option Strict Off, otherwise an error
Please can somebody help me with the correct DIM statements and syntax to simplify the following into a DO UNTIL loop?:
Sub DesRisk_Loader()
Dim Qn(7) As String
Dim Ys(7) As String
Dim No(7) As String
Dim Wk(7) As Integer
Application.ScreenUpdating = False
n = 1
x = 1
Do
Application.Goto Reference:="DesHome"
ActiveCell.Offset(x, 0).Select
Qn(n) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Ys(n) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
No(n) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Wk(n) = ActiveCell.Value
x = x + 2
n = n + 1
Loop Until n = 8
''Q.1
If Qn(1) <> "" Then
DesForm.DesFrame1.Visible = True
DesForm.Dq1.Caption = Qn(1)
FH = 0
If Ys(1) = "P" Then
DesForm.D1y.Value = True
Else
DesForm.D1y.Value = False
End If
If No(1) = "O" Then
DesForm.D1n.Value = True
Else
DesForm.D1n.Value = False
End If
DesForm.DesDly1.Value = Wk(1)
Else:
Exit Sub
End If
''Q.2
If Qn(2) <> "" Then
DesForm.DesFrame2.Visible = True
DesForm.Dq2.Caption = Qn(2)
FH = 1
If Ys(2) = "P" Then
DesForm.D2y.Value = True
Else
DesForm.D2y.Value = False
End If
If No(2) = "O" Then
DesForm.D2n.Value = True
Else
DesForm.D2n.Value = False
End If
DesForm.DesDly2.Value = Wk(2)
Else: GoTo Jump1
End If
''Q.3
If Qn(3) <> "" Then
DesForm.DesFrame3.Visible = True
DesForm.Dq3.Caption = Qn(3)
FH = 2
If Ys(3) = "P" Then
DesForm.D3y.Value = True
Else
DesForm.D3y.Value = False
End If
If No(3) = "O" Then
DesForm.D3n.Value = True
Else
DesForm.D3n.Value = False
End If
DesForm.DesDly3.Value = Wk(3)
Else: GoTo Jump1
End If
ditto till..
''Q.7
If Qn(7) <> "" Then
DesForm.DesFrame7.Visible = True
DesForm.Dq7.Caption = Qn(7)
FH = 6
If Ys(7) = "P" Then
DesForm.D7y.Value = True
Else
DesForm.D7y.Value = False
End If
If No(7) = "O" Then
DesForm.D7n.Value = True
Else
DesForm.D7n.Value = False
End If
DesForm.DesDly7.Value = Wk(7)
Else: GoTo Jump1
End If
Jump1:
DesForm.Height = 140 + (FH * 75)
DesForm.DesOK.Top = 85 + (FH * 75)
DesForm.DesCancel.Top = 85 + (FH * 75)
Load DesForm
DesForm.Show
End Sub
Thanks
Scott
At the top of your code (First Line in the entire module), type the following OPTION EXPLICIT
That will help identify all undeclared variables.