VBA Excel Procedure Too Large

谁都会走 提交于 2019-12-11 06:03:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!