在一个记事本中组合两个工作表数据

时间:2018-03-22 10:28:26

标签: excel vba excel-vba excel-formula

我是VBA的新手, 我有一个工作簿,有3个工作表。 Sheet1在Sheet2中有一个引用,而Sheet3是独立的。

我需要在记事本中组合Sheet1和Sheet3数据。

我可以单独执行脚本并且工作正常但是当我尝试将2张数据组合到一个记事本中时,它只打印sheet3数据而不是Sheet1数据。

以下是我的剧本。

Sub myself()

Dim str As String
Dim MaxStrLen As String
Dim rest As Integer
Dim Lstr As Integer
Dim LMstr As Integer
Dim MStr As Integer
Dim LR As Range
Dim CNT As Integer

Dim LastRow As Long
Dim LastCol As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim i As Long
Dim j As Long
Dim h As Long
Dim k As Long

Dim FilePath As String

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("Sheet3")

Open "C:\Users\Antony\Music\Excel Macros\Test2.txt" For Output As #2

'''''FIRST FIVE LINES WILL PRINT IN THE NOTEPAD

With ws1
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

    For i = 1 To LastRow
        sOut = vbNullString
        For j = 1 To LastCol
            str = .Cells(i, j).Value
            MStr = ws2.Cells(i, j).Value
            Lstr = Len(str)
            rest = MStr - Lstr
            sOut = sOut & str & Space(rest)
        Next
        Print #2, sOut
    Next

    '''''LAST LINE WILL PRINT IN THE SAME NOTEPAD

    ws3.Activate

    For k = 2 To LastRow
      str = Join(Application.Transpose(Application.Transpose(.Cells(k, "A").Resize(1, LastCol).Value)), "@#")
      str = Replace(str, "=", vbNullString)

        Print #2, str
    Next


End With


Close #2

End Sub

请帮我在SAME记事本中打印;即前五行+最后一行

2 个答案:

答案 0 :(得分:0)

这是我的脚本,工作正常     Sub我自己()

Dim str As String
Dim MaxStrLen As String
Dim rest As Integer
Dim Lstr As Integer
Dim LMstr As Integer
Dim MStr As Integer
Dim LR As Range
Dim CNT As Integer

Dim LastRow As Long
Dim LastCol As Long
Dim LRow As Long
Dim LCol As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim i As Long
Dim j As Long
Dim h As Long
Dim k As Long

Dim FilePath As String

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("Sheet3")

Open "C:\Users\Antony\Music\Excel Macros\Test.txt" For Output As #2

'''''FIRST FIVE LINES WILL PRINT IN THE NOTEPAD

With ws1
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

For i = 1 To LastRow
    sOut = vbNullString
    For j = 1 To LastCol
        str = .Cells(i, j).Value
        MStr = ws2.Cells(i, j).Value
        Lstr = Len(str)
        rest = MStr - Lstr
        sOut = sOut & str & Space(rest)
    Next
    Print #2, sOut
Next
End With
'''''LAST LINE WILL PRINT IN THE SAME NOTEPAD

With ws3
    LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

slast = vbNullString

For k = 2 To LRow
  str = Join(Application.Transpose(Application.Transpose(.Cells(k, "A").Resize(1, LastCol).Value)), "@#")
  str = Replace(str, "=", vbNullString)

    Print #2, str
Next

Endtext = "EODR"

Print #2, slast & Endtext

End With


Close #2

End Sub

答案 1 :(得分:0)

我看到你找到了一个解决方案,但是,有一个更好的方法可以确保所有内容都打印到相同的输出。

  

在记事本中加载您想要的每一行数据的变体   然后只输出一次输出。这比显着更快   一遍又一遍地打印

以下是一个通用示例,用于输出A1:A10Sheet1Sheet3的值

Option Explicit

Sub Test()
    Dim newFileName As String
    Dim v As Variant
    Dim fileNumber As Long, i As Long

    ReDim v(1 To 20)
    For i = 1 To 10
        v(i) = Sheet1.Cells(i, "A").Value
        v(i + 10) = Sheet3.Cells(i, "A").Value
    Next i

    fileNumber = FreeFile
    newFileName = "C:\NewTextFile.txt"

    Open newFileName For Output As #fileNumber
    Print #fileNumber, Join(v, vbCrLf)

    Close #fileNumber
End Sub