You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA代码调试请求:按指定行号复制工作表行至目标位置

Fixing Your VBA Copy-Paste Issue

Let's figure out why your code is stalling and get it working as intended:

The main issues with your current code:

  • Incorrect quote characters: You’re using full-width Chinese quotes (“”) for sheet names (like Sheets(“Log”)). VBA only accepts standard half-width English quotes (""), which is almost certainly why your code stops at the copy line — it’s hitting a compilation error it can’t resolve.
  • Not using the row number from G15: You read the value from PrintSheet!G15 into the val variable, but then you hardcode Rows(15) instead of referencing Rows(val). So your macro isn’t actually using the row number the user inputs!
  • Unnecessary Select/Paste: Using .Select and .Paste is error-prone (if the user clicks another sheet while the macro runs, it’ll paste in the wrong place) and less efficient than direct copy-pasting.

Here’s the corrected code:

Sub GetOrder()
    Dim targetRow As Integer
    ' Grab the row number from PrintSheet's G15 cell
    targetRow = Sheets("PrintSheet").Range("G15").Value
    
    ' Copy the specified row from Log and paste directly to PrintSheet row 40
    Sheets("Log").Rows(targetRow).EntireRow.Copy _
        Destination:=Sheets("PrintSheet").Range("A40")
End Sub

Quick breakdown of the fixes:

  1. Swapped all full-width quotes for standard English quotes to eliminate the compilation error.
  2. Now the macro uses the targetRow variable (pulled from G15) to select which row to copy from the Log sheet, matching your original requirement.
  3. Removed the .Select and .Paste steps by using the Destination parameter in the .Copy method — this is a cleaner, more reliable way to handle copy-paste operations in VBA.

内容的提问来源于stack exchange,提问作者Phil Lawson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:23:11