我在这里有一个代码,它可以很好地完成我想要的报告,但唯一的问题是我想在将数据从一个文件复制到另一个文件时排除一列。我的代码从A列复制到I但我想从H中排除所有数据,并且可能将数据从I移到H,所以我没有空列H.希望这有意义任何帮助表示赞赏谢谢。
Serial.println("food");
Temp = Serial.read();
答案 0 :(得分:0)
您可以使用Union
创建要复制的非连续范围,例如
Sub Distinct()
Const TRNS_START As String = "TRNS"
Const TRNS_END As String = "ENDTRNS"
Const COMPANY As String = "Triumph Foods LLC"
Dim searchRng As Range, copyRngStart As Range, copyRngEnd As Range, copyRng As Range
Dim wsFrom As Worksheet
Set wsFrom = Worksheets("Information")
Set searchRng = wsFrom.Range("A1")
' Enter/continue loop while A-column is non-empty
Do While Not IsEmpty(searchRng.Value)
' When we encounter the string TRNS in column A and Triumph Foods LLC in column E
If searchRng.Value = TRNS_START And _
searchRng.Offset(0, 4).Value = COMPANY Then
Set copyRngStart = searchRng ' Set the start of the copy area
End If
' When we encounter the string ENDTRNS
' (*and had a start cell already*)
If searchRng.Value = TRNS_END And Not copyRngStart Is Nothing Then
Set copyRngEnd = searchRng.Offset(-1, 6) ' A:G
Set copyRng = wsFrom.Range(copyRngStart, copyRngEnd)
' union with I
Set copyRng = Application.Union(copyRng, copyRng.Columns(1).Offset(, 8))
copyRng.Copy _
Destination:=Sheets("Display").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Set copyRngStart = Nothing 'clear the "start" range
End If
Set searchRng = searchRng.Offset(1, 0)
Loop
End Sub
答案 1 :(得分:-1)
我不喜欢使用VBA进行复制,但更喜欢直接分配值。对于您的具体问题,我建议将值分配为两部分。因此,在您复制值的代码中,您将使用以下代码替换代码:
Sheets("Display").Range("A" & (Sheets("Display").Columns(1).Rows.Count + 1) & ":H" & (Rows.Count + 1)).Value2 = Worksheets("Information").Range("A1:G1").Offset(copyRngStart.Row, 0)
Sheets("Display").Range("I" & (Sheets("Display").Columns(1).Rows.Count + 1)).Value2 = Worksheets("Information").Range("H1").Offset(copyRngStart.Row, 0)
这将首先从A到G直接写入,然后将I写入H.Hope这对您有用。