2016-04-11 18 views
1

Yapıştırmak için yüzlerce hücreye ihtiyacım var. Ben excel VBA> word içinde veri (örneğin, "word tablo hücresi = 1") eklemek nasıl iyi anladım ve bu iyi çalışıyor, ama benim sorun tek bir kelime hücresine tek bir excel hücresini kopyalar.Kopyala Hücre verileri excel kelimesine VBA

Kodum şu anda:

'Do the For Loops here, WIP 
For Each Cell In ActiveSheet.UsedRange.Cells 
    'do some stuff 

    For Each oRow In wdDoc.Tables(1).Rows 
    For Each oCell In oRow.Cells 

    ' Set sCellText equal to text of the cell. 
    ' Note: This section can be modified to suit 
    ' your programming purposes. 
    If ((oCell.Column.Index Mod 2) = 0) Then 

    Else 
    'counter = counter + 1 

    'oCell.Range.Text = ThisWorkbook.Sheets(1).Cells(1, 2).Value 
    oCell.Range.Text = Cell 

    End If 

Next oCell 
Next oRow 
Next 

Ne şu anda yaptığından her kelime tablo hücresine, ilk excel hücreyi kopyalamak olacak. İkinci excel hücresine çarptığında, diğer her kelime tablo hücresinin üzerine yazar. Şu anda olup bitenlerin altında

Görünüm: Ben istiyorum ne

Image Not Working

gerçekleşmesi:. (ilk kelime tablo hücresine pastaları kopyalamak, hücreyi excel Okur - hücreyi excel ikinci okur, pastaları kopyalamak ikinci kelime tablo hücresine.

DesiredOutput

teşekkür,

Alex.

+0

Word ve Excel'de aynı boyutta tablolar var mı? Bu arada çok fazla döngüye sahipsin, sadece birine ihtiyacın var. – vacip

+0

Benzer boyut - tam olarak aynı boyutta değil. Sadece bir döngü ile bunun üzerine nasıl giderim? Hücre sayılarını tam olarak bilmediğim için Hücre (1,1) vb. Söylemekten kaçınmaya çalışıyordum. Şerefe. – Javanoob33

cevap

2

Bu kod hile yapmalıdır. Zaten benim testinde çalıştı: o işlevselliğini göstermektedir böylece

Dim firstRow As Integer 
Dim firstColumn As Integer 
Dim numRows As Integer 
Dim numColumns As Integer 

Dim uRange As Range 

Set uRange = ActiveSheet.UsedRange 
firstRow = uRange.Row 
firstColumn = uRange.Column 
numRows = uRange.Rows.Count 
numColumns = uRange.Columns.Count 

Dim rowNumber As Integer 
Dim columnNumber As Integer 
Dim tableRow As Integer 
Dim tableColumn As Integer 
tableRow = 1 
tableColumn = 1 

Dim tableRows As Integer 
Dim tableColumns As Integer 
tableRows = doc.Tables(1).Rows.Count 
tableColumns = doc.Tables(1).Columns.Count 

For rowNumber = firstRow To firstRow + numRows - 1 
    For columnNumber = firstColumn To firstColumn + numColumns - 1 
     doc.Tables(1).Cell(tableRow, tableColumn).Range.Text = Cells(rowNumber, columnNumber).Value 
     tableColumn = tableColumn + 2 
     If tableColumn > tableColumns Then 
      tableColumn = 1 
      tableRow = tableRow + 1 
     End If 
     If tableRow > tableRows Then 
      Exit Sub 
     End If 
    Next 
Next 

Ben bilerek farklı boyut giriş ve çıkış aralıkları seçti.

Excel sayfası (giriş):

enter image description here

Kelime sayfası (çıkış):

enter image description here

Bu yöntemle, size o zaman satırları okumak isteyip üzerinde kontrole sahip sütunlar veya sütunlar sonra satırlar.

UsedRange işlevinin her zaman doğru sonuçları döndürmediğini unutmayın. İstediğinizden daha geniş bir aralık vereceği durumlar var.

+0

Bu harika, çok teşekkürler! mükemmel çalışır. Haha olduğumdan çok daha nezih. – Javanoob33