返回列表 上一主題 發帖

[發問] 選擇檔案轉成文字檔問題.

回復 1# dou10801


    以下是練習的方案,請前輩參考

Option Explicit
Sub 按鈕1_Click()
Application.DisplayAlerts = False
Dim MyFile, Arr, Brr$(), p2%, myPath$, R&, Q, MDS$(2 To 4), Nm$, Sc%, mcs1$, mcs2$
MyFile = Application.GetOpenFilename("Excel檔,*.XLS*")
If MyFile = "False" Then Exit Sub
Q = Split([B6], ",")
myPath = ThisWorkbook.Path & "\"
mcs1 = [B1] & Mid([B2], 2, 6) & [B3] & [B4]
mcs2 = [B5]: Sc = Val([B7])
With Workbooks.Open(MyFile, , True)
   Nm = .Name
   If Sc > .Worksheets.Count Then MsgBox Nm & " 活頁簿沒有第" & Sc & " 個表": .Close 0: Exit Sub
   With .Sheets(Sc)
      If Not .AutoFilter Is Nothing Then If .FilterMode = True Then .ShowAllData
      Arr = Range(.[G1], .[A65536].End(3))
   End With
   .Close 0
End With
If UBound(Arr) = 1 Then MsgBox Nm & " 活頁簿第" & Sc & " 個表沒有資料": Exit Sub
ReDim Brr(1 To UBound(Arr) - 1, 1 To 1)
For R = 2 To UBound(Arr)
   If Arr(R, 3) <> "" Then
      p2 = 29 - Len(Arr(R, Val(Q(2))))
      MDS(2) = Arr(R, Val(Q(0)))
      MDS(3) = Format(Arr(R, Val(Q(1))), "00000000000")
      MDS(4) = Arr(R, Val(Q(2)))
      Brr(R - 1, 1) = mcs1 & MDS(2) & MDS(3) & "00" & mcs2 & MDS(4) & String(p2, " ")
   End If
Next R
Workbooks.Add
[A1].Resize(UBound(Arr) - 1, 1) = Brr
Nm = StrReverse(Mid(StrReverse(Nm), InStr(StrReverse(Nm), ".") + 1))
ActiveWorkbook.SaveAs myPath & Nm & ".TXT", 42
ActiveWindow.Close
ThisWorkbook.Activate
MsgBox "產生媒體檔:" & myPath & Nm & ".TXT"
End Sub
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 15# dou10801


   
      Arr = .[A1].CurrentRegion
      'brr = .[A1].CurrentRegion
      ReDim brr(1 To UBound(Arr), 1 To 1)
      'MsgBox UBound(Arr)
      For R = 1 To UBound(Arr)
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 14# Andy2483
以選22.XLSX,第3工作表為例,末筆資料,[合計]字樣為何拿不掉.請前輩指點,謝謝.
If Cells(R, 3) <> "" Then    '已判斷C欄空白不加入BRR(),但產生檔還是有[合計]字樣.
            p1 = Len(Cells(R, MDS4))  '備註欄長度.
            p2 = 29 - p1              '補空白長度
            mcs = "" & mcs1 & Cells(R, MDS2) & Format(Cells(R, MDS3), "00000000000") & "00" & mcs2 & Cells(R, MDS4) & String(p2, " ")
            brr(R, 1) = mcs
          End If
杜小平

TOP

回復 13# dou10801


    將每個變數都做宣告 可以讓邏輯更清楚,建議養成習慣
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 12# Andy2483
方法1.:
With Workbooks.Open(xlsPath, , True).Sheets(Val(MP1))
"3"是字串,指定3索引號工作表必須將字串 "3" 轉化為數值 3

方法2.:
宣告為短整數:
Dim MP1%
MP1 = Range("B7")
With Workbooks.Open(xlsPath, , True).Sheets(MP1)

mcs = "'" & mcs1 & Cells(R, MDS2) & Format(Cells(R, MDS3),~~

With Workbooks.Open(xlsPath, , True).Sheets(2)
    .Activate
  .Cells.NumberFormatLocal = "@"

此次問答,感謝前輩,不吝賜教指導,學到很多小細節,感恩感恩.
杜小平

TOP

回復 11# dou10801


   
With Workbooks.Open(xlsPath, , True).Sheets(2)
       .Activate
      .Cells.NumberFormatLocal = "@"
      Arr = .[A1].CurrentRegion
      brr = .[A1].CurrentRegion

用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

本帖最後由 dou10801 於 2023-10-30 13:51 編輯

回復 10# Andy2483
With Workbooks.Open(xlsPath, , True).Sheets(2)
1.選取22.XLSX為例.
2,會產生[工作表一]的資料,且不是字串組合.
mcs = "" & mcs1 & Cells(R, MDS2) & Format(Cells(R, MDS3), "00000000000") & "00" & mcs2 & Cells(R, MDS4) & String(p2, " ")

22txt.jpg (41.44 KB)

22txt.jpg

工作表1.jpg (167.11 KB)

工作表1.jpg

工作表2.jpg (153.34 KB)

工作表2.jpg

杜小平

TOP

回復 9# dou10801


    With Workbooks.Open(xlsPath, , True).Sheets(2) 執行結果 怎樣不行?
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 8# Andy2483
With Workbooks.Open(xlsPath, , True).Sheets(1)
      .Cells.NumberFormatLocal = "@"
取第一工作表正常了.


With Workbooks.Open(xlsPath, , True).Sheets(2)    或   With Workbooks.Open(xlsPath, , True).Sheets(val(mp1)
      .Cells.NumberFormatLocal = "@"
這個還是不行...........
杜小平

TOP

回復 7# dou10801


   
With Workbooks.Open(xlsPath, , True).Sheets(1)
      .Cells.NumberFormatLocal = "@"
      Arr = .[A1].CurrentRegion
      brr = .[A1].CurrentRegion
~~
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

        靜思自在 : 人生最大的成就是從失敗中站起來。
返回列表 上一主題