2010-06-02 88 views
0

我正在通過VBA將ms訪問的數據填充到excel。我的數據庫包含許多組詳細信息,我想在羣體時間分隔組成員,問題是每個組的末尾我想在Excel表格中插入2行我使用下面提到的代碼,但是我不工作是否有可能?在excel中插入行

Dim varConnection 
Dim varSQL 
Dim cal, cal1, x 


    varConnection = "ODBC; DSN=MS Access Database;DBQ=table.accdb; Driver={Driver do Microsoft Access (*.accdb)}" 


    varSQL = "SELECT leftman.Productname,leftman.Description,leftman.SULM,leftman.MTR,leftman.meter1 FROM leftman INNER JOIN Product ON leftman.gid = Product.Productname ORDER BY Product.ID, leftman.ID" 
    With ActiveSheet.QueryTables.Add(Connection:=varConnection, Destination:=ActiveSheet.Range("B4")) 
      .CommandText = varSQL 
      .Name = "Query-39008" 
      .Refresh BackgroundQuery:=False 

    End With 
    x = Range("J5", Range("J5").End(xlDown)).Rows.Count 

    k1 = 5 
    k2 = 6 
    For i = 0 To x 
    s = k1 + i 
    s1 = k2 + i 

    If Range("J" & s & "").Value = Range("J" & s1 & "").Value Then 
    msgbox "same group" 

    Else 

    Range("J" & s & "").Select 
    ActiveCell.Offset(1).EntireRow.Insert 
    ActiveCell.Offset(1).EntireRow.Insert 
    k1 = 5 + 2 
    k2 = 6 + 2 

    End If 
    Next i 

首先我找回GroupWise中的數據從訪問 X是我的行數 K1是小區1和K2是小區2我是小區1單元2通過循環比較,如果數據不是
比賽那麼它是使細胞後,我想插入2行

回答

1

用戶可以很好的方式考慮的另一組:

x = Range("J5", Range("J5").End(xlDown)).Rows.Count 

For i = x To 5 Step -1 
    s = i 
    s1 = i - 1 

    If Range("a" & s & "").Value = Range("a" & s1 & "").Value Then 
     MsgBox "same group" 

    Else 

     Range("a" & s & "").Select 
     ActiveCell.EntireRow.Insert 
     ActiveCell.EntireRow.Insert 

    End If 
Next i 
0

在我的經驗,「選擇」功能不可靠。您可以通過以下方式避免它:

.Rows(s).Insert Shift:=xlDown 

假設我正確地理解's'是行號。

0
Range("a" & s & "").Select 
Selection.Insert Shift:=xlDown