Table.Rows ConvertToText Issue

chickyguy

New Member
Joined
Mar 27, 2024
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

Scratching my head over this issue.

I have a module that's like this:

VBA Code:
For Each Table in Doc.Tables
    For Each Row in Table.Rows
        ConvertedRow = Row.ConvertToText
        ' Do Stuff
    Next Row
Next Table

That fine and is working properly... but when I need to adjust it to the following:

Code:
For Each Table in Doc.Tables
    For Cnt =1 to Table.Rows.Count
        ConvertedRow = Table.Rows(Cnt).ConvertToText
        ' Do Stuff
    Next Cnt
Next Table

Line 3 is throwing me Run-time error '5941': The requested member of the colleccton does not exist.

Aren't they equal to each other? Why does the above work but not the other?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
When you convert the first row to text, it is no longer part of the table, which means that the next row is now row 1, not row 2, so you need to loop backwards (much like deleting):

Code:
For Cnt =Table.Rows.Count To 1 step -1

should fix it.
 
Upvote 0
When you convert the first row to text, it is no longer part of the table, which means that the next row is now row 1, not row 2, so you need to loop backwards (much like deleting):

Code:
For Cnt =Table.Rows.Count To 1 step -1

should fix it.
Ah... that explains it.

However, as I'm extracting info based on keywords like this:

TitleA TitleB
Data Data

It's difficult to do it backwards as I'm taking out the next row of data if a Title is matched, is there any other recommendations to solve this problem?
 
Upvote 0
Why do you need to adjust it from the version that was working?
 
Upvote 0
Why do you need to adjust it from the version that was working?

With the current method, I would do something like this:

VBA Code:
Dim IsTitleAFound

...
If IsTitleAFound = True Then
    'Extract data to sheet
ElseIf InStr(ConvertedRowText, "TitleA") Then
    IsTitleAFound = True
EndIf

It worked fine as its only TitleA, but now theres TitleB, which needs different steps from TitleA, and so I'm doing booleans for each one to check.

As is, the booleans do work, but I'm not sure if it's maintainable so if there's better alternatives I'm open to suggestions.
 
Upvote 0
That doesn't really give enough context to suggest the best option, I'm afraid. Why do you need separate booleans? Why do you need to convert the rows?
 
Upvote 0
That doesn't really give enough context to suggest the best option, I'm afraid. Why do you need separate booleans? Why do you need to convert the rows?
The booleans are to extract the data given:
DateOtherFieldAOtherFieldB
03/05/2024xxxyyy

If I need the date value from a table, I go through the rows and if the row contain the word "Date", I know the next field is the value I'm looking for.
I convert it to text so I can split it by "-" before either processing the data or just populate the sheet as is.

I'll add onto the code for more context:

VBA Code:
Dim IsTitleAFound, IsPurchaseDetailsFound As Boolean

...
If IsDateFound = True Then
    'Extract data to sheet
    ' call formatting of date cell function
ElseIf IsPurchaseDetailsFound Then
    ' SplitText = Split(ConvertedRowText, "-")
    Amount = Amount + SplitText(0)
    Sheet.Cells(Row, 1) = SplitText(1)
ElseIf InStr(ConvertedRowText, "Date Processed") Then
    IsDateFound = True
ElseIf InStr(ConvertedRowText, "Purchased Details") Then
    IsPurchaseDetailsFound = True
EndIf

I'm using the boolean method to say to use the next row.

Normally in JS, python or Java I can do:
JavaScript:
let i = 0;

while (i < row.count) {
    if (row[i].includes("Date")) {
        // Extract date from row[++i];
    }
    else if (row[i].includes("Purchased Details")) {
        // Process purchase details from row[++i]
    }
    i++;
}

So in a way I'm trying to structure my code to be like the latter, to make it cleaner.
 
Upvote 0
Can't you just loop row by row and column by column looking for the data? Then when you find it, you know which row/column you are in and can adjust accordingly, without needing to alter the table.
 
Upvote 0
Can't you just loop row by row and column by column looking for the data? Then when you find it, you know which row/column you are in and can adjust accordingly, without needing to alter the table.

Oh hmm, do you mean something like this:

VBA Code:
For Each Table in Doc.Tables
    For Cnt =1 to Table.Rows.Count
        Row = Table.Rows(Cnt)
        For ColCnt = 1 to Row.Count
            Cell = Row(ColCnt)
            If InStr(Cell, "Date") Then
                 ProcessDate Table.Rows(Cnt+1).(ColCnt)
            ElseIf InStr(Cell, "Purchase Details) Then
                 Amount = Amount + Table.Rows(Cnt+1).(ColCnt)
                 Sheet.Cells(Row, 1) = Table.Rows(Cnt+1).(ColCnt+1)
            End If
        Next ColCnt
   Next Cnt
Next Table

Am I right in my understanding?
 
Upvote 0
Ah I forgot mention I altered the table as I'm reading a pdf file through word, then populating the excel sheet with the necessary information. So I don't really save the OG table since I just wipe the word app after that.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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