问题
I trying to create a tool using VBA excel. I'm am an amateur at this and need some help. I have a dedicated button/macro that uses a mess load of codes if/then statements. I need this one button to do what I want it to do, this is what I have. I'm getting the Procedure Too Large error, please help
Private Sub Save_Click()
Dim ws As Worksheet
Set ws = Worksheets("Sales Tracker")
Dim newRow As Long
newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
ws.Cells(newRow, 1).Value = Me.AcctNum.Value
If AcctNum = "" Then
ws.Cells(newRow, 1).Value = "--------"
End If
If CableCB = True And Cable = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And Cable = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And Cable = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And Cable = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And Cable = "private" Then
ws.Cells(newRow, 3).Value = "Cable"
ElseIf CableCB = True And Cable = "--------" Then
ws.Cells(newRow, 3).Value = "ERROR"
ElseIf CHSICB = True And CHSI = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CHSICB = True And CHSI = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CHSICB = True And CHSI = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CHSICB = True And CHSI = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CHSICB = True And CHSI = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CHSICB = True And CHSI = "--------" Then
ws.Cells(newRow, 3).Value = "ERROR"
ElseIf CDVCB = True And CDV = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CDVCB = True And CDV = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CDVCB = True And CDV = "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CDVCB = True And CDV = "--------" Then
ws.Cells(newRow, 3).Value = "ERROR"
ElseIf XH = True Then
ws.Cells(newRow, 3).Value = "private"
End If
If private= True And private= True And Cable = "private" And private= "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And private= True And Cable = "private" And private= "--------" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And private= True And Cable = "private" And private= "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And private= True And Cable = "private" And private= "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And CHSICB = True And Cable = "private" And private= "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And CHSICB = True And Cable = "private" And private= "private" Then
ws.Cells(newRow, 3).Value = "private"
ElseIf CableCB = True And private= True And private= "Cable" And CDV = "private" Then
and so on and so on!!!
回答1:
It seems to me you need to rethink your structure a lot. I assume you have taken out all your variablenames and your strings, but without this, your code becomes nonsense. We can only help you with your structure if we know what we are looking at, so maybe you can rename your variables to varA varB varC and your strings to "stringA" "stringB"? That would make more sense than this.
A few thoughts, if the following is the case:
`If CableCB = True And Cable = "stringA" Then
ws.Cells(newRow, 3).Value = "stringA"
ElseIf CableCB = True And Cable = "stringB" Then
ws.Cells(newRow, 3).Value = "stringB"
ElseIf CableCB = True And Cable = "stringC" Then
ws.Cells(newRow, 3).Value = "stringC`
You could change the lot to:
If CableCB Then
ws.Cells(newRow, 3).Value = Cable
That would significantly shorten your procedure. But without knowing the variables and strings at play here, we can not really help you any further.
来源:https://stackoverflow.com/questions/32706131/vba-excel-procedure-too-large