VBA Excel Procedure Too Large - procedure

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!!!

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.

Related

(ROBLOX) Dont add gui when other player join

I would explain better here, I'm doing a role game and I do ranks enter image description here
But if somebody another joins then the text over the head will Destroy and to another player spawn the GUI.
The script:
local ServerStorage = game:GetService("ServerStorage")
local Tag = ServerStorage.Tag
local CloneTag = Tag:Clone()
local NameTag = CloneTag.NameTag
local RankTag = CloneTag.RankTag
local VIPTag = CloneTag.VIPTag
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
CloneTag.Parent = char.Head
if player:IsInGroup(14472135) then
if player:GetRankInGroup(14472135) == 1 then
NameTag.Text = player.Name
RankTag.Text = "Trainee"
elseif player:GetRankInGroup(14472135) == 2 then
NameTag.Text = player.Name
RankTag.Text = "Police"
elseif player:GetRankInGroup(14472135) == 3 then
NameTag.Text = player.Name
RankTag.Text = "Specialist"
elseif player:GetRankInGroup(14472135) == 4 then
NameTag.Text = player.Name
RankTag.Text = "Sergeant"
elseif player:GetRankInGroup(14472135) == 252 then
NameTag.Text = player.Name
RankTag.Text = "Corporal"
elseif player:GetRankInGroup(14472135) == 253 then
NameTag.Text = player.Name
RankTag.Text = "Major"
elseif player:GetRankInGroup(14472135) == 254 then
NameTag.Text = player.Name
RankTag.Text = "Lieutenant"
VIPTag.Visible = true
elseif player:GetRankInGroup(14472135) == 255 then
NameTag.Text = player.Name
RankTag.Text = "Colonel"
VIPTag.Visible = true
end
else
NameTag.Text = player.Name
RankTag.Text = "Immigrant"
end
end)
end)
Any Suggestion?
Thanks
As I said in my comment, you simply move the variables so that the cloning is done when a character spawns. There are also ways to make this code better. (1) You can use a table to specify each rank's text, which will shorten it. You can use another table (or the same table with more detailed table values) to store the visibility of the VIP tag. (2) You also don't need to do NameTag.Text = player.Name in every if-elseif body, you can move it out of the if so that it will always happen.
I can show a quick example of fix #1:
local ranks = {
[1] = "Trainee",
[2] = "Police",
[3] = "Specialist",
--...
[255] = "Colonel"
}
local rankVIP = {
[254] = true,
[255] = true
}
and then, all you need to set someone's rank label's text is this:
local rankInGroup = player:GetRankInGroup(14472135)
RankTag.Text = ranks[rankInGroup]
VIPTag.Visible = rankVIP[rankInGroup] or false
Here, the or false means "if I couldn't find this (if this is nothing, if this is nil), make the visibility false. If I did find something, then it will be that."
With all of the fixes and improvements, here is the new code:
local ServerStorage = game:GetService("ServerStorage")
local Tag = ServerStorage.Tag
local ranks = {
[1] = "Trainee",
[2] = "Police",
[3] = "Specialist",
[4] = "Sergeant",
[252] = "Corporal",
[253] = "Major",
[254] = "Lieutenant",
[255] = "Colonel"
}
local rankVIP = {
[254] = true,
[255] = true
}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local CloneTag = Tag:Clone()
local NameTag = CloneTag.NameTag
local RankTag = CloneTag.RankTag
local VIPTag = CloneTag.VIPTag
NameTag.Text = player.Name
CloneTag.Parent = char.Head
if player:IsInGroup(14472135) then
local rankInGroup = player:GetRankInGroup(14472135)
RankTag.Text = ranks[rankInGroup]
VIPTag.Visible = rankVIP[rankInGroup] or false
else
RankTag.Text = "Immigrant"
end
end)
end)

Is there a way to Disconnect the Function from UIS.InputBegan:Connect(...)?

I tried everyway to somehwo make this work... It works on the first time you execute it, but if you try to execute it again it just fails. Here is the code:
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)
If you want to disconnect the event, then you can do something like this to disconnect the event.
local conn
function handle(parm1, parm2)
-- logic goes here
if somethingistrue then
conn:Disconnect()
end
end
conn = UIS.InputBegan:Connect(handle)
You want to look at globals. Globals in lua are variables that can be accessed from another thread.
You might be looking for this.
if getgenv().HasRanBefore then return end
if not getgenv().HasRanBefore then getgenv().HasRanBefore = true end
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)

Roblox Studio (Tried Gates) The Value doesn`t change

I tried a Gates Script but the script works fine but the 2nd Remote Event doesnt like to fire. It does nothing.
--This Works Fine
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HalloweenGatesTakeMoney")
local Price = script.Parent:WaitForChild("Price").Value
script.Parent.MouseButton1Click:Connect(function(player)
script.Parent.Parent.Visible = false
script.Parent.Parent.Parent.Shadow.Visible = false
local GateName = script.Parent:WaitForChild("Gate").Value
GateName:Destroy()
remoteEvent:FireServer(Price)
end)
--But this does nothing
remoteEvent2.OnServerEvent:Connect(function(player, Price)
player.EventMoney.Sweets.Value = player.EventMoney.Sweets.Value - Price
wait(1)
if player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "HauntedEntrance" then
player:WaitForChild("HalloweenGates").HauntedEntrance.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "PumpkinCave" then
player:WaitForChild("HalloweenGates").PumpkinCave.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "HauntedBiome" then
player:WaitForChild("HalloweenGates").HauntedBiome.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "GiantPumpkin" then
player:WaitForChild("HalloweenGates").GiantPumpkin.Value = true
end
end)
--Full script of above
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HalloweenGates")
local remoteEvent2 = ReplicatedStorage:WaitForChild("HalloweenGatesTakeMoney")
remoteEvent.OnServerEvent:Connect(function(player, Price, GateName, GateValue)
if player.EventMoney.Sweets.Value >= Price then
player.PlayerGui.HalloweenGates.Frame.Visible = true
player.PlayerGui.HalloweenGates.Shadow.Visible = true
player.PlayerGui.HalloweenGates.Frame.Price.Text = "This Gate costs "..Price.." Sweets"
player.PlayerGui.HalloweenGates.Frame.Buy.Gate.Value = GateName
player.PlayerGui.HalloweenGates.Frame.Buy.Price.Value = Price
player.PlayerGui.HalloweenGates.Frame.Buy.GateValueName.Value = GateValue
end
end)
remoteEvent2.OnServerEvent:Connect(function(player, Price)
player.EventMoney.Sweets.Value = player.EventMoney.Sweets.Value - Price
wait(1)
if player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "HauntedEntrance" then
player:WaitForChild("HalloweenGates").HauntedEntrance.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "PumpkinCave" then
player:WaitForChild("HalloweenGates").PumpkinCave.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "HauntedBiome" then
player:WaitForChild("HalloweenGates").HauntedBiome.Value = true
elseif player.PlayerGui:WaitForChild("HalloweenGates").Frame.Buy.GateValueName == "GiantPumpkin" then
player:WaitForChild("HalloweenGates").GiantPumpkin.Value = true
end
end)
--Here's where I fire the remoteEvent2 event
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HalloweenGatesTakeMoney")
local Price = script.Parent:WaitForChild("Price").Value
script.Parent.MouseButton1Click:Connect(function(player)
script.Parent.Parent.Visible = false
script.Parent.Parent.Parent.Shadow.Visible = false
local GateName = script.Parent:WaitForChild("Gate").Value
GateName:Destroy()
remoteEvent:FireServer(Price)
end)

Why does my debounce not work, yet produce no errors?

Why does my debounce not work, yet produce no errors?
Here's my script so far, but it isn't working.
local UIS = game:GetService("UserInputService")
local toggle = false
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
end
end)
If toggle is false you set it to true
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
And then immediately set it to false as you run into your second conditional statement.
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
So your change is revoked immediately befor anything had a chance to update.
If you have two exclusive states use if / else.
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
else
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
As all you're doing is to assign boolean values you can simply get rid of your conditions and do this:
toggle = not toggle -- toggle your state
-- assign the state to the visible property
script.Parent.BackpackFrame.Visible = toggle
script.Parent.BackpackText.Visibile = toggle
The code is setting toggle to false immediately after setting it to true.
if toggle == false then
-- false part
toggle = true
end
if toggle == true then
-- true part
toggle = false
end
Just use an else instead of checking toggle twice
if toggle then
-- true part
toggle = false
else
-- false part
toggle = true
end

Functions are having a problem where the 'end' part is not working

For some reason, I think the error messages are telling me to both take away a bracket or add one and I'm very confused. It is always either telling me that there is supposed to be something instead of the bracket or that a bracket is needed depending on how I change it.
Here's the code:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo>0 and not Reloading then
Ammo=Ammo-1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)```
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
Either add an end to that do or remove it.
To fix such errors count all keywords that require an end and all ends and see if you get the same number. There are text editors that help you with that
You should use an IDE like ZeroBraneStudio, or IntelliJ Community (with EmmyLua)
First issue:
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
You should remove do from else do
Second issue:
You'll be getting "unexpected )" from here:
print(Ammo)
end)
This is because you're missing an end from if Ammo > 0 and not Reloading then
The resultant code should look like this:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not Reloading then
Ammo = Ammo - 1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)

Resources