如何编辑这段VBA代码以在Excel中创建线条。

huangapple go评论72阅读模式
英文:

how to edit this vba code to create lines in excel

问题

我的上一篇帖子是关于编辑VBA代码以创建我用于电子支付的文件头。
今天的问题是修改代码以创建与文件头相关联的行。
首先,我将解释行的格式:
行长为62个字符:
1)每行开头有一个星号(),
2)帐号号码长度为20个字符(不超过20个字符),例如,如果Excel中的一个单元格包含帐号号码300034,另一个单元格包含与该帐号相关联的密钥70,则到目前为止,该行将是
00000000000030003470。
3)客户将收到的金额(不超过13个字符),例如,如果一个单元格包含30759.25,则到目前为止,该行将如下所示:
*000000000000300034700000003075925
如果金额为99999999999.99,则该行将如下所示:
*000000000000300034709999999999999
4)客户的姓名(不超过27个字符),如果姓名是joseph david,则表示包括“joseph”和“david”之间的空格在内共有12个字符,因此到目前为止创建的行将如下所示:
*000000000000300034709999999999999joseph david 1
在每27个字符后有15个空格,然后在行的末尾加上数字(1),以达到行的62个字符。
我现有的代码没有限制姓名、帐号和金额的大小。
代码如下:

Public Function Lines(Account, Key, Amount, Name As String)

'Account
C1 = Len(Account)
C1 = (18 - C1)
For i = 1 To C1
M = M & "0"
Next
C1 = M & Account

'Key
If Key = "" Then
   Key = "00"
ElseIf Key < 10 Then
   Key = "0" & Key
Else
   Key = Key
End If

'Amount
Amount = Amount * 100
C2 = Len(Amount)
C2 = (13 - C2)
For i = 1 To C2
M1 = M1 & "0"
Next
C2 = M1 & Amount

'Name & PreName
C3 = Name

'Line
C4 = "*" & C1 & Key & C2 & C3

'CD ZERO
C5 = Len(C4)
C5 = (61 - C5)
For i = 1 To C5
M3 = M3 & " "
Next
C5 = C4 & M3

Lines = C5 & "1"

End Function

提前感谢。

英文:

My last post was about editing a VBA code to create the header of a file that i’m using for electronic payments.
Today’s problem would be about modifying a code to create lines associated with the header.
So first, I’ll explain the format of the line :
The line is 62 characters long :

  1. A star at the begining (*) of each line,
  2. The account number which is 20 characters long (no more than that), for example if a cell in excel contains the account number, let’s say 300034 and another cell contains the key associated with the account, let’s say 70, the line to this stage woulld be *00000000000030003470.
  3. the amounts that a client will receive, (no more than 13 characters), for example if a cell contains 30759.25, than the line until now would look like this :
    *000000000000300034700000003075925
    If the amount is 99999999999.99 then the line would look like this :
    *000000000000300034709999999999999
  4. the name of the client (no more than 27 characters), if the name is joseph david, it means that there is 12 characters including the space between « joseph » and « david », so the line created thus far would look like this :
    *000000000000300034709999999999999joseph david 1
    With 15 spaces after david to each 27 characters, and then the number (1) at the end of the line to reach 62 characters of the line.
    The code that i have does not limit the size of names, accounts, amounts.
    The code is :

    Public Function Lines(Account, Key, Amount, Name As String) As String
    
    &#39;Account
    C1 = Len(Account)
    C1 = (18 - C1)
    For i = 1 To C1
    M = M &amp; &quot;0&quot;
    Next
    C1 = M &amp; Account
    
    &#39;Key
    If Key = &quot;&quot; Then
       Key = &quot;00&quot;
    ElseIf Key &lt; 10 Then
       Key = &quot;0&quot; &amp; Key
    Else
       Key = Key
    End If
    
    &#39;Amount
    Amount = Amount * 100
    C2 = Len(Amount)
    C2 = (13 - C2)
    For i = 1 To C2
    M1 = M1 &amp; &quot;0&quot;
    Next
    C2 = M1 &amp; Amount
    
    &#39;Nen &amp; Pr&#233;Name
    C3 = Name
    
    &#39;Annee
    C4 = &quot;*&quot; &amp; C1 &amp; Key &amp; C2 &amp; C3
    
    &#39;CD ZERO
    C5 = Len(C4)
    C5 = (61 - C5)
    For i = 1 To C5
    M3 = M3 &amp; &quot; &quot;
    Next
    C5 = C4 &amp; M3
    
    Lines = C5 &amp; &quot;1&quot;
    
    End Function
    

thanks in advance

答案1

得分: 3

请尝试以下代码:

Public Function Lines(Account As String, Key As String, Amount as Double, Name As String) As String
    Const accountLen = 20
    Const amountLen = 13
    Const nameLen = 27

    Lines = "*" _
          & Right(String(accountLen, "0") & Account & Key, accountLen) _
          & Format(Amount * 100, String(amountLen, "0")) _
          & Left(Right(Name, nameLen) & String(nameLen, " "), nameLen) _
          & "1"
End Function

String 函数用于创建包含一定数量字符的字符串,例如 String(20, "0") 创建一个字符串 "00000000000000000000"

对于 Account,我们将这20个零与账户和密钥连接起来,并取最右边的20个字符 - 这样就得到了带有前导零的账户和密钥。

对于 Amount,我假设我们有一个数值,我们可以使用 Format 函数创建带有前导零和2位小数的字符串。更新:根据 OP 的要求,我已更改此部分,不包含小数点或逗号。

对于 Name,我们取最右边的27个字符加上27个空格,并从结果的左边取27个字符 - 这样就得到了带有尾部空格的名称。

英文:

Try the following code:

Public Function Lines(Account As String, Key As String, Amount as Double, Name As String) As String
    Const accountLen = 20
    Const amountLen = 13
    Const nameLen = 27

    Lines = &quot;*&quot; _
          &amp; Right(String(accountLen, &quot;0&quot;) &amp; Account &amp; Key, accountLen) _
          &amp; Format(Amount * 100, String(amountLen, &quot;0&quot;)) _
          &amp; Left(Right(Name, nameLen) &amp; String(nameLen, &quot; &quot;), nameLen) _
          &amp; &quot;1&quot;
End Function

The String function creates a String that contains a number of characters, eg String(20, &quot;0&quot;) creates a string "00000000000000000000".

For the Account, we concatenate this 20 zeros with the account and the key and take the rightmost 20 characters - that gives the account & key with leading zeros.

For the Amount, I assume we have a numeric value, and we can use the Format function to create leading zeros and 2 decimals. Update: Changed this as OP don't want decimal comma (or point).

For the Name, we take the rightmost 27 characters plus 27 spaces and take 27 characters from the left of the result - that gives the name with trailing blanks.

huangapple
  • 本文由 发表于 2023年8月8日 21:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定