Is there a way to count delimiters before a text-to-columns is performed on a paste event?

philwojo

Well-known Member
Joined
May 10, 2013
Messages
533
Hello, I have a sheet that data is being pasted in to and it should contain 57 fields, so 56 delimiters, in this case a comma. Is there a way to have VBA code to count the number of delimiters that are in the data on the paste event and display that number?

I need to check this because it is possible that the data could contain more or less that what should be there and since some data fields don't contain data you can't always tell if there is an extra or missing comma. Specifically the last 3 fields should all be blank, but the data needs the comma's there for the file to be read properly by another tool.

Thanks,
Phil
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This?

Code:
Dim s As String, lCounter As Long

s = "blu, blah, blu bla, bla bla bla, bli"
lCounter = Len(s) - Len(Replace(s, ",", ""))
MsgBox lCounter

M.
 
Upvote 0
Kind of, the data will be the 57 fields, but it could be multiple rows of, each a unique entry, maybe up to 100 rows. It would be unique each time, there is not set number for how many rows could be pasted in at one time.
 
Last edited:
Upvote 0
Here is a sample of the data I have, as you can see some fields are blank, some are populated in the chart, but it starts off like this raw data above the table below. I want to be able to count the number of comma's that show up in the raw data before it is broken out via text-to-columns via a paste by a user. I want to make sure that there are 56 commas in that raw data.

Once the text-to-columns is done, which could be as soon as you paste it in if the sheet had it done previously, there is no way to know if all 56 commas, 57 fields, are present in the raw data. I can have anywhere from 1 line of data in the table to several hundred.

Does this help, and if not I apologize, I'm trying to give as much information as I can.

1,,,,,Activate,AP001,,Single-Use Account,,Add,,,2017-10-06 23:59:59,2017-11-05 23:59:59,,,0.00,0.00,USD,,,,,326,,INV# RS/EXP/01/2017-18 / 2017-07-15|INV# RS/EXP/02/2017-18 / 2017-07-15,Add|Add,27256.25|5678.75,1|1,Each|Each,,,,,,,,,,Add,950,,"Redisolve",Add,,,,,,,Vendor ID|Payment Number,Add,5751|326,,,
2,,,,,Activate,AP001,,Single-Use Account,,Add,,,2017-10-06 23:59:59,2017-11-05 23:59:59,,,0.00,0.00,USD,,,,,327,,INV# IDTI-10061 / 2017-07-21,Add,22152.00,1,Each,,,,,,,,,,Add,950,,"IDT-Systems Limited",Add,,,,,,,Vendor ID|Payment Number,Add,16454|327,,,
3,,,,,Activate,AP001,,Single-Use Account,,Add,,,2017-10-06 23:59:59,2017-11-05 23:59:59,,,0.00,0.00,USD,,,,,328,,INV# 38026439 / 2017-07-14|INV# 38026442 / 2017-07-14,Add|Add,108.00|292.00,1|1,Each|Each,,,,,,,,,,Add,950,,"Imaging Solutions AG WIRE PMT",Add,,,,,,,Vendor ID|Payment Number,Add,3352|328,,,



