標題:
[發問]
如何進行sheet1表不連續拷貝和回存資料?
[打印本頁]
作者:
luke
時間:
2012-4-19 21:19
標題:
如何進行sheet1表不連續拷貝和回存資料?
各位大大
小弟想把sheet1表B欄中兩筆資料B1:B64和B101:B164內容
拷貝至sheet2表內所指定的特定儲存格即C:R欄做整理,
然後將sheet2這些特定儲存格內容回存至sheet1表原來的儲存格位置.
請問如何進行sheet1表不連續拷貝和回存資料?
煩請先進指導.
[attach]10516[/attach]
作者:
register313
時間:
2012-4-19 23:43
回復
1#
luke
Sub 轉入()
Sheet2.Columns("C:R") = ""
For C = 1 To 4
Sheet1.[B1:B16].Offset(16 * (C - 1), 0).Copy Sheet2.[C3].Offset(0, 5 * (C - 1))
Sheet1.[B101:B116].Offset(16 * (C - 1), 0).Copy Sheet2.[C22].Offset(0, 5 * (C - 1))
Next C
Sheet2.[C11:C12,C26,C30:C31,C34].EntireRow.Insert
End Sub
Sub 轉出()
Dim AR()
I = 0
For X = 1 To 2
If X = 1 Then
Set Rng = Sheet2.[C3:C20]
I = 0
Else
Set Rng = Sheet2.[C24:C43]
I = 0
End If
For C = 1 To 4
For Each A In Rng.Offset(0, 5 * (C - 1))
If A.Value <> "" Then
ReDim Preserve AR(I)
AR(I) = A.Value
I = I + 1
End If
Next
Next C
If X = 1 Then
Sheet1.[B1].Resize(I, 1) = Application.Transpose(AR)
Else
Sheet1.[B101].Resize(I, 1) = Application.Transpose(AR)
End If
Next X
End Sub
複製代碼
作者:
luke
時間:
2012-4-20 15:37
回復
2#
register313
謝謝R大多次協助
轉出資料OK,
但sheet2表 C:R欄有些儲存格有限制,因此轉入時不能清除內容
小弟錄製了乙個巨集Macro1
煩請先進 大大指導是否可以簡化程式內容
[attach]10536[/attach]
作者:
register313
時間:
2012-4-20 16:28
回復
3#
luke
程式前半段為儲存格複製(因儲存格位置變化太多,其他儲存格又不能更動,所以程式沒太大簡化)
程式後半段為儲存格框線之繪制
Sub 轉入()
Sheets("sheet1").Range("B1:B8").Copy Sheets("sheet2").Range("C3:C10")
Sheets("sheet1").Range("B9:B16").Copy Sheets("sheet2").Range("C13:C20")
Sheets("sheet1").Range("B17:B24").Copy Sheets("sheet2").Range("H3:H10")
Sheets("sheet1").Range("B25:B32").Copy Sheets("sheet2").Range("H13:H20")
Sheets("sheet1").Range("B33:B40").Copy Sheets("sheet2").Range("M3:M10")
Sheets("sheet1").Range("B41:B48").Copy Sheets("sheet2").Range("M13:M20")
Sheets("sheet1").Range("B49:B56").Copy Sheets("sheet2").Range("R3:R10")
Sheets("sheet1").Range("B57:B64").Copy Sheets("sheet2").Range("R13:R20")
Sheets("sheet1").Range("B101:B104").Copy Sheets("sheet2").Range("C24:C27")
Sheets("sheet1").Range("B105:B108").Copy Sheets("sheet2").Range("C29:C32")
Sheets("sheet1").Range("B109:B112").Copy Sheets("sheet2").Range("C35:C38")
Sheets("sheet1").Range("B113:B116").Copy Sheets("sheet2").Range("C40:C43")
Sheets("sheet1").Range("B117:B120").Copy Sheets("sheet2").Range("H24:H27")
Sheets("sheet1").Range("B121:B124").Copy Sheets("sheet2").Range("H29:H32")
Sheets("sheet1").Range("B125:B128").Copy Sheets("sheet2").Range("H35:H38")
Sheets("sheet1").Range("B129:B132").Copy Sheets("sheet2").Range("H40:H43")
Sheets("sheet1").Range("B133:B136").Copy Sheets("sheet2").Range("M24:M27")
Sheets("sheet1").Range("B137:B140").Copy Sheets("sheet2").Range("M29:M32")
Sheets("sheet1").Range("B141:B144").Copy Sheets("sheet2").Range("M35:M38")
Sheets("sheet1").Range("B145:B148").Copy Sheets("sheet2").Range("M40:M43")
Sheets("sheet1").Range("B149:B152").Copy Sheets("sheet2").Range("R24:R27")
Sheets("sheet1").Range("B153:B156").Copy Sheets("sheet2").Range("R29:R32")
Sheets("sheet1").Range("B157:B160").Copy Sheets("sheet2").Range("R35:R38")
Sheets("sheet1").Range("B161:B164").Copy Sheets("sheet2").Range("R40:R43")
With Sheets("sheet2").Range("C1:R43").Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 0
End With
End Sub
複製代碼
作者:
luke
時間:
2012-4-20 22:14
回復
4#
register313
謝謝R大幫忙
作者:
Hsieh
時間:
2012-4-20 22:36
回復
5#
luke
試試看
Sub 轉入()
With Sheet1
r = 1: i = 3: k = 3
Do Until r > 163
.Cells(r, 2).Resize(IIf(r >= 65, 4, 8), 1).Copy Sheet2.Cells(i, k)
If r = 65 Then
r = 101
Else
r = r + IIf(r >= 65, 4, 8)
End If
If r <= 65 Then
i = IIf(i = 3, 13, 3): k = IIf(i = 3, k + 5, k)
ElseIf Int(((Int((r - 1) / 4) - 24) - 1) / 2) Mod 2 = 0 Then
i = IIf(r = 101, 24, IIf(i = 24, 29, 24)): k = IIf(r = 101, 3, IIf(i = 24, k + 5, k))
Else
i = IIf(i = 29, 35, IIf(i = 35, 40, 35)): k = IIf(i = 24, k + 5, k)
End If
Loop
End With
End Sub
Sub 轉出()
Dim A As Range, C As Range
With Sheet2
Set A = Union(.[C3:R20], .[C24:R43])
For i = 1 To A.Areas.Count
r = IIf(i = 1, 1, 101)
For j = 1 To 16 Step 5
Set C = A.Areas(i).Columns(j).SpecialCells(xlCellTypeConstants)
'C.Select
C.Copy Sheet1.Cells(r, 3)
r = r + C.Count
Next
Next
End With
End Sub
複製代碼
作者:
luke
時間:
2012-4-20 23:30
回復
6#
Hsieh
謝謝H大
第30列 應改為C.Copy Sheet1.Cells(r,
2
)
此外, sheet2表C1:R4, 若儲存格有格線,
轉出到sheet1表B欄時, 如何消除格線
煩請先進指導
作者:
register313
時間:
2012-4-20 23:46
回復
7#
luke
Sheet1.Range("B:B").Borders.LineStyle = xlNone
作者:
luke
時間:
2012-5-3 22:14
回復
6#
Hsieh
回覆H大
執行"轉入"後, sheet2表W3:W62多出了4個如附件黃色區塊
煩請先進 指導修改程式
[attach]10795[/attach]
作者:
Hsieh
時間:
2012-5-3 22:53
回復
9#
luke
Sub 轉入()
Dim Rng As Range
With Sheet1
j = 3
For i = 1 To 57 Step 8
k = k + 1
r = IIf(k Mod 2 = 1, 3, 13)
Set Rng = .Cells(i, 2).Resize(8, 1)
Rng.Copy Sheet2.Cells(r, j)
j = IIf(r = 13, j + 5, j)
Next
j = 3
For i = 101 To 161 Step 4
k = k + 1
x = k Mod 4
r = IIf(x = 1, 24, IIf(x = 2, 29, IIf(x = 3, 35, 40)))
Set Rng = .Cells(i, 2).Resize(4, 1)
Rng.Copy Sheet2.Cells(r, j)
j = IIf(r = 40, j + 5, j)
Next
End With
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)