2016-03-22 32 views
1

için tablo başlıkları ve içerikleri ve ekran çıktısını alın: VBA: Bir tablo böyle sahip bir dosyaya

  Name Color Shape Size Cost Note 1 Note 2 
Ctrl But A Fruit 1 a  d  f  1  6  9 
Ctrl But B Fruit 2 b  e  g  2  7 
Ctrl But C Fruit 3 a  d  f  3  10 
Ctrl But D Fruit 4 b  e  g  4  8 

burada 'Ctrl Ama A' kontrol düğmeleri vardır.

Her bir düğmeye basıldığında, belirli bir biçimde satır bilgilerini csv dosyasına vermek istiyorum.

Name: Fruit A 
Colour: a 
Shape: d 
Size: f 
Cost: 1 
Note 1: 6 
Note 2: 9 

tablonun sütunları değişken olup olmadığını Bunu başarmak için en iyi yolu nedir: A butonuna basıldığında Örneğin, ben şu çıktı istersiniz?

kod böyle bir şey yapacağını:

1. go to the top of the 'Name' cell 
2. look up the value in the same row as the button 
3. go to the top of the 'Color' cell 
4. look up the value in the same row as the button 
repeat until the end of the row is reached 
output and save file 

Sizin Yardım büyük takdir!

cevap

1

varsayarak:

  • hücreleri yatan düğmeler boş verilerle
  • sütun sağ düğmelerin başlamak

bu

Option Explicit 

Sub FruitButton() 
Dim iRow As Long, iniCol As Long, endCol As Long, jCol As Long 

Open "C:\mypath\myfile.csv" For Output As #1 '<== adapt it to your needs 

With ActiveSheet 
    Call GetIniEndCol(.Buttons(Application.Caller), ActiveSheet, iRow, iniCol, endCol) 
    For jCol = iniCol To endCol 
     Write #1, .Cells(1, jCol) & ": " & .Cells(iRow, jCol) 
    Next jCol 
End With 

Close #1 

End Sub 


Sub GetIniEndCol(myButton As Button, ws As Worksheet, iRow As Long, iniCol As Long, endCol As Long) 
Dim iCol As Long 

With myButton.TopLeftCell 
    iRow = .Row 
    iCol = .Column 
End With 

With ws 
    iniCol = .Cells(iRow, iCol).End(xlToRight).Column 
    endCol = .Cells(iRow, .Columns.Count).End(xlToLeft).Column 
End With 

End Sub 
deneyebilirsiniz
İlgili konular