[英]How to copy a value from a cell to various cells VBA Excel
I will like to do something like this using VBA in Excel. 我想在Excel中使用VBA做类似的事情。 I need to copy each value in column A(text or numeric) and the format of the cell to the column B twice .
我需要将A列中的每个值(文本或数字)和单元格的格式复制到B列两次。 The result should be like this :
结果应该是这样的:
There are probably a dozen different answers to this question as you are not being very precise. 由于您不太精确,因此可能有十二个不同的答案。 Yet, this is one possible answer:
但是,这是一个可能的答案:
Option Explicit
Public Sub Test()
Dim lngRow As Long
For lngRow = 1 To 4
Worksheets("Sheet1").Cells(lngRow, 1).Formula = "=row()"
Worksheets("Sheet1").Cells(lngRow + lngRow - 1, 2).Formula = "=A" & lngRow
Worksheets("Sheet1").Cells(lngRow + lngRow, 2).Formula = "=A" & lngRow
Next lngRow
End Sub
I did it like this: 我这样做是这样的:
Private Sub CommandButton1_Click() Dim ws As Worksheet 专用子CommandButton1_Click()昏暗的工作表
For Each ws In ThisWorkbook.Worksheets 对于ThisWorkbook.Worksheets中的每个ws
Dim c As Range
For Each c In ws.UsedRange.Columns("A").Cells
ActiveSheet.Cells(c.Value, 1).Formula = "=row()"
ActiveSheet.Cells(c.Value + c.Value - 1, 2).Formula = "=A" & c.Value
ActiveSheet.Cells(c.Value + c.Value, 2).Formula = "=A" & c.Value
Next c Next ws 下一步c下一步ws
End Sub 结束子
But if I put a non-numeric value in the first column it does not work. 但是,如果我在第一列中放置一个非数字值,它将无法正常工作。 What I am missing?
我缺少什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.