我正在制作一个代码,可以将数据从一张纸复制到另一张(请参见下文),它目前可以在一张纸上工作,但现在我希望这些代码能够在多张纸上工作,或者任何工作表都处于活动状态。你可以帮我修改这段代码,以便在多张纸上工作。
Sub sbCopyRangeToAnotherSheet()
Sheets("Sheet1").Select
Range("A15:E188").Select
Selection.ClearContents
Range("A15").Select
Sheets("FCI").Select
Range("C51").Select
'Copy the data
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Activate the destination worksheet
Sheets("Sheet1").Activate
'Select the target range
Range("A15").Select
'Paste in the target destination
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End Sub
答案 0 :(得分:0)
使用循环修改代码以浏览工作簿中的所有工作表。 如果这是必要的,请告诉我。
Sub sbCopyRangeToAnotherSheet()
Sheets("Sheet1").Select
Range("A15:E188").Select
Selection.ClearContents
Range("A15").Select
Sheets("FCI").Select
Range("C51").Select
'Copy the data
For Each Sheet In ActiveWorkbook.Sheets
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Activate the destination worksheet
Sheets("Sheet1").Activate
'Select the target range
Range("A15").Select
'Paste in the target destination
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Next
End Sub
答案 1 :(得分:0)
正如我的代码所理解的那样,您希望能够将数据从任何活动工作表复制到FSI表。如果是这种情况,我已经做了如下代码所做的更改。
declare @documentId int;
set @documentId = (SELECT MAX(DOCUMENT_ID) FROM dbo.DOCUMENTS)+ 1
INSERT INTO [dbo].[DOCUMENTS] (
DOCUMENT_ID
,DOCUMENT_TYPE
,DOCUMENT_REF
,DOCUMENT_NOTE
,DOCUMENT_DATE
,LAST_UPDATE_ID
,LAST_UPDATE_DATE
,DOCUMENT_FUNCTION_CODE
,BATCH_ID
,AUDIT_XML
,AUDIT_USER
,REMINDER_DATE)
SELECT
@documentId,
NULL,
NULL,
'MIGRATED FROM BRING UP NOTES ' + CAST(GETDATE() AS VARCHAR(20)) + ' ' + sr.BRINGUP_NOTES,
GETDATE(),
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
sr.BRINGUP_DATE
FROM [dbo].[SERVICE_REQUESTS] sr
----- Insert into another table ------------
INSERT INTO [dbo].[SERVICE_REQUEST_ITEM_DOCS] (
[SERVICE_REQUEST_ID]
,[SERVICE_REQUEST_ITEM_SEQNO]
,[DOCUMENT_ID]
,[LAST_UPDATE_ID]
,[LAST_UPDATE_DATE]
,[COVER_LETTER])
SELECT
sr.SERVICE_REQUEST_ID,
1,
@documentId,
0,
GETDATE(),
0
FROM [dbo].[SERVICE_REQUESTS] sr
答案 2 :(得分:0)
可能这就是您正在寻找的东西,您也可以根据自己的要求自行选择代码。
{{1}}