简体   繁体   中英

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. I need to copy each value in column A(text or numeric) and the format of the cell to the column B twice . The result should be like this :

在此处输入图片说明 How can this be done in VBA?

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

For Each ws In ThisWorkbook.Worksheets

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

End Sub

But if I put a non-numeric value in the first column it does not work. What I am missing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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