• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help, How to set Right and Left function for different columns??

Ftwtreefiddy

New Member
>>> use code - tags <<<
Code:
Sub ExportFixedLength1()
    Dim A As String * 30, B As String * 25, C As String * 20, D As String * 15, E As String * 10
    Dim FF%, Rg As Range
        FF = FreeFile
        Open ThisWorkbook.Path & "\ExportFL 1 .txt" For Output As #FF
    For Each Rg In ActiveSheet.UsedRange.Rows
         A = Rg.Cells(1).Text
         B = Rg.Cells(2).Text
         C = Rg.Cells(3).Text
         D = Rg.Cells(4).Text
         E = Rg.Cells(5).Text
         Print #FF, A; B; C; D; E
    Next
         Close #FF
End Sub
Using this to print a text file with fixed spacing but I need to make it so column C(3) will extract the text from the right not the default left?? Anytime I've tried to add the Right function this is an exapmle :
Code:
Dim Myvalue As String
Dim Myvalue
Myv = Right(C, 20)
Or anything similar I get an error?
 
Last edited by a moderator:
Code:
Sub ExportFixedLength2n()

        Dim V, L%, P%(), C%, F%, Rg As Range, S

            V = [{30,25,20,15,10,5}]

            L = 1

            ReDim P(1 To UBound(V))

            For C = 1 To UBound(V):  P(C) = L:  L = L + V(C):  Next

            Application.UseSystemSeparators = False

            F = FreeFile

            Open ThisWorkbook.Path & "\ExportFL 2n .txt" For Output As #F

    For Each Rg In ActiveSheet.UsedRange.Rows

        For C = 1 To UBound(V)

            S = Right(Rg.Cells(C).Text, V(C))

            Print #F, Tab(P(C) - (V(C) - Len(S)) * IsNumeric(Rg.Cells(C).Value)); S;

        Next

            Print #F, Tab(L)

    Next

            Close #F

            Application.UseSystemSeparators = True

End Sub



Above works just as well and I can set all text to pull from the Right but can't have some from the left and some from the right? Any help?
 

Attachments

  • ProjectA.txt
    1.2 KB · Views: 3
Last edited:
Unclear …​
So read again my thread post #8 in particular :​
As a reminder any smart import feature does not need numbers to be right formatted …​
For further help attach at least a sample source workbook and accordingly the expected export text file​
without forgetting to indicate the maximum length of each exported column.​
 
I'm using the code you put together, Thanks by the way! It works great however, I need to set it up, so column E pulls from the right when printing. As all columns already pull from the left. I need this because I have formatted E and H to have four "0"'s to the left and right respectively(could be 30 or 100 zeros) these zeros represent the decimal place where E is a whole number and H has four zeros after the decimal. If the input values change from 1, 2 or 3 digits, I want the zeros to drop off as the text file is produced same as it does currently for column H. That way all the spacing is held as the spacing denotes where this data will go in our database when we import the text file.

Hope this makes sense see the difference between column E and H on row 5 where I change the input value from 1 to 111 on E and 20 to 222 on H. The text produced is correct for H5 but not for E5. When output to text E1-E4 is correct 00001, E5 is not (it matches E1-E4) it should be 00111. E5 dropped two of the values and kept two of the placeholder zeros. Check H5 it matches H1-H4 as it should, due to the fact it drops all zeros from the right because it pulls the first 6 spaces from the left and drops everything else. I would like to be able to set different rows as necessary to either pull from the left or right and drop the placeholders thus keeping the set lengths of each column.


If ExportFixedLength1(What I’m using) is too rudimentary for that type of functionality. I am able to work with ExportFixedLength2n as well and have already been able to set it from right to left but only for the entire sheet not by comlumn. So if you need to use code more like ExportFixedLength2n I can work with it. If it's not obvious, it's my first time working with code as I'm an electircal engineer but desperatly need this for a strange project I found myself on...
 

Attachments

  • Working macro for fileE to A.xlsm
    18.1 KB · Views: 1
  • File E - Inbound Receipt - Respond to File A .txt
    6.5 KB · Views: 1
  • Starting data for E to A.txt.xlsx
    10.5 KB · Views: 1
Last edited:
FromToFieldLenDecTypText
Required​
Example​
120R20APOT ID
521-01720479-MWW​
2140RCT20ANUM
4160RC20ABIL
6180RCP20APA
8185RCP50SPOT
00005​
86105RC20AIT
477794A​
106155RCP50AEM
24-100P SOL BC PE-89 TYPE REA FOAMFILLED,BLK LLDPE​
156168RCR64SREC
660000​
169180RC12ASTOR
1​
181188RCR8ASTO
189200RCRD12AST
201208R80SPROC
209214RCP60SPRO
File B to A
Above is the output requirments I'm working with for one data set... Everything pulled from right example attached using Exportfixedlength2n, now colummn H is wrong but E is correct.
 

Attachments

  • Working macro for FileE to A Right.xlsm
    18.1 KB · Views: 5
  • File E – RgtInbound Receipt – Response to File A .txt
    1.1 KB · Views: 7
Last edited:
According to post #5 check this demonstration :​
Code:
Sub Demo2n()
        Dim V, L%, T$, P%(), C%, F%, Rg As Range, S
            V = [{20,20,20,20,5,20,50,13,12,8,12,8,6}]
            L = 1
            T = String(V(5), "0")
            ReDim P(1 To UBound(V))
            For C = 1 To UBound(V):  P(C) = L:  L = L + V(C):  Next
            Application.UseSystemSeparators = False
            F = FreeFile
            Open ThisWorkbook.Path & "\File E – RgtInbound Receipt – Response to File A .txt" For Output As #F
    For Each Rg In ActiveSheet.UsedRange.Rows
            S = Rg.Value2
            S(1, 5) = Format$(S(1, 5), T)
            S(1, 8) = S(1, 8) & "0000"
        For C = 1 To UBound(V)
            S(1, C) = Left(S(1, C), V(C))
            Print #F, Tab(P(C) - (V(C) - Len(S(1, C))) * IsNumeric(S(1, C))); S(1, C);
        Next
            Print #F, Tab(L)
    Next
            Close #F
            Application.UseSystemSeparators = True
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
While adapting this for my other data sheet I get the (error: subscript out of range) as soon as I add a 20th column to V??

Can't adapt it fully for the below data...

75744
 

Attachments

  • FilcSampleFileC.txt
    2.2 KB · Views: 2
  • Working macro for fileC to B.xlsm
    19.1 KB · Views: 3
Last edited by a moderator:
'Cause obviously only 19 columns are used so at least some data must be in the 20th column​
or just do not forget to loop on the columns used rather than on the upper bound of the array variable V​
or just adapt the souce range on 20 columns rather than using the property UsedRange !​
 
Back
Top