VBA to check to see that a table is only two columns wide and only has one row (plus the header)

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,340
Office Version
  1. 365
Platform
  1. Windows
I have a table that I want to make sure the user did not add any columns to and that they only entered one row of data before I run the rest of my code. I need a message box that states if there are added columns or more then one row of data. And then exit the code.

The table is on a sheet named sheet("Step 1")
The table name is "Table1"

Code:
Option Explicit
Sub PopoulateProducts()

Dim lr As Long

Sheets("Products").Select

'Clear existing data if any
lr = Sheets("Products").Cells(Rows.Count, "A").End(xlUp).Row
If lr > 2 Then Sheets("Parts").Range("A3:D" & lr).ClearContents

'Move data from Step 1 to Products tab
Products.Range("A3").Value = Sheets("Step 1").Range("C7")
Products.Range("B3").Value = Sheets("Step 1").Range("D7")
Products.Range("C3").Value = Sheets("Step 1").Range("C7").Value & "  " & Sheets("Step 1").Range("D7")

 'End If
 
Sheets("Step 2").Activate
 
End Sub

Thank You!!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
How about
VBA Code:
   With Sheets("Step 1").Listobjects("Table1").DataBodyRange
      If .Columns.Count > 2 Or .Rows.Count > 1 Then
         MsgBox "Table is too large"
         Exit Sub
      End If
   End With
 
Upvote 0
Solution
Thanks Fluff. Is a way to delete rows if there is more than one and delete the extra columns if there is more than two?
 
Upvote 0
As that is a totally different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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