excel - Excel根据单元格值在特定工作表上复制粘贴范围
问题描述
我有一个带有名为“NIR”的主表的工作簿,其中包含以下数据:A列包含产品名称(与其他工作表名称相同);B列包含数量,C列包含价格。
我想创建一个 VBA 以在 A 列的主工作表“NIR”中搜索,并根据主工作表“NIR”,A 列中的单元格将 B 列和 C 列复制到特定工作表。
例子:
Sheet "NIR"
A3="shoes"
A4="pants"
B3 = 3 (pairs)
C3 = 10 (price)
根据表“NIR” A3将B3和C3复制到表“鞋”和“裤子”
解决方案
尝试:
Option Explicit
Sub Macro1()
Dim LastRowNIR As Long, i As Long, LastRowWs As Long
Dim arr As Variant
With ThisWorkbook.Worksheets("NIR")
'Find the last row of column A
LastRowNIR = .Cells(.Rows.Count, "A").End(xlUp).Row
'Import all data in an array starting from A1 to C last row
arr = .Range("A1:C" & LastRowNIR)
'Loop array
For i = LBound(arr) To UBound(arr)
With ThisWorkbook.Worksheets(arr(i, 1))
'Find the last row of column B
LastRowWs = .Cells(.Rows.Count, "B").End(xlUp).Row
'Write in the next available row the quantity
.Range("B" & LastRowWs + 1).Value = arr(i, 2)
'Write in the next available row the prices
.Range("C" & LastRowWs + 1).Value = arr(i, 3)
End With
Next i
End With
End Sub
推荐阅读
- c# - InvalidOperationException:尝试评估 LINQ 查询参数表达式时引发异常
- macos - 如何在终端中使用代理?
- c# - HttpClient Polly WaitAndRetry 策略
- python - 为什么我的输入函数打印的是 else 语句而不是我定义的字符串?
- apache-spark - Spark FileAlreadyExistsException 在写入 JSON 文件时出现阶段失败
- angular - 令牌未以角度 WWW-Authenticate: Bearer 发送到服务器并得到 401 错误
- javascript - 使用 webkitRelativePath 时,路径分隔符是操作系统特定的吗?
- oauth-2.0 - 在 SPA 应用程序中保护没有 OAuth2 客户端凭据流的 Api 调用
- jquery - 在 jquery 中更改字体系列
- gnuplot - Gnuplot中的动态彩色条形图?