2017-04-18 105 views
1

我想将值发布到列A的最后一行,然后将另一个值发布到同一行的列F那上面可能有空的空间。我将如何去完成这个?后列值到A列中的最后一行,另一个值列F列A列中发布的同一行

这是我到目前为止。

Sub test411() 

    Dim iRow As Integer, iCol As Integer 
    Dim sEntity As String, sEnt2 As String, sVal1 As Integer 
    Dim wsEntry As Worksheet 
    Dim wsUp As Worksheet 
    Set wsEntry = Worksheets("Entries") 
    Set wsUp = Worksheets("Sheet1") 
    Dim lastrow As Long 

    For iRow = 6 To 7 
    lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row 

    wsEntry.Activate 
    iCount = WorksheetFunction.CountIf(wsEntry.Range(Cells(iRow, 4), Cells(iRow, 11)), ">0") 
    sEntity = wsEntry.Cells(5, 4).Value 
    wsUp.Range("A" & lastrow + 1, "A" & lastrow + iCount).Value = sEntity 

    For iCol = 5 To 11 
     If Cells(iRow, iCol) > "0" Then 
     sEnt2 = wsEntry.Cells(5, iCol).Value 
     sVal1 = wsEntry.Cells(iRow, iCol).Value 
     lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row 
     wsUp.Range("A" & lastrow + 1, "A" & lastrow + 2).Value = sEnt2 
     wsUp.Cells("I", lastrow).Value = sVal1 
     End If 

    Next iCol 

    Next iRow 

End Sub 
+0

代码是否工作?您需要使用您希望运行的工作表限定'Rows.Count',否则它将在活动工作表上运行。所以我假设你想'lastrow = wsUp.Cells(wsUp.Rows.Count,“A”)。End(xlUp).Row'。 – BruceWayne

+0

我已经使用代码来包含所有的代码。所以不工作的部分是sVal1。 – Angel

+0

你可以做一个while循环吗?你能演示一个例子并添加屏幕截图吗? – Sid29

回答

1

也许以下解决方案可以帮助:

Sub test411() 

Dim wsUp As Worksheet 
Dim wsEntry As Worksheet 
Dim lastrow As Long, iCount As Long 
Dim iRow As Integer, iCol As Integer 

Set wsUp = Worksheets("Sheet1") 
Set wsEntry = Worksheets("Entries") 

For iRow = 6 To 7 
    lastrow = wsUp.Cells(wsUp.Rows.Count, "A").End(xlUp).Row 
    wsEntry.Activate 
    iCount = WorksheetFunction.CountIf(wsEntry.Range(wsEntry.Cells(iRow, 4), wsEntry.Cells(iRow, 11)), ">0") 
    wsUp.Range("A" & lastrow + 1, "A" & lastrow + iCount).Value = wsEntry.Cells(5, 4).Value 
    For iCol = 5 To 11 
     If Cells(iRow, iCol) > "0" Then 
      lastrow = wsUp.Cells(wsUP.Rows.Count, "A").End(xlUp).Row 
      wsUp.Range("A" & lastrow + 1, "A" & lastrow + 2).Value = wsEntry.Cells(5, iCol).Value 
      wsUp.Cells("I", lastrow).Value = wsEntry.Cells(iRow, iCol).Value 
     End If 
    Next iCol 
Next iRow 

End Sub 

说明:而不是使用这个变量来设置另一个单元分配单元格的值到一个变量,然后,我把短路线并立即将最终单元格设置为原始单元格的值(两者之间没有变量)。像这样,您不必担心Excel VBA变量和数据类型。

+0

好一个拉尔夫! – Sid29

+1

不要忘记在'iCount = ...'行和'Rows.Count'中限定'Cells()'行。 – BruceWayne

+0

@BruceWayne好的。下一次,请随意编辑我的文章并自己动手。 – Ralph

相关问题