[TABLE="class: grid, width: 7163, align: left"]
<tbody>[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Activate[/TD]
[TD]AP001[/TD]
[TD][/TD]
[TD]Single-Use Account[/TD]
[TD][/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD]2017-10-06 23:59:59[/TD]
[TD]2017-11-05 23:59:59[/TD]
[TD][/TD]
[TD][/TD]
[TD]0.00[/TD]
[TD]0.00[/TD]
[TD]USD[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]326[/TD]
[TD][/TD]
[TD]INV# RS/EXP/01/2017-18 / 2017-07-15|INV# RS/EXP/02/2017-18 / 2017-07-15[/TD]
[TD]Add|Add[/TD]
[TD]27256.25|5678.75[/TD]
[TD]1|1[/TD]
[TD]Each|Each[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Add[/TD]
[TD]950[/TD]
[TD][/TD]
[TD]Redisolve[/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Vendor ID|Payment Number[/TD]
[TD]Add[/TD]
[TD]5751|326[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Activate[/TD]
[TD]AP001[/TD]
[TD][/TD]
[TD]Single-Use Account[/TD]
[TD][/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD]2017-10-06 23:59:59[/TD]
[TD]2017-11-05 23:59:59[/TD]
[TD][/TD]
[TD][/TD]
[TD]0.00[/TD]
[TD]0.00[/TD]
[TD]USD[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]327[/TD]
[TD][/TD]
[TD]INV# IDTI-10061 / 2017-07-21[/TD]
[TD]Add[/TD]
[TD]22152.00[/TD]
[TD]1[/TD]
[TD]Each[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Add[/TD]
[TD]950[/TD]
[TD][/TD]
[TD]IDT-Systems Limited[/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Vendor ID|Payment Number[/TD]
[TD]Add[/TD]
[TD]16454|327[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Activate[/TD]
[TD]AP001[/TD]
[TD][/TD]
[TD]Single-Use Account[/TD]
[TD][/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD]2017-10-06 23:59:59[/TD]
[TD]2017-11-05 23:59:59[/TD]
[TD][/TD]
[TD][/TD]
[TD]0.00[/TD]
[TD]0.00[/TD]
[TD]USD[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]328[/TD]
[TD][/TD]
[TD]INV# 38026439 / 2017-07-14|INV# 38026442 / 2017-07-14[/TD]
[TD]Add|Add[/TD]
[TD]108.00|292.00[/TD]
[TD]1|1[/TD]
[TD]Each|Each[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Add[/TD]
[TD]950[/TD]
[TD][/TD]
[TD]Imaging Solutions AG WIRE PMT[/TD]
[TD]Add[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Vendor ID|Payment Number[/TD]
[TD]Add[/TD]
[TD]3352|328[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Still confuse - couldn't you simply count the number of columns after the Text-Column is done?

If not, i'm afraid i don't know how to help you. I do not have as much experience in dealing with text files (import), but i believe that it should be possible to loop and count the number of commas on each row.

Maybe someone else can help. Or try Google: Excel VBA import text files.

M.
 
Upvote 0
I can't count the columns because sometimes there is no data between comma's so it would be a blank field, and if there were extra blank fields after field #57 there is no way to see that once the text-to-columns has been executed.

I appreciate that assistance though, thank you for your time.

I am not importing the text file, just copying it from another source and then pasting it in to a single cell in excel.

I am considering maybe having the user paste the data in to a "input" type of sheet and then using macros to manipulate the data after that. I would then use code to make sure that the "input" or "raw data" sheet had code to always reset the text-to-columns back to default (of which I already have that code).

It just means more work to move the data to my other existing sheets now, but I might have to go that route.

Again, thanks for your assistance, and hopefully someone else might have some input.

Phil
 
Upvote 0
I made some searches in the internet

Maybe something like

Code:
Sub aTest()
    Dim myFile As String, textline As String
    Dim dic As Object, i As Long, vKey As Variant
    Set dic = CreateObject("Scripting.Dictionary")
    
    myFile = Application.GetOpenFilename()
    Open myFile For Input As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
    
    Do Until EOF(1)
        i = i + 1
        Line Input [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , textline
        dic(i) = Len(textline) - Len(Replace(textline, ",", ""))
    Loop
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
    
    For Each vKey In dic.keys
        MsgBox dic(vKey)
    Next vKey
End Sub

M.
 
Last edited:
Upvote 0
I appreciate you searching, I think I am looking at it slightly differently than you are.

I did the following so far.


Code:
Sub Test2()Dim copyRange As Range


Worksheets("PIF File Checker").Activate
Call Clearcells


Worksheets("Sheet1").Activate
   Set copyRange = Range("b2:b" & Range("A65536").End(xlUp).Row)
   With copyRange
       Sheets("PIF File Checker").Range("B7").Resize(.Rows.Count, .Columns.Count).Value = .Value
   End With
   
Worksheets("PIF File Checker").Activate
Call Text2ColSplit


End Sub

Code:
Sub Text2ColSplit()
    Selection.TextToColumns _
      Destination:=Range("b7:b6000"), _
      DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, _
      Comma:=True


End Sub

This almost does what I want it to do, but it only does the text-2-columns for the 1st line of data that is put on the "PIF File Checker" sheet. How do I make it loop through and do all lines of data that were moved over. I'm not very strong in VBA yet, I can usually tweak samples I find online, but don't know enough to code it myself yet.

Phil
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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