简体   繁体   English

Excel复制范围并粘贴到可用的特定范围并打印

[英]Excel copy range and paste in a specific range available and print

I would like to copy a range in one sheet and paste it as a value in another sheet, but just in a specific range in the next available cell in column B. Starting from B4 to B23 only. 我想在一个工作表中复制一个范围并将其作为值粘贴到另一工作表中,但只在B列中下一个可用单元格中的特定范围内。仅从B4到B23。

I changed some code I found online but it's not working for me in finding the next available row. 我更改了一些在网上找到的代码,但是对寻找下一个可用行不起作用。 After I run the macro the first time, when I run it again and again it does nothing, and it's not working in pasting only the values either. 第一次运行宏后,当我一次又一次运行它时,它什么也不做,也无法仅粘贴值。

I tried saving the file before running the Macro again, but still it's not working. 我尝试在再次运行宏之前保存文件,但是仍然无法正常工作。

At the end, when the range in the Print sheet is full, I would like a message box asking me to select one of the printers (not the default) on one of my servers (specifying the server path in the code like \\a_server_name) and print this Print Sheet only, or clear the records in the range in the Print Sheet, or save only the Sheet Print in a new file (SaveAs) to a location I can choose on one of my servers (specifying the server path in the code \\a_server_name) or simply do nothing and end the sub. 最后,当“打印”表中的范围已满时,我想要一个消息框,要求我在我的一台服务器上选择一台打印机(不是默认打印机)(在\\ a_server_name之类的代码中指定服务器路径)并仅打印此打印纸,或清除打印纸中范围内的记录,或仅将新打印文件中的工作表打印(另存为)保存到我可以在其中一台服务器上选择的位置(在代码\\ a_server_name)或不执行任何操作并结束子代码。

Thank you. 谢谢。

Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

Set copySheet = Worksheets(“Data”)
Set pasteSheet = Worksheets("Print”)

copySheet.Range("J11:Q11").Copy
pasteSheet.Range("B4:I23").End(xlUp).Offset(1,0)
.PasteSpecial.xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True

在此处输入图片说明

在此处输入图片说明

问题截图

This will set the values equal to each other without copying/pasting. 这将使值彼此相等,而无需复制/粘贴。

Option Explicit

Sub Testing()

Dim wsC As Worksheet: Set wsC = ThisWorkbook.Sheets("Data")
Dim wsP As Worksheet: Set wsP = ThisWorkbook.Sheets("Print")

Dim LRow As Long
LRow = wsP.Range("B" & wsP.Rows.Count).End(xlUp).Offset(1).Row

wsP.Range("B" & LRow).Resize(wsC.Range("J11:Q11").Rows.Count, wsC.Range("J11:Q11").Columns.Count).Value = wsC.Range("J11:Q11").Value

End Sub

Modifying your code - and reducing to minimal example 修改您的代码-并减少到最少的示例

Sub test()

Dim copySheet As Worksheet: Set copySheet = Worksheets("Data")
Dim pasteSheet As Worksheet: Set pasteSheet = Worksheets("Print")

copySheet.Range("J11:Q11").Copy
pasteSheet.Range("B" & pasteSheet.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

End Sub

From what i can gather, you want to copy 8 cells and paste all 8 cells to 20 rows, starting at B4. 根据我的收集,您想复制8个单元格并将所有8个单元格粘贴到20行(从B4开始)。 You are not clear on how you want to rerun the macro, it will just write over the data you just pasted. 您不清楚如何重新运行宏,它只会覆盖您刚刚粘贴的数据。

The first code will copy the 8 cells into the 20 rows 第一个代码会将8个单元格复制到20行中

With ThisWorkbook
        Sheets("Data").Range("J11:Q11").Copy
        Sheets("Print").Range("B4:I23").PasteSpecial Paste:=xlPasteValues
End With

This second code uses a for loop to accoplish the same task, but it also will write over the previously pasted data. 第二个代码使用for循环来完成相同的任务,但它还将覆盖先前粘贴的数据。

Dim i As Long
    With ThisWorkbook
        For i = 4 To 23
            Sheets("Data").Range("J11:Q11").Copy
            Sheets("Print").Cells(i, 2).PasteSpecial Paste:=xlPasteValues
        Next i
    End With

If you want to be able to reuse the macro, you will have to modify the range to be copied that allows you to select the range you want to copy. 如果要能够重用该宏,则必须修改要复制的范围,以允许您选择要复制的范围。 Maybe a variable that allows a user input with a InputBox . 也许是允许用户使用InputBox输入的变量。

Edit: 编辑:

Dim lRow As Long
lRow = Sheets("Print").Cells(Rows.Count, 2).End(xlUp).Row

    With ThisWorkbook
            Sheets("Data").Range("J11:Q11").Copy
            Sheets("Print").Cells(lRow, 2).Offset(1).PasteSpecial Paste:=xlPasteValues
    End With

Edit #3 编辑#3

With ThisWorkbook
    Dim lRow As Long
    lRow = .Sheets("Print").Range("B" & Rows.Count).End(xlUp).Row
        Sheets("Data").Range("J11:Q11").Copy
        Sheets("Print").Cells(lRow, 2).Offset(1).PasteSpecial Paste:=xlPasteValues
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM