Select Range VBA With CountA

excelbytes

Active Member
Joined
Dec 11, 2014
Messages
291
Office Version
  1. 365
Platform
  1. Windows
I have the following code:

VBA Code:
Private Sub CheckBox2_Click()
    
   
On Error Resume Next
On Error GoTo err_handler
  
If CheckBox2.Value = True Then Sheets("IC304").Range("B2:B50").Value = "MP0"
If CheckBox2.Value = False Then Sheets("IC304").Range("B2:B50").Value = ""
If CheckBox2.Value = True Then Sheets("TH102").Range("B2:B50").Value = "MP0"
If CheckBox2.Value = False Then Sheets("TH102").Range("B2:B50").Value = ""
  
Exit Sub

err_handler:
MsgBox "Error number: " & Err.Description, vbExclamation, Err.Number
Exit Sub
End Sub

However, I don't want to insert MP0 in the range B2:B50, rather start at B2 and only enter it based on the count from another worksheet:

VBA Code:
Sub CountCells()

Dim ws As Worksheet
Set ws = Worksheets("OFFICIAL DRAFT")
Application.WorksheetFunction.CountA(ws.Range("D15:D100"))

End Sub

How can I combine these so that it will only enter MP0 starting in cell B2 and going down as far as the count in the OFFICIAL DRAFT worksheet D15:D100?

Thanks!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi
untested but see if update to your code will do what you want

VBA Code:
Private Sub CheckBox2_Click()
    Dim ws(1 To 3)  As Worksheet
    Dim CountCells  As Long, i  As Long
    Dim Checked     As Boolean
    Dim rng         As Range
    
    With ThisWorkbook
        For i = 1 To 3
           Set ws(i) = .Worksheets(Choose(i, "IC304", "TH102", "OFFICIAL DRAFT"))
        Next i
    End With
    
    Set rng = ws(3).Range("D15:D100")
    
    CountCells = Application.CountA(rng)
    Checked = CheckBox2.Value And CountCells > 0
    If Not Checked Then CountCells = rng.Cells.Count
    
    For i = 1 To 2
        ws(i).Cells(2, 2).Resize(CountCells).Value = IIf(Checked, "MP0", "")
    Next i
    
End Sub

Dave
 
Upvote 0
Hi
untested but see if update to your code will do what you want

VBA Code:
Private Sub CheckBox2_Click()
    Dim ws(1 To 3)  As Worksheet
    Dim CountCells  As Long, i  As Long
    Dim Checked     As Boolean
    Dim rng         As Range
   
    With ThisWorkbook
        For i = 1 To 3
           Set ws(i) = .Worksheets(Choose(i, "IC304", "TH102", "OFFICIAL DRAFT"))
        Next i
    End With
   
    Set rng = ws(3).Range("D15:D100")
   
    CountCells = Application.CountA(rng)
    Checked = CheckBox2.Value And CountCells > 0
    If Not Checked Then CountCells = rng.Cells.Count
   
    For i = 1 To 2
        ws(i).Cells(2, 2).Resize(CountCells).Value = IIf(Checked, "MP0", "")
    Next i
   
End Sub

Dave
Dave,

It works great! I had to make a slight modification because of a range size I changed, but it's fine.

I have one other similar process on the same workbook and tried to use this as a template to take care of that one but it didn't work. Here is what I had:

VBA Code:
Private Sub CheckBox3_Click()
    Dim ws(1 To 2)  As Worksheet
    Dim CountCells  As Long, i  As Long
    Dim Checked     As Boolean
    Dim rng         As Range
    
    With ThisWorkbook
        For i = 1 To 2
           Set ws(i) = .Worksheets(Choose(i, "IC304", "OFFICIAL DRAFT"))
        Next i
    End With
    
    Set rng = ws(2).Range("D15:D100")
    
    CountCells = Application.CountA(rng)
    Checked = CheckBox2.Value And CountCells > 0
    If Not Checked Then CountCells = rng.Cells.Count
    
    For i = 1 To 2
        ws(i).Cells(2, 5).Resize(CountCells).Value = IIf(Checked, "100", "")
    Next i
    

    
End Sub

The value 100 needs to go into column E starting at cell E2. What am I doing wrong?
 
Upvote 0
The value 100 needs to go into column E starting at cell E2. What am I doing wrong?

Looks like you have not updated the CheckBox code needs to refer to which may be your issue?

Rich (BB code):
Checked = CheckBox3.Value And CountCells > 0

Dave
 
Upvote 0

Forum statistics

Threads
1,223,103
Messages
6,170,123
Members
452,303
Latest member
c4cstore

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