Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Tom F.
 
Posts: n/a
Default Validating data pasted into worksheet

I have an application that has a user paste data into a set group of columns
and up to 100 rows. The data is sales information. They then click a button
and the data is exported into a flat file. I need the data to be validated
before the flat file is created. 1. I need field length validation on some
columns (13 characters max) and 2. I need the data in two columns to be
validated against a code list (MN, KN, TD, or GD). I am thinking that doing
this as a Macro is best because selecting the "Data" "Validation" for a
range is erased once new data is pasted into a cell.
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

This seems like a very reasonable approach.

Tom F. wrote:

I have an application that has a user paste data into a set group of columns
and up to 100 rows. The data is sales information. They then click a button
and the data is exported into a flat file. I need the data to be validated
before the flat file is created. 1. I need field length validation on some
columns (13 characters max) and 2. I need the data in two columns to be
validated against a code list (MN, KN, TD, or GD). I am thinking that doing
this as a Macro is best because selecting the "Data" "Validation" for a
range is erased once new data is pasted into a cell.


--

Dave Peterson
  #3   Report Post  
Tom F.
 
Posts: n/a
Default

Do you have any sample code?

"Dave Peterson" wrote:

This seems like a very reasonable approach.

Tom F. wrote:

I have an application that has a user paste data into a set group of columns
and up to 100 rows. The data is sales information. They then click a button
and the data is exported into a flat file. I need the data to be validated
before the flat file is created. 1. I need field length validation on some
columns (13 characters max) and 2. I need the data in two columns to be
validated against a code list (MN, KN, TD, or GD). I am thinking that doing
this as a Macro is best because selecting the "Data" "Validation" for a
range is erased once new data is pasted into a cell.


--

Dave Peterson

  #4   Report Post  
Dave Peterson
 
Posts: n/a
Default

How about something like this:

Option Explicit
Sub testme()

Dim myRng As Range
Dim okToContinue As Boolean

With ActiveSheet
Set myRng = .Range("a2:g" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

okToContinue = ValidateMyData(myRng)

If okToContinue = False Then
MsgBox "You had at least one error!"
Exit Sub
End If

'do your export here.

End Sub

Function ValidateMyData(myRng As Range) As Boolean

Dim myCol As Range

ValidateMyData = True
For Each myCol In myRng.Columns
Select Case myCol.Column
Case Is = 1
If DoLength(myCol, 13) = False Then
ValidateMyData = False
End If
Case Is = 2
If DoLength(myCol, 4) = False Then
ValidateMyData = False
End If
Case Is = 5
If DoList(myCol, Array("md", "xy", "zz")) = False Then
ValidateMyData = False
End If
End Select
Next myCol

End Function
Function DoLength(myRng As Range, MaxLen As Long) As Boolean
DoLength = CBool(Application.Evaluate("Max(len(" & _
myRng.Address(external:=True) & "))") <= MaxLen)
If DoLength = False Then
MsgBox "Something too wide with: " & myRng.Address(0, 0) & vbLf _
& "no more than: " & MaxLen & " characters!"
End If
End Function
Function DoList(myRng As Range, myList As Variant) As Boolean
Dim myCell As Range
Dim res As Variant
DoList = True
For Each myCell In myRng.Cells
res = Application.Match(myCell.Value, myList, 0)
If IsError(res) Then
MsgBox "Invalid entry in: " & myRng.Address(0, 0)
DoList = False
Exit For
End If
Next myCell

End Function

Tom F. wrote:

Do you have any sample code?

"Dave Peterson" wrote:

This seems like a very reasonable approach.

Tom F. wrote:

I have an application that has a user paste data into a set group of columns
and up to 100 rows. The data is sales information. They then click a button
and the data is exported into a flat file. I need the data to be validated
before the flat file is created. 1. I need field length validation on some
columns (13 characters max) and 2. I need the data in two columns to be
validated against a code list (MN, KN, TD, or GD). I am thinking that doing
this as a Macro is best because selecting the "Data" "Validation" for a
range is erased once new data is pasted into a cell.


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to search for and display data in another worksheet Mark H Excel Worksheet Functions 0 June 14th 05 12:40 PM
How to..use worksheet form and save data to an Excel list Jan Excel Discussion (Misc queries) 2 June 10th 05 04:45 AM
How to move data on worksheet Bonnie Excel Discussion (Misc queries) 3 February 23rd 05 05:49 PM
How can I show data under a Chart that I inserted in a worksheet? Kimo Charts and Charting in Excel 1 February 21st 05 04:11 PM
URGENT Please... new worksheet with copied formats but no data. DarrellK Excel Worksheet Functions 2 December 1st 04 07:11 PM


All times are GMT +1. The time now is 12:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"