Consulting

Results 1 to 2 of 2

Thread: Exporting an multidimensional array from an Ecxel file to a new Excel file (.xls)

  1. #1

    Exporting an multidimensional array from an Ecxel file to a new Excel file (.xls)

    Hey, guys,

    I'm a beginner at VBA in Excel, so unfortunately I quickly reached the limits of my knowledge.

    A little background:
    I use a laser to measure speed fields in a pipe. For this purpose, an Excel calculation was made which converts the pipe positions into the position that the laser has to move to.
    There is the X Y and Z axis here. So 3 columns.
    The whole thing is limited to 100 lines because nobody enters so many values.

    The software of the laser has the option to upload files via "Import Mesh" (.xls), so that you do not have to transfer everything individually into the software.
    However in the new file in column A the X coordinate must be column B the Y and column C the Z coordinate.

    So far so good.

    The files to be exported can be found in column 9 - 11 from line 2.
    e.g. in this format
    123
    456
    789

    I have proceeded in such a way that I first let myself read in how big the array is at all and is then fixed on it. That works well... may not be nice but it works.

    Then I create a new document with the name Export.xls.
    and export the array to the new file.

    My problem is that it doesn't output it in the new file as above, it looks like this:
    1
    2
    3
    4
    5
    6
    7
    8
    9

    So what I miss is something that makes it stand next to each other and not everything among each other I have already searched a few forums and watched videos but not found. Bzw nothing found what I understand ...
    Since I am a beginner I would still like to leave it as simple as possible

    Here still the source code:

    Option Explicit
    Sub InformationenExportieren()
    
        'Variablen definieren
        Dim Zieldatei As String 'Speicherort der Text Datei
        Dim p As Integer
        Dim i As Integer, j As Integer 'Schleifenvariable
        Dim size
        
        Dim A()
        
        'einzählen welche Array größe
        For p = 2 To 100
        
            If Cells(p, 9).Value = "" Then
                Exit For
            Else
            
            size = size + 1
          End If
        Next
        
        'einlesen des Array
        ReDim A(size - 1, 2)
        
        For i = 0 To size - 1
            For j = 0 To 2
            
            A(i, j) = Cells(i + 2, j + 9).Value
            
            Next j
            
        Next i
    
            
        'Fehlermarke einfügen
        On Error GoTo FehlerMarke
        
        'Tabellenblattaktivieren
        ThisWorkbook.Worksheets("waagerechtes Rohr").Activate
        
        'ZielDatei erstellen
        Zieldatei = ThisWorkbook.Path & "\Export.xls"
        
        'ZielDatei öffnen
        Open Zieldatei For Output As #1
        
        'Information in Zieldatei einfügen
              
        For j = 0 To 2
            
            For i = 0 To size - 1
        
            Print #1, A(i, j)
            
           Next i
            
        Next j
    
        
        'Zieldatei schließen
        Close #1
        
        Exit Sub
        
    FehlerMarke:
        MsgBox Err.Description
        
    End Sub
    Sorry for my german comments

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    As a possible starter, try:
    Sub InformationenExportieren3()
    With ActiveSheet
      Set NewWkBk = Workbooks.Add(xlWBATWorksheet)
      .Range(.Cells(2, 9), .Cells(2, 9).End(xlDown)).Resize(, 3).Copy NewWkBk.Sheets(1).Cells(1)
    End With
    On Error GoTo FehlerMarke
    NewWkBk.SaveAs Filename:=ThisWorkbook.Path & "\Export3.xls", FileFormat:=xlExcel8 'note filename
    NewWkBk.Close False
    Exit Sub
    FehlerMarke:
    MsgBox Err.Description
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •