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
1,927
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,826
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,927
Office Version
  1. 365
Platform
  1. Windows
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,826
Office Version
  1. 365
Platform
  1. Windows
As that is a totally different question, it needs a new thread. Thanks
 

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,927
Office Version
  1. 365
Platform
  1. Windows
Thanks and understand
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,826
Office Version
  1. 365
Platform
  1. Windows
You're welcome.
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,589
Messages
5,765,317
Members
425,272
Latest member
Umba

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
Top