简体   繁体   English

复制和粘贴范围内的仅已使用的单元格

[英]Copy and paste only used cells in range

I would really appreciate a little help. 我真的很感谢您的帮助。 I have two open Workbooks, one is used for calculations, second one for keeping records. 我有两本打开的工作簿,一本用于计算,第二本用于保存记录。 I used to do everything manually, but then I discovered macros and VBA, but I am a beginner. 我曾经手动完成所有操作,但后来发现了宏和VBA,但我是一个初学者。 I have managed to write a code, that works for me, but I would like to have it improved. 我设法编写了一个对我有用的代码,但我想对其进行改进。

I set a range Y22:Y37 (Sheets have same name in both workbooks), which is not always populated with values completely, but I don't know how to change the code to copy only used cells in the range. 我设置了一个范围Y22:Y37(两个工作簿中的表名都相同),它并不总是完全填充值,但是我不知道如何更改代码以仅复制该范围中使用的单元格。 I tried to use SkipBlanks:=True, but it didn't work. 我尝试使用SkipBlanks:= True,但是没有用。

Once I copy the range I activate the second workbook, find the first empty row and paste transposed values there (starting in column B on purpose). 复制范围后,我激活了第二个工作簿,找到第一个空行并将转置的值粘贴到那里(有意从B列开始)。 But again, I paste the whole range Y22:Y37, which is unnecessary I think. 但是,我再次粘贴整个范围Y22:Y37,我认为这是不必要的。 Plus I would like to have a bottom border under used cells after pasting them. 另外,我想在粘贴完使用过的单元格后在底部边框。 In the Picture, you can see that meanwhile I managed to make bottom border, but I used entire row. 在图片中,您可以看到同时我设法制作了下边框,但是我使用了整行。

I somehow adjusted to my needs various codes I could find, but I know I have probably used many redundant parts of the code, but I hope someone can help me make it cleaner. 我以某种方式适应了我可以找到的各种代码的需求,但是我知道我可能已经使用了代码的许多冗余部分,但是我希望有人可以帮助我使它变得更整洁。 Thank you very much in advance, even for reading this far. 非常感谢您,即使您已阅读本文。 Pictures of workbooks are in links below. 下面的链接中包含工作簿的图片。

Sub CopyVyuctovani()
Set TargetWB = Workbooks("Výdej.xlsm")
Set SourceWB = Workbooks("DPV.xlsm")
TargetSH = ActiveSheet.Name
SourceWB.Sheets(TargetSH).Range("Y22:Y37").Copy
TargetWB.Sheets(TargetSH).Activate
lMaxRows = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & lMaxRows + 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Range("A" & lMaxRows + 1).Value = SourceWB.Sheets("Souhrn").Range("E30").Value
Application.CutCopyMode = False
Range("A" & Rows.Count).End(xlUp).EntireRow.Font.Color = RGB(255, 0, 0)
Range("A" & Rows.Count).End(xlUp).EntireRow.Borders(xlEdgeBottom).LineStyle = xlContinuous
End Sub

Source Workbook 源工作簿

Target Workbook 目标工作簿

The code is more or less the same but it will fix your two problems 代码大致相同,但可以解决您的两个问题

Sub CopyVyuctovani()

    Dim targetWB As Workbook
    Dim sourceWb As Workbook
    Dim targetSH As String
    Dim lmaxrows As Long

    Set targetWB = Workbooks("Výdej.xlsm")
    Set sourceWb = Workbooks("DPV.xlsm")
    targetSH = ActiveSheet.Name

    sourceWb.Sheets(targetSH).Range("Y22:Y37").Copy

    With targetWB.Sheets(targetSH)
        lmaxrows = Cells(Rows.Count, "B").End(xlUp).Row
        .Range("B" & lmaxrows + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
        .Range("A" & lmaxrows + 1).Value = sourceWb.Sheets("Souhrn").Range("E30").Value
        Application.CutCopyMode = False
        .Range("A" & lmaxrows & ":Q" & lmaxrows).Font.Color = RGB(255, 0, 0)
        .Range("A" & lmaxrows & ":Q" & lmaxrows).Borders(xlEdgeBottom).LineStyle = xlContinuous
    End With

End Sub

@Imran Malek @伊姆兰·马勒克

thank you, with this one I get no error, great :) But somehow, the copied range is pasted into line 38 of target WB (maybe it uses the last row 37 in source wb?) so I tried to activate the target WB first and it seems it works. 谢谢你,有了这个我没有任何错误,很好:)但是以某种方式,复制的范围被粘贴到目标WB的第38行中(也许它使用了源wb的最后一行37?),所以我尝试首先激活目标WB似乎可行。 Then I had a problem with formatting, with your code the format was used on a row that is exactly above the pasted one. 然后我在格式化方面遇到了问题,在您的代码中,该格式在刚好粘贴的行上使用了。 So I added +1 to 1maxrows and it looks good now. 所以我将+1加到1maxrows,现在看起来不错。 The code looks like this now. 现在的代码看起来像这样。

Sub CopyVyuctovani()

Dim targetWB As Workbook
Dim sourceWb As Workbook
Dim targetSH As String
Dim lmaxrows As Long

Set targetWB = Workbooks("Výdej.xlsm")
Set sourceWb = Workbooks("DPV.xlsm")
targetSH = ActiveSheet.Name

sourceWb.Sheets(targetSH).Range("Y22:Y37").Copy

With targetWB.Sheets(targetSH)
    .Activate
    lmaxrows = Cells(Rows.Count, "B").End(xlUp).Row
    .Range("B" & lmaxrows + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
    .Range("A" & lmaxrows + 1).Value = sourceWb.Sheets("Souhrn").Range("E30").Value
    Application.CutCopyMode = False
    .Range("A" & lmaxrows + 1 & ":Q" & lmaxrows + 1).Font.Color = RGB(255, 0, 0)
    .Range("A" & lmaxrows + 1 & ":Q" & lmaxrows + 1).Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
End Sub

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

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