optimizing vba code or suggestion of solution

tonypiha

New Member
Joined
Mar 6, 2010
Messages
10
Hi all,
I play poker and I use an excel with specific ranges of cards. The cards you will call a bet depends on the position of the raiser and the position you are calling. I have matrix with the cards with named ranges
The position´s name are:
EP
MP
CO
BTN
SB
BB

So if the EP open raise with a bet and I am sitting at MP, I have a range called Call_MP_vs_EP
So I did 2 columns of buttons that assign the respective string to a cell ( here, P40 and Q40)and the I have a button that run a macro to call the specific range
The Macro is below. I am wondering if there is a smarter way to do it
And it is not possible to have the same positions, for instance EP vs EP


Private Sub MP_Click()


' range call of MP vs EP
Dim Raise As Variant
Dim vscall As Variant
Dim atencao As String


'vscall = define caller position


'Raise = define raiser position


vscall = Sheets("Dashboard").Range("p40").Value
Raise = Sheets("Dashboard").Range("q40").Value

Application.ScreenUpdating = False


'delete previous range
Sheets("Dashboard").Range("B3:N20").Delete

'Not possible to have 2 positions at same time, then go to error msg


On Error GoTo atencao
'Select, copy and past the defined range based on Q40 and P40 cell value


Application.Range("Call_" & vscall & "_vs_" & Raise).Copy
Sheets("Dashboard").Select
Sheets("Dashboard").Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False

On Error GoTo atencao
Range("A1").Select
Application.ScreenUpdating = True

atencao: MsgBox "ranges inválidos"


End Sub

4d224f6


4d224f6
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the board.

Is this what you mean? Try:
Code:
Private Sub MP_Click()
    
    Dim strRange    As String
    
    strRange = "Call_@P40_vs_@Q40"
    
    With Sheets("Dashboard")
        .Select
        
        If .Cells(40, 16).Value = .Cells(40, 17).Value Then
            MsgBox "ranges inválidos", vbExclamation, "Ranges Inválidos"
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        .Cells(3, 2).Resize(18, 13).ClearContents
        
        strRange = Replace(strRange, "@P40", .Cells(40, 16).Value)
        strRange = Replace(strRange, "@Q40", .Cells(40, 17).Value)
        
        On Error Resume Next
        Range(strRange).Copy
        .Cells(4, 2).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
        Application.CutCopyMode = False
        On Error GoTo 0
                    
    End With
    
    With Application
        .Goto Cells(1, 1), True
        .ScreenUpdating = True
    End With
    
End Sub
 
Last edited:
Upvote 0
Thanks , I really appreciate

But, it worked only once and just the formatting was pasted, the values of the cells don´t
 
Upvote 0
This should post the values:
Rich (BB code):
Private Sub MP_Click()
    
    Dim strRange    As String
    
    strRange = "Call_@P40_vs_@Q40"
    
    With Sheets("Dashboard")
        .Select
        
        If .Cells(40, 16).Value = .Cells(40, 17).Value Then
            MsgBox "ranges inválidos", vbExclamation, "Ranges Inválidos"
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        .Cells(3, 2).Resize(18, 13).ClearContents
        
        strRange = Replace(strRange, "@P40", .Cells(40, 16).Value)
        strRange = Replace(strRange, "@Q40", .Cells(40, 17).Value)
        
        On Error Resume Next
        Range(strRange).Copy
        .Cells(4, 2).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
        .Cells(4, 2).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        On Error GoTo 0
                    
    End With
    
    With Application
        .Goto Cells(1, 1), True
        .ScreenUpdating = True
    End With
    
End Sub
When you say only ran once, did you change the values in P40 and Q40?

Can you list all the possible combinations there are of P40 and Q40?

Do you have named ranges for all of these combinations, which the code should copy and paste into B4?
 
Last edited:
Upvote 0
Thank you very much
the range name is correctly defined( I checked passing the mouse over strRange variable), but nothing is being pasted in target cell
When I disabled the line, on error resume next it returned an error
 
Upvote 0
What did the error say? I'm guessing the names are not matching.

Can you post the exact range names please to see if the code is generating the same names of range to copy - since this is what is not being pasted.
 
Upvote 0
one range is named:

Call_MP_vs_CO

the error:
error on the application definition or object definition
 
Last edited:
Upvote 0
I added the line Range(strRange).select to test and the right range is selected, so I assume the names are correct...

 
Upvote 0
Try:
Code:
Private Sub MP_Click()
    
    Dim strRange    As String
    
    strRange = "Call_@P40_vs_@Q40"
    
    With Sheets("Dashboard")
        .Select
        
        If .Cells(40, 16).Value = .Cells(40, 17).Value Then
            MsgBox "ranges inválidos", vbExclamation, "Ranges Inválidos"
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        .Cells(3, 2).Resize(18, 13).ClearContents
        
        strRange = Replace(strRange, "@P40", .Cells(40, 16).Value)
        strRange = Replace(strRange, "@Q40", .Cells(40, 17).Value)
                
        .Range(strRange).Copy
        .Cells(4, 2).PasteSpecial Paste:=xlPasteFormats
        .Cells(4, 2).PasteSpecial Paste:=xlPasteValues
                    
    End With
    
    With Application
        .CutCopyMode = False
        .Goto Cells(1, 1), True
        .ScreenUpdating = True
    End With
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